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.
ina226.cpp
- Committer:
- Caconym
- Date:
- 2016-03-17
- Revision:
- 1:5dc94660e311
- Parent:
- 0:4318f664a8e1
File content as of revision 1:5dc94660e311:
#include "ina226.h" 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); write(REG_CONF, write_byte); //set up calibration (full range ~1.6A) write(REG_CAL, 0x0800); } void INA226::write(INA226REG reg, short value) { shortToChar stc; char cmd[3]; stc.u16 = value; cmd[0] = reg; cmd[1] = stc.u8[1]; cmd[2] = stc.u8[0]; i2c.write(i2cAddress,cmd,3); } short INA226::read(INA226REG reg) { shortToChar stc; char cmd[2]; cmd[0] = reg; i2c.write(i2cAddress,cmd,1); 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; }