Library for INA219 current sensor
Revision 0:a576b66395ed, committed 2016-10-19
- Comitter:
- maximusismax
- Date:
- Wed Oct 19 10:19:57 2016 +0000
- Commit message:
- INA219 Library (Author: Jack Cosslett)
Changed in this revision
INA219.cpp | Show annotated file Show diff for this revision Revisions of this file |
INA219.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r a576b66395ed INA219.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/INA219.cpp Wed Oct 19 10:19:57 2016 +0000 @@ -0,0 +1,51 @@ +#include "INA219.h" + +INA219::INA219(I2C& theI2C, char address) + :i2c(theI2C), i2cAddress(address) +{ + init(); +} + +void INA219::init() +{ + //set up config register //256 averages //continuous shunt voltage conversion + short write_byte = CONF_DEFAULT & ~_BV(BRNG); + + write(REG_CONF, write_byte); + //set up calibration (full range ~5A) + write(REG_CAL, 0x14F7); +} + +void INA219::write(INA219REG reg, short value) +{ + shortToChar stc; + char cmd[3]; + stc.u16 = value; + + cmd[0] = reg; + cmd[1] = stc.u8[1]; + cmd[2] = stc.u8[0]; + + i2c.write(i2cAddress,cmd,3); +} + +short INA219::read(INA219REG reg) +{ + shortToChar stc; + + char cmd[2]; + cmd[0] = reg; + i2c.write(i2cAddress,cmd,1); + + i2c.read(i2cAddress, cmd, 2); + stc.u8[1] = cmd[0]; + stc.u8[0] = cmd[1]; + + return (stc.u16); +} + +float INA219::getCurrent() +{ + short curr = read(REG_CURR); + return (float)curr / 6554; +} \ No newline at end of file
diff -r 000000000000 -r a576b66395ed INA219.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/INA219.h Wed Oct 19 10:19:57 2016 +0000 @@ -0,0 +1,85 @@ +/* + * ina226.h + * + * driver for INA219 current monitor chip + * + * Created: 03/11/2015 17:13:11 + * Author: Jack + */ + + +#ifndef INA219_H_ +#define INA219_H_ + +#include "mbed.h" + +#define INA219_1 0x40 +#define INA219_2 0x41 + +/* +//INA226 register addresses +#define REG_CONF 0x00 +#define REG_SHNT 0x01 +#define REG_BUS 0x02 +#define REG_PWR 0x03 +#define REG_CURR 0x04 +#define REG_CAL 0x05 +*/ +enum INA219REG +{ + REG_CONF=0x00, + REG_SHNT=0x01, + REG_BUS=0x02, + REG_PWR=0x03, + REG_CURR=0x04, + REG_CAL=0x05, +}; +//BITS +//CONFIG REGISTER +#define CONF_DEFAULT 0x399F + +#define RST 15 //reset bit +#define BRNG 13 //bus voltage range: 0=16V, 1=32V +#define PG1 12 // PGA gain +#define PG0 11 // & range +#define BADC4 10 // bus adc resolution/averaging +#define BADC3 9 // +#define BADC2 8 // +#define BADC1 7 // +#define SADC4 6 // shunt adc resolution/averaging +#define SADC3 5 // +#define SADC2 4 // +#define SADC1 3 // +#define MODE3 2 // operating mode +#define MODE2 1 // +#define MODE1 0 // + +#define _BV(bit) (1 << (bit)) + +/* +void ina226_init(I2C* i2c, char addr); +void ina226_write(I2C* i2c, char addr, char reg, short value); +short ina226_read(I2C* i2c, char addr, char reg); +*/ + +union shortToChar +{ + short u16; + char u8[2]; +}; + +class INA219 +{ + public: + INA219(I2C& theI2C, char address); + void init(); + void write(INA219REG reg, short value); + short read(INA219REG reg); + float getCurrent(); + private: + I2C& i2c; + char i2cAddress; +}; + + +#endif /* INA219_H_ */ \ No newline at end of file