The INA219 is a high-side current shunt and power monitor with an I2C interface. The INA219 monitors both shunt drop and supply voltage, with programmable conversion times and filtering. A programmable calibration value, combined with an internal multiplier, enables direct readouts in amperes. An additional multiplying register calculates power in watts. The I2C interface features 16 programmable addresses.
INA219.cpp@0:81c08f01f0fe, 2013-11-19 (annotated)
- Committer:
- mazgch
- Date:
- Tue Nov 19 05:31:39 2013 +0000
- Revision:
- 0:81c08f01f0fe
- Child:
- 1:2816fc874abc
draft;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mazgch | 0:81c08f01f0fe | 1 | #include "INA219.h" |
mazgch | 0:81c08f01f0fe | 2 | |
mazgch | 0:81c08f01f0fe | 3 | INA219::INA219(PinName sda, PinName scl, unsigned char adr) : |
mazgch | 0:81c08f01f0fe | 4 | _i2c(sda, scl) |
mazgch | 0:81c08f01f0fe | 5 | { |
mazgch | 0:81c08f01f0fe | 6 | _i2c.frequency(100000); |
mazgch | 0:81c08f01f0fe | 7 | _adr = adr; |
mazgch | 0:81c08f01f0fe | 8 | } |
mazgch | 0:81c08f01f0fe | 9 | |
mazgch | 0:81c08f01f0fe | 10 | bool INA219::detect(void) |
mazgch | 0:81c08f01f0fe | 11 | { |
mazgch | 0:81c08f01f0fe | 12 | _i2c.start(); |
mazgch | 0:81c08f01f0fe | 13 | _adr = (1 == _i2c.write(_adr|1/*write address for reading*/)); |
mazgch | 0:81c08f01f0fe | 14 | _i2c.stop(); |
mazgch | 0:81c08f01f0fe | 15 | return _det; |
mazgch | 0:81c08f01f0fe | 16 | } |
mazgch | 0:81c08f01f0fe | 17 | |
mazgch | 0:81c08f01f0fe | 18 | double INA219::getCurrent(void) |
mazgch | 0:81c08f01f0fe | 19 | { |
mazgch | 0:81c08f01f0fe | 20 | double d = 0.0; |
mazgch | 0:81c08f01f0fe | 21 | if (_det) |
mazgch | 0:81c08f01f0fe | 22 | { |
mazgch | 0:81c08f01f0fe | 23 | char r = 0x01; |
mazgch | 0:81c08f01f0fe | 24 | if (0 == _i2c.write(_adr, &r, sizeof(r), true)) |
mazgch | 0:81c08f01f0fe | 25 | { |
mazgch | 0:81c08f01f0fe | 26 | char v[2] = {0}; |
mazgch | 0:81c08f01f0fe | 27 | if (0 == _i2c.read(_adr, v, sizeof(v))) |
mazgch | 0:81c08f01f0fe | 28 | { |
mazgch | 0:81c08f01f0fe | 29 | int u = (int)((((short)v[0]) << 8) + (unsigned char)v[1]) * 10; // uV |
mazgch | 0:81c08f01f0fe | 30 | int i = u * 2/*0.5ohm*/; |
mazgch | 0:81c08f01f0fe | 31 | d = 1e-6 * i; |
mazgch | 0:81c08f01f0fe | 32 | } |
mazgch | 0:81c08f01f0fe | 33 | } |
mazgch | 0:81c08f01f0fe | 34 | else |
mazgch | 0:81c08f01f0fe | 35 | _i2c.stop(); |
mazgch | 0:81c08f01f0fe | 36 | } |
mazgch | 0:81c08f01f0fe | 37 | return d; |
mazgch | 0:81c08f01f0fe | 38 | } |
mazgch | 0:81c08f01f0fe | 39 | |
mazgch | 0:81c08f01f0fe | 40 | double INA219::getVoltage(void) |
mazgch | 0:81c08f01f0fe | 41 | { |
mazgch | 0:81c08f01f0fe | 42 | double d = 0.0; |
mazgch | 0:81c08f01f0fe | 43 | if (_det) |
mazgch | 0:81c08f01f0fe | 44 | { |
mazgch | 0:81c08f01f0fe | 45 | char r = 0x02; |
mazgch | 0:81c08f01f0fe | 46 | if (0 == _i2c.write(_adr, &r, sizeof(r), true)) |
mazgch | 0:81c08f01f0fe | 47 | { |
mazgch | 0:81c08f01f0fe | 48 | char v[2] = {0}; |
mazgch | 0:81c08f01f0fe | 49 | if (0 == _i2c.read(_adr, v, sizeof(v))) |
mazgch | 0:81c08f01f0fe | 50 | { |
mazgch | 0:81c08f01f0fe | 51 | int u = (int)(((((short)v[0]) << 8) + (unsigned char)v[1]) >> 3) * 4; // mV |
mazgch | 0:81c08f01f0fe | 52 | d = 1e-3 * u; |
mazgch | 0:81c08f01f0fe | 53 | } |
mazgch | 0:81c08f01f0fe | 54 | } |
mazgch | 0:81c08f01f0fe | 55 | else |
mazgch | 0:81c08f01f0fe | 56 | _i2c.stop(); |
mazgch | 0:81c08f01f0fe | 57 | } |
mazgch | 0:81c08f01f0fe | 58 | return d; |
mazgch | 0:81c08f01f0fe | 59 | } |