Library for INA219 current sensor
INA219.cpp@0:a576b66395ed, 2016-10-19 (annotated)
- Committer:
- maximusismax
- Date:
- Wed Oct 19 10:19:57 2016 +0000
- Revision:
- 0:a576b66395ed
INA219 Library (Author: Jack Cosslett)
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
maximusismax | 0:a576b66395ed | 1 | #include "INA219.h" |
maximusismax | 0:a576b66395ed | 2 | |
maximusismax | 0:a576b66395ed | 3 | INA219::INA219(I2C& theI2C, char address) |
maximusismax | 0:a576b66395ed | 4 | :i2c(theI2C), i2cAddress(address) |
maximusismax | 0:a576b66395ed | 5 | { |
maximusismax | 0:a576b66395ed | 6 | init(); |
maximusismax | 0:a576b66395ed | 7 | } |
maximusismax | 0:a576b66395ed | 8 | |
maximusismax | 0:a576b66395ed | 9 | void INA219::init() |
maximusismax | 0:a576b66395ed | 10 | { |
maximusismax | 0:a576b66395ed | 11 | //set up config register //256 averages //continuous shunt voltage conversion |
maximusismax | 0:a576b66395ed | 12 | short write_byte = CONF_DEFAULT & ~_BV(BRNG); |
maximusismax | 0:a576b66395ed | 13 | |
maximusismax | 0:a576b66395ed | 14 | write(REG_CONF, write_byte); |
maximusismax | 0:a576b66395ed | 15 | //set up calibration (full range ~5A) |
maximusismax | 0:a576b66395ed | 16 | write(REG_CAL, 0x14F7); |
maximusismax | 0:a576b66395ed | 17 | } |
maximusismax | 0:a576b66395ed | 18 | |
maximusismax | 0:a576b66395ed | 19 | void INA219::write(INA219REG reg, short value) |
maximusismax | 0:a576b66395ed | 20 | { |
maximusismax | 0:a576b66395ed | 21 | shortToChar stc; |
maximusismax | 0:a576b66395ed | 22 | char cmd[3]; |
maximusismax | 0:a576b66395ed | 23 | stc.u16 = value; |
maximusismax | 0:a576b66395ed | 24 | |
maximusismax | 0:a576b66395ed | 25 | cmd[0] = reg; |
maximusismax | 0:a576b66395ed | 26 | cmd[1] = stc.u8[1]; |
maximusismax | 0:a576b66395ed | 27 | cmd[2] = stc.u8[0]; |
maximusismax | 0:a576b66395ed | 28 | |
maximusismax | 0:a576b66395ed | 29 | i2c.write(i2cAddress,cmd,3); |
maximusismax | 0:a576b66395ed | 30 | } |
maximusismax | 0:a576b66395ed | 31 | |
maximusismax | 0:a576b66395ed | 32 | short INA219::read(INA219REG reg) |
maximusismax | 0:a576b66395ed | 33 | { |
maximusismax | 0:a576b66395ed | 34 | shortToChar stc; |
maximusismax | 0:a576b66395ed | 35 | |
maximusismax | 0:a576b66395ed | 36 | char cmd[2]; |
maximusismax | 0:a576b66395ed | 37 | cmd[0] = reg; |
maximusismax | 0:a576b66395ed | 38 | i2c.write(i2cAddress,cmd,1); |
maximusismax | 0:a576b66395ed | 39 | |
maximusismax | 0:a576b66395ed | 40 | i2c.read(i2cAddress, cmd, 2); |
maximusismax | 0:a576b66395ed | 41 | stc.u8[1] = cmd[0]; |
maximusismax | 0:a576b66395ed | 42 | stc.u8[0] = cmd[1]; |
maximusismax | 0:a576b66395ed | 43 | |
maximusismax | 0:a576b66395ed | 44 | return (stc.u16); |
maximusismax | 0:a576b66395ed | 45 | } |
maximusismax | 0:a576b66395ed | 46 | |
maximusismax | 0:a576b66395ed | 47 | float INA219::getCurrent() |
maximusismax | 0:a576b66395ed | 48 | { |
maximusismax | 0:a576b66395ed | 49 | short curr = read(REG_CURR); |
maximusismax | 0:a576b66395ed | 50 | return (float)curr / 6554; |
maximusismax | 0:a576b66395ed | 51 | } |