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 07:44:01 2013 +0000
Parent:
0:81c08f01f0fe
Child:
2:a123ae7c1e4b
Commit message:
eof

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
--- a/INA219.cpp	Tue Nov 19 05:31:39 2013 +0000
+++ b/INA219.cpp	Tue Nov 19 07:44:01 2013 +0000
@@ -10,29 +10,28 @@
 bool INA219::detect(void)
 {
     _i2c.start();
-    _adr = (1 == _i2c.write(_adr|1/*write address for reading*/));
+    bool det = (1 == _i2c.write(_adr|1/*write address for reading*/));
     _i2c.stop();
-    return _det;
+    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 r = 0x01;
-        if (0 == _i2c.write(_adr, &r, sizeof(r), true))
+        char v[2] = {0};
+        if (0 == _i2c.read(_adr, v, sizeof(v)))
         {
-            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;
-            }
+            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();
+    }
+    else 
+    {
+        _i2c.stop();
     }
     return d;
 }
@@ -40,20 +39,17 @@
 double INA219::getVoltage(void)
 {
     double d = 0.0;
-    if (_det)
+    char r = 0x02;
+    if (0 == _i2c.write(_adr, &r, sizeof(r), true))
     {
-        char r = 0x02;
-        if (0 == _i2c.write(_adr, &r, sizeof(r), true))
+        char v[2] = {0};
+        if (0 == _i2c.read(_adr, v, sizeof(v)))
         {
-            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;
-            }
+            int u = (int)(((((short)v[0]) << 8) + (unsigned char)v[1]) >> 3) * 4; // mV
+            d = 1e-3 * u;
         }
-        else 
-            _i2c.stop();
     }
+    else 
+        _i2c.stop();
     return d;
 }
--- a/INA219.h	Tue Nov 19 05:31:39 2013 +0000
+++ b/INA219.h	Tue Nov 19 07:44:01 2013 +0000
@@ -24,4 +24,4 @@
     bool _det;
     unsigned char _adr;
     I2C _i2c;
-};
\ No newline at end of file
+};