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.

Dependents:   INA219TEST

Fork of INA219 by Michael Ammann

Files at this revision

API Documentation at this revision

Comitter:
x893
Date:
Mon Jun 30 08:27:27 2014 +0000
Parent:
2:a123ae7c1e4b
Commit message:
Fix errors

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
diff -r a123ae7c1e4b -r c4a937ab46bf INA219.cpp
--- a/INA219.cpp	Fri Nov 22 08:36:36 2013 +0000
+++ b/INA219.cpp	Mon Jun 30 08:27:27 2014 +0000
@@ -1,37 +1,58 @@
 #include "INA219.h"
 
-INA219::INA219(PinName sda, PinName scl, unsigned char adr) :
+INA219::INA219(PinName sda, PinName scl, unsigned char adr, int hz) :
     _i2c(sda, scl)
 {
-    _i2c.frequency(100000);
-    _adr = adr;
+    _i2c.frequency(hz);
+    Address = adr;
+}
+
+I2C INA219::getI2C(void)
+{
+    return _i2c;
 }
 
 bool INA219::detect(void)
 {
     _i2c.start();
-    bool det = (1 == _i2c.write(_adr|1/*write address for reading*/));
+    bool det = (0 == _i2c.write(Address | 1 /*write address for reading*/));
     _i2c.stop();
     return det;
 }
 
+bool INA219::getRegister(unsigned char reg, unsigned short *data)
+{
+    unsigned char v[2];
+    v[0] = reg;
+    if (0 == _i2c.write(Address, (char *)&v, 1, true))
+    {
+        if (0 == _i2c.read(Address, (char *)&v, 2))
+        {
+            *data = (v[0] << 8) | v[1];
+            return true;
+        }
+    }
+    else
+    {
+        _i2c.stop();
+    }
+    return false;
+}
+
+unsigned short INA219::getRegister(unsigned char reg)
+{
+    unsigned short data = 0;
+    getRegister(0x00, &data);
+    return data;
+}
+
 double INA219::getCurrent(void)
 {
     double d = 0.0;
-    char r = 0x01;
-    if (0 == _i2c.write(_adr, &r, sizeof(r), true))
+    unsigned short data;
+    if (getRegister(0x01, &data))
     {
-        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();
+        d = 1e-6 * ((int)data * 100); // uV * 0.1 ohm = A
     }
     return d;
 }
@@ -39,17 +60,10 @@
 double INA219::getVoltage(void)
 {
     double d = 0.0;
-    char r = 0x02;
-    if (0 == _i2c.write(_adr, &r, sizeof(r), true))
+    unsigned short data;
+    if (getRegister(0x02, &data))
     {
-        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;
-        }
+        d = 1e-3 * (int)(data >> 1); // V
     }
-    else 
-        _i2c.stop();
     return d;
 }
diff -r a123ae7c1e4b -r c4a937ab46bf INA219.h
--- a/INA219.h	Fri Nov 22 08:36:36 2013 +0000
+++ b/INA219.h	Mon Jun 30 08:27:27 2014 +0000
@@ -16,13 +16,16 @@
 class INA219
 {
 public: 
-    INA219(PinName sda, PinName scl, 
-            unsigned char adr /* range 0x80(1000000)-0x9E(1001111) */);
+    INA219(PinName sda, PinName scl,
+        unsigned char adr /* range 0x80(1000000)-0x9E(1001111) */,
+        int hz);
     bool detect(void);
+    I2C getI2C(void);
     double getCurrent(void);
     double getVoltage(void);
+    unsigned short getRegister(unsigned char reg);
+    bool getRegister(unsigned char reg, unsigned short *data);
+    unsigned char Address;
 protected:
-    bool _det;
-    unsigned char _adr;
     I2C _i2c;
 };