電流電圧センサINA226のライブラリ
Dependents: Hybrid_IZU2021_MAIN_OS5 Hybrid_IZU2021_MAIN
PQ_INA226.cpp@0:4c315fe513d0, 2020-12-15 (annotated)
- Committer:
- tanahashi
- Date:
- Tue Dec 15 15:37:10 2020 +0000
- Revision:
- 0:4c315fe513d0
- Child:
- 1:415a7cc3ec67
first commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tanahashi | 0:4c315fe513d0 | 1 | #include "mbed.h" |
tanahashi | 0:4c315fe513d0 | 2 | #include "PQ_INA226.h" |
tanahashi | 0:4c315fe513d0 | 3 | |
tanahashi | 0:4c315fe513d0 | 4 | INA226::INA226(I2C &i2c, A0_t A0, A1_t A1) |
tanahashi | 0:4c315fe513d0 | 5 | { |
tanahashi | 0:4c315fe513d0 | 6 | _i2c = &i2c; |
tanahashi | 0:4c315fe513d0 | 7 | _addr = (0b1000000 | A1 << 2 | A0) << 1; |
tanahashi | 0:4c315fe513d0 | 8 | _i2c->frequency(400000); |
tanahashi | 0:4c315fe513d0 | 9 | } |
tanahashi | 0:4c315fe513d0 | 10 | |
tanahashi | 0:4c315fe513d0 | 11 | void INA226::begin(){ |
tanahashi | 0:4c315fe513d0 | 12 | cmd[0] = INA226_CALIBRATION; |
tanahashi | 0:4c315fe513d0 | 13 | cmd[1] = 0x0A; |
tanahashi | 0:4c315fe513d0 | 14 | cmd[2] = 0x00; |
tanahashi | 0:4c315fe513d0 | 15 | _i2c->write(_addr, cmd, 3); |
tanahashi | 0:4c315fe513d0 | 16 | } |
tanahashi | 0:4c315fe513d0 | 17 | |
tanahashi | 0:4c315fe513d0 | 18 | bool INA226::test() |
tanahashi | 0:4c315fe513d0 | 19 | { |
tanahashi | 0:4c315fe513d0 | 20 | cmd[0] = INA226_WHO_AM_I; |
tanahashi | 0:4c315fe513d0 | 21 | _i2c->write(_addr, cmd, 1); |
tanahashi | 0:4c315fe513d0 | 22 | _i2c->read(_addr, buff, 1); |
tanahashi | 0:4c315fe513d0 | 23 | if(buff[0] == 0x22) { |
tanahashi | 0:4c315fe513d0 | 24 | return true; |
tanahashi | 0:4c315fe513d0 | 25 | } else { |
tanahashi | 0:4c315fe513d0 | 26 | return false; |
tanahashi | 0:4c315fe513d0 | 27 | } |
tanahashi | 0:4c315fe513d0 | 28 | } |
tanahashi | 0:4c315fe513d0 | 29 | |
tanahashi | 0:4c315fe513d0 | 30 | void INA226::read(float *voltage, float *current, float *power) |
tanahashi | 0:4c315fe513d0 | 31 | { |
tanahashi | 0:4c315fe513d0 | 32 | read_voltage(voltage); |
tanahashi | 0:4c315fe513d0 | 33 | read_current(current); |
tanahashi | 0:4c315fe513d0 | 34 | read_power(power); |
tanahashi | 0:4c315fe513d0 | 35 | } |
tanahashi | 0:4c315fe513d0 | 36 | |
tanahashi | 0:4c315fe513d0 | 37 | void INA226::read_voltage(float *voltage){ |
tanahashi | 0:4c315fe513d0 | 38 | cmd[0] = INA226_BUS_VOLTAGE; |
tanahashi | 0:4c315fe513d0 | 39 | _i2c->write(_addr, cmd, 1); |
tanahashi | 0:4c315fe513d0 | 40 | _i2c->read(_addr, buff, 2); |
tanahashi | 0:4c315fe513d0 | 41 | *voltage = (short)(buff[0] << 8 | buff[1]) * INA226_VOLTAGE_LSB; |
tanahashi | 0:4c315fe513d0 | 42 | } |
tanahashi | 0:4c315fe513d0 | 43 | |
tanahashi | 0:4c315fe513d0 | 44 | void INA226::read_current(float *current){ |
tanahashi | 0:4c315fe513d0 | 45 | cmd[0] = INA226_CURRENT; |
tanahashi | 0:4c315fe513d0 | 46 | _i2c->write(_addr, cmd, 1); |
tanahashi | 0:4c315fe513d0 | 47 | _i2c->read(_addr, buff, 2); |
tanahashi | 0:4c315fe513d0 | 48 | *current = (short)(buff[0] << 8 | buff[1]); |
tanahashi | 0:4c315fe513d0 | 49 | } |
tanahashi | 0:4c315fe513d0 | 50 | |
tanahashi | 0:4c315fe513d0 | 51 | void INA226::read_power(float *power) |
tanahashi | 0:4c315fe513d0 | 52 | { |
tanahashi | 0:4c315fe513d0 | 53 | cmd[0] = INA226_POWER; |
tanahashi | 0:4c315fe513d0 | 54 | _i2c->write(_addr, cmd, 1); |
tanahashi | 0:4c315fe513d0 | 55 | _i2c->read(_addr, buff, 2); |
tanahashi | 0:4c315fe513d0 | 56 | *power = (short)(buff[0] << 8 | buff[1]) * INA226_POWER_LSB; |
tanahashi | 0:4c315fe513d0 | 57 | } |