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.

Committer:
mazgch
Date:
Fri Nov 22 08:36:36 2013 +0000
Revision:
2:a123ae7c1e4b
Parent:
1:2816fc874abc
update

Who changed what in which revision?

UserRevisionLine numberNew 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 1:2816fc874abc 13 bool det = (1 == _i2c.write(_adr|1/*write address for reading*/));
mazgch 0:81c08f01f0fe 14 _i2c.stop();
mazgch 1:2816fc874abc 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 1:2816fc874abc 21 char r = 0x01;
mazgch 1:2816fc874abc 22 if (0 == _i2c.write(_adr, &r, sizeof(r), true))
mazgch 0:81c08f01f0fe 23 {
mazgch 1:2816fc874abc 24 char v[2] = {0};
mazgch 1:2816fc874abc 25 if (0 == _i2c.read(_adr, v, sizeof(v)))
mazgch 0:81c08f01f0fe 26 {
mazgch 1:2816fc874abc 27 int u = (int)((((short)v[0]) << 8) + (unsigned char)v[1]) * 10; // uV
mazgch 1:2816fc874abc 28 int i = u * 2/*0.5ohm*/;
mazgch 1:2816fc874abc 29 d = 1e-6 * i;
mazgch 0:81c08f01f0fe 30 }
mazgch 1:2816fc874abc 31 }
mazgch 1:2816fc874abc 32 else
mazgch 1:2816fc874abc 33 {
mazgch 1:2816fc874abc 34 _i2c.stop();
mazgch 0:81c08f01f0fe 35 }
mazgch 0:81c08f01f0fe 36 return d;
mazgch 0:81c08f01f0fe 37 }
mazgch 0:81c08f01f0fe 38
mazgch 0:81c08f01f0fe 39 double INA219::getVoltage(void)
mazgch 0:81c08f01f0fe 40 {
mazgch 0:81c08f01f0fe 41 double d = 0.0;
mazgch 1:2816fc874abc 42 char r = 0x02;
mazgch 1:2816fc874abc 43 if (0 == _i2c.write(_adr, &r, sizeof(r), true))
mazgch 0:81c08f01f0fe 44 {
mazgch 1:2816fc874abc 45 char v[2] = {0};
mazgch 1:2816fc874abc 46 if (0 == _i2c.read(_adr, v, sizeof(v)))
mazgch 0:81c08f01f0fe 47 {
mazgch 1:2816fc874abc 48 int u = (int)(((((short)v[0]) << 8) + (unsigned char)v[1]) >> 3) * 4; // mV
mazgch 1:2816fc874abc 49 d = 1e-3 * u;
mazgch 0:81c08f01f0fe 50 }
mazgch 0:81c08f01f0fe 51 }
mazgch 1:2816fc874abc 52 else
mazgch 1:2816fc874abc 53 _i2c.stop();
mazgch 0:81c08f01f0fe 54 return d;
mazgch 0:81c08f01f0fe 55 }