Steefmaester

[classe/driver] MCP3221

Sep 8th, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.90 KB | None | 0 0
  1. //By Steef
  2.  
  3. //MCP3221.h
  4. #ifndef MCP3221_H
  5. #define MCP3221_H
  6.  
  7. #include "I2C.h"
  8.  
  9. using namespace std;
  10.  
  11.  
  12. class MCP3221 {
  13.  
  14. private:
  15.    
  16.     int ADDRESS_MCP3221;
  17.     I2C *bus;
  18.     int VDD;
  19.    
  20. public:    
  21.  
  22.     MCP3221(int address,I2C *bus, int VDD);
  23.     float getval();
  24.  
  25. };
  26.  
  27.  
  28. #endif
  29.  
  30.  
  31.  
  32. //MCP3221.cpp
  33. #include "MCP3221.h"
  34.  
  35.  
  36.  
  37. MCP3221::MCP3221(int address, I2C* bus, int VDD)
  38. {
  39.     ADDRESS_MCP3221 = address;
  40.     this->bus= bus;
  41.     this->VDD = VDD;
  42.     bus->setAddress(ADDRESS_MCP3221);
  43.    
  44. }
  45. float MCP3221::getval()
  46. {
  47.     vector<unsigned char> buffer;
  48.     float val;
  49.    
  50.     bus->setAddress(ADDRESS_MCP3221);
  51.     bus->receive(buffer, 2);
  52.    
  53.     if(buffer.size() == 2){
  54.        
  55.         val = ( static_cast<int>(buffer.at(0))<<8) + static_cast<int>(buffer.at(1) );
  56.  
  57.         val = (val/4095)*VDD;//MCP3221 -> 12 bits
  58.        
  59.     }
  60.    
  61.     return val;
  62.    
  63. }
Advertisement
Add Comment
Please, Sign In to add comment