Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: ina226.cpp
- Revision:
- 1:5dc94660e311
- Parent:
- 0:4318f664a8e1
diff -r 4318f664a8e1 -r 5dc94660e311 ina226.cpp
--- a/ina226.cpp Wed Dec 02 18:01:38 2015 +0000
+++ b/ina226.cpp Thu Mar 17 14:53:03 2016 +0000
@@ -1,6 +1,12 @@
#include "ina226.h"
-INA226::INA226(I2C* theI2C, char address)
+INA226::INA226(I2C& theI2C, char address)
+ :i2c(theI2C), i2cAddress(address)
+{
+ init();
+}
+
+void INA226::init()
{
//set up config register //256 averages //continuous shunt voltage conversion
short write_byte = CONF_DEFAULT |_BV(AVG2) | _BV(AVG0) | _BV(MODE3) | _BV(MODE1);
@@ -20,7 +26,7 @@
cmd[1] = stc.u8[1];
cmd[2] = stc.u8[0];
- i2c->write(i2cAddress,cmd,3);
+ i2c.write(i2cAddress,cmd,3);
}
short INA226::read(INA226REG reg)
@@ -29,11 +35,17 @@
char cmd[2];
cmd[0] = reg;
- i2c->write(i2cAddress,cmd,1);
+ i2c.write(i2cAddress,cmd,1);
- i2c->read(i2cAddress, cmd, 2);
+ i2c.read(i2cAddress, cmd, 2);
stc.u8[1] = cmd[0];
stc.u8[0] = cmd[1];
return (stc.u16);
+}
+
+float INA226::getCurrent()
+{
+ short curr = read(REG_CURR);
+ return curr/20;
}
\ No newline at end of file