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.

Files at this revision

API Documentation at this revision

Comitter:
mazgch
Date:
Tue Nov 19 05:31:39 2013 +0000
Child:
1:2816fc874abc
Commit message:
draft;

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
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/INA219.cpp	Tue Nov 19 05:31:39 2013 +0000
@@ -0,0 +1,59 @@
+#include "INA219.h"
+
+INA219::INA219(PinName sda, PinName scl, unsigned char adr) :
+    _i2c(sda, scl)
+{
+    _i2c.frequency(100000);
+    _adr = adr;
+}
+
+bool INA219::detect(void)
+{
+    _i2c.start();
+    _adr = (1 == _i2c.write(_adr|1/*write address for reading*/));
+    _i2c.stop();
+    return _det;
+}
+
+double INA219::getCurrent(void)
+{
+    double d = 0.0;
+    if (_det)
+    {
+        char r = 0x01;
+        if (0 == _i2c.write(_adr, &r, sizeof(r), true))
+        {
+            char v[2] = {0};
+            if (0 == _i2c.read(_adr, v, sizeof(v)))
+            {
+                int u = (int)((((short)v[0]) << 8) + (unsigned char)v[1]) * 10; // uV
+                int i = u * 2/*0.5ohm*/;
+                d = 1e-6 * i;
+            }
+        }
+        else 
+            _i2c.stop();
+    }
+    return d;
+}
+
+double INA219::getVoltage(void)
+{
+    double d = 0.0;
+    if (_det)
+    {
+        char r = 0x02;
+        if (0 == _i2c.write(_adr, &r, sizeof(r), true))
+        {
+            char v[2] = {0};
+            if (0 == _i2c.read(_adr, v, sizeof(v)))
+            {
+                int u = (int)(((((short)v[0]) << 8) + (unsigned char)v[1]) >> 3) * 4; // mV
+                d = 1e-3 * u;
+            }
+        }
+        else 
+            _i2c.stop();
+    }
+    return d;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/INA219.h	Tue Nov 19 05:31:39 2013 +0000
@@ -0,0 +1,27 @@
+#include "mbed.h"
+
+
+/* Supply measurement chip (TI INA 219)
+   http://www.ti.com/product/ina219
+   
+   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 addresse
+*/
+class INA219
+{
+public: 
+    INA219(PinName sda, PinName scl, 
+            unsigned char adr /* range 0x80(1000000)-0x9E(1001111) */);
+    bool detect(void);
+    double getCurrent(void);
+    double getVoltage(void);
+protected:
+    bool _det;
+    unsigned char _adr;
+    I2C _i2c;
+};
\ No newline at end of file