INA226 Library
Dependents: HYBRYD2018_IZU_ROCKET Hybrid_IZU2019 SWAN_IZU2019_v1 Hybrid_OB2021_MAIN
Diff: INA226.cpp
- Revision:
- 0:ad712ebc38d6
- Child:
- 1:9c367d227582
diff -r 000000000000 -r ad712ebc38d6 INA226.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/INA226.cpp Mon Jul 10 10:50:39 2017 +0000 @@ -0,0 +1,148 @@ +#include"mbed.h" +#include"INA226.h" + +//Serial pc(USBTX, USBRX); //tx, rx + +myINA226::myINA226(I2C &i2cBus){ + i2c = &i2cBus; + i2c->frequency(400000); +} + + +void myINA226::set_callibretion(unsigned int val) +{ + char reg = cal_reg; + char cmd[3]; + cmd[0] = reg; + cmd[1] = static_cast<char>(val>>8 & 0x00FF); + cmd[2] = static_cast<char>(val & 0x00FF); + + i2c->write(SLV_WRITE, cmd, 1); +} + +int myINA226::Connection_check() +{ + char reg = check_reg; + char check; + + i2c->write(SLV_WRITE, ®, 1); + i2c->read(SLV_READ, check, 2); + + if(check[0] == 0x54 && check[1] == 0x49)return 0; + else return 1; +} + +void myINA226::setup(int i) +{ + char reg = Ctrl_reg; + char cmd[3][3]; + int j; + + for(j=0;j<3;j++){cmd[j][0]=reg; cmd[j][1]=0x41;} + cmd[0][2]=0xFF;cmd[1][2]=0xFE;cmd[2][2]=0xFD; + + i2c->write(SLV_WRITE, cmd[i], 3); +} + +void myINA226::get_Voltage_current(float *V_bus, float *current) +{ + char reg = V_bus_reg; + char buff[2]; + unsigned int val[2]; + + setup(0); + + i2c->write(SLV_WRITE, ®, 1); + wait_ms(10); + i2c->read(SLV_READ, buff, 2, false); + + val[0] = (unsigned int)buff[0]<<8; + val[1] = (unsigned int)buff[1]; + + *V_bus = (float)(val[0] | val[1]); + *V_bus = *V_bus*1.25; + + reg = current_reg; + + i2c->write(SLV_WRITE, ®, 1); + wait_ms(10); + i2c->read(SLV_READ, buff, 2, false); + + val[0] = (unsigned int)buff[0]<<8; + val[1] = (unsigned int)buff[1]; + + *current = (float)(val[0] | val[1]); + *current = *current;//1.25; +} + +void myINA226::get_Shunt_voltage(float *V_shunt) +{ + char reg = V_shunt_reg; + char buff[2]; + unsigned int val[2]; + + setup(2); + + i2c->write(SLV_WRITE, ®, 1); + wait_ms(10); + i2c->read(SLV_READ, buff, 2, false); + + val[0] = (unsigned int)buff[0]<<8; + val[1] = (unsigned int)buff[1]; + + *V_shunt = (float)(val[0] | val[1]); + *V_shunt = *V_shunt*1.25; +} + +void myINA226::get_Bus_voltage(float *V_bus) +{ + char reg = V_bus_reg; + char buff[2]; + unsigned int val[2]; + + setup(1); + + i2c->write(SLV_WRITE, ®, 1); + wait_ms(10); + i2c->read(SLV_READ, buff, 2, false); + + val[0] = (unsigned int)buff[0]<<8; + val[1] = (unsigned int)buff[1]; + + *V_bus = (float)(val[0] | val[1]); + *V_bus = *V_bus*1.25; +} + +void myINA226::get_current(float *current) +{ + + char reg = current_reg; + char buff[2]; + unsigned int val[2]; + + i2c->write(SLV_WRITE, ®, 1); + wait_ms(10); + i2c->read(SLV_READ, buff, 2, false); + + val[0] = (unsigned int)buff[0]<<8; + val[1] = (unsigned int)buff[1]; + + *current = (float)(val[0] | val[1]); + *current = *current;//*1.25; +} + +void myINA226::get_power(float *power) +{ + char reg = power_reg; + char buff[2]; + unsigned int val[2]; + + i2c->write(SLV_WRITE, ®, 1); + wait_ms(10); + i2c->read(SLV_READ, buff, 2, false); + + val[0] = (unsigned int)buff[0]<<8; + val[1] = (unsigned int)buff[1]; + + *power = (float)(val[0] | val[1]); +}