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
00001 #include "mbed.h" 00002 #include "PQINA226.h" 00003 00004 INA226::INA226 (I2C &i2c, A0_t A0, A1_t A1) 00005 { 00006 _i2c = &i2c; 00007 _addr = (0b1000000 | A1 << 2 | A0) << 1; 00008 _i2c->frequency(400000); 00009 } 00010 00011 void INA226::begin() 00012 { 00013 cmd[0] = INA226_CALIBRATION; 00014 cmd[1] = 0x0A; 00015 cmd[2] = 0x00; 00016 _i2c->write(_addr, cmd, 3); 00017 } 00018 00019 bool INA226::test() 00020 { 00021 cmd[0] = INA226_WHO_AM_I; 00022 _i2c->write(_addr, cmd, 1); 00023 _i2c->read(_addr, buff, 1); 00024 if(buff[0] == 0x22) { 00025 return true; 00026 } else { 00027 return false; 00028 } 00029 } 00030 00031 void INA226::read(float *voltage, float *current, float *power) 00032 { 00033 read_voltage(voltage); 00034 read_current(current); 00035 read_power(power); 00036 } 00037 00038 void INA226::read_voltage(float *voltage) 00039 { 00040 cmd[0] = INA226_BUS_VOLTAGE; 00041 _i2c->write(_addr, cmd, 1); 00042 _i2c->read(_addr, buff, 2); 00043 *voltage = (short)(buff[0] << 8 | buff[1]) * INA226_VOLTAGE_LSB; 00044 } 00045 00046 void INA226::read_current(float *current) 00047 { 00048 cmd[0] = INA226_CURRENT; 00049 _i2c->write(_addr, cmd, 1); 00050 _i2c->read(_addr, buff, 2); 00051 *current = (short)(buff[0] << 8 | buff[1]); 00052 } 00053 00054 void INA226::read_power(float *power) 00055 { 00056 cmd[0] = INA226_POWER; 00057 _i2c->write(_addr, cmd, 1); 00058 _i2c->read(_addr, buff, 2); 00059 *power = (short)(buff[0] << 8 | buff[1]) * INA226_POWER_LSB; 00060 }
Generated on Fri Jul 22 2022 20:45:36 by
1.7.2