Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //By Steef
- //MCP3221.h
- #ifndef MCP3221_H
- #define MCP3221_H
- #include "I2C.h"
- using namespace std;
- class MCP3221 {
- private:
- int ADDRESS_MCP3221;
- I2C *bus;
- int VDD;
- public:
- MCP3221(int address,I2C *bus, int VDD);
- float getval();
- };
- #endif
- //MCP3221.cpp
- #include "MCP3221.h"
- MCP3221::MCP3221(int address, I2C* bus, int VDD)
- {
- ADDRESS_MCP3221 = address;
- this->bus= bus;
- this->VDD = VDD;
- bus->setAddress(ADDRESS_MCP3221);
- }
- float MCP3221::getval()
- {
- vector<unsigned char> buffer;
- float val;
- bus->setAddress(ADDRESS_MCP3221);
- bus->receive(buffer, 2);
- if(buffer.size() == 2){
- val = ( static_cast<int>(buffer.at(0))<<8) + static_cast<int>(buffer.at(1) );
- val = (val/4095)*VDD;//MCP3221 -> 12 bits
- }
- return val;
- }
Advertisement
Add Comment
Please, Sign In to add comment