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.
Dependents: IZU2020_AVIONICS IZU2020_AVIONICS
PQINA226.cpp
- Committer:
- tanahashi
- Date:
- 2019-12-04
- Revision:
- 3:252d161776cc
- Parent:
- 2:2a98c184354f
- Child:
- 5:7f4ebd11d751
File content as of revision 3:252d161776cc:
#include "mbed.h"
#include "PQINA226.h"
INA226::INA226(I2C &i2c, A0_t A0, A1_t A1)
{
_i2c = &i2c;
_addr = (0b1000000 | A1 << 2 | A0) << 1;
_i2c->frequency(400000);
}
void INA226::begin(){
cmd[0] = CALIBRATION;
cmd[1] = 0x0A;
cmd[2] = 0x00;
_i2c->write(_addr, cmd, 3);
}
bool INA226::test()
{
cmd[0] = WHO_AM_I;
_i2c->write(_addr, cmd, 1);
_i2c->read(_addr, buff, 1);
if(buff[0] == 0x22) {
return true;
} else {
return false;
}
}
void INA226::read(float *voltage, float *current, float *power)
{
read_voltage(voltage);
read_current(current);
read_power(power);
}
void INA226::read_voltage(float *voltage){
cmd[0] = BUS_VOLTAGE;
_i2c->write(_addr, cmd, 1);
_i2c->read(_addr, buff, 2);
*voltage = (short)(buff[0] << 8 | buff[1]) * VOLTAGE_LSB;
}
void INA226::read_current(float *current){
cmd[0] = CURRENT;
_i2c->write(_addr, cmd, 1);
_i2c->read(_addr, buff, 2);
*current = (short)(buff[0] << 8 | buff[1]);
}
void INA226::read_power(float *power)
{
cmd[0] = POWER;
_i2c->write(_addr, cmd, 1);
_i2c->read(_addr, buff, 2);
*power = (short)(buff[0] << 8 | buff[1]) * POWER_LSB;
}