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@2:2a98c184354f, 2019-12-04 (annotated)
- Committer:
- tanahashi
- Date:
- Wed Dec 04 08:18:37 2019 +0000
- Revision:
- 2:2a98c184354f
- Parent:
- 1:13a0acb5a366
- Child:
- 3:252d161776cc
fixed WHO_AM_I address
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| tanahashi | 0:a88db0ffce6e | 1 | #include "mbed.h" |
| tanahashi | 0:a88db0ffce6e | 2 | #include "PQINA226.h" |
| tanahashi | 0:a88db0ffce6e | 3 | |
| tanahashi | 0:a88db0ffce6e | 4 | INA226::INA226(I2C &i2c, A0_t A0, A1_t A1) |
| tanahashi | 0:a88db0ffce6e | 5 | { |
| tanahashi | 0:a88db0ffce6e | 6 | _i2c = &i2c; |
| tanahashi | 1:13a0acb5a366 | 7 | _addr = (0b1000000 | A1 << 2 | A0) << 1; |
| tanahashi | 0:a88db0ffce6e | 8 | _i2c->frequency(400000); |
| tanahashi | 0:a88db0ffce6e | 9 | } |
| tanahashi | 0:a88db0ffce6e | 10 | |
| tanahashi | 0:a88db0ffce6e | 11 | void INA226::begin(){ |
| tanahashi | 0:a88db0ffce6e | 12 | cmd[0] = CALIBRATION; |
| tanahashi | 0:a88db0ffce6e | 13 | cmd[1] = 0x0A; |
| tanahashi | 0:a88db0ffce6e | 14 | cmd[2] = 0x00; |
| tanahashi | 0:a88db0ffce6e | 15 | _i2c->write(_addr, cmd, 3); |
| tanahashi | 0:a88db0ffce6e | 16 | } |
| tanahashi | 0:a88db0ffce6e | 17 | |
| tanahashi | 0:a88db0ffce6e | 18 | bool INA226::test() |
| tanahashi | 0:a88db0ffce6e | 19 | { |
| tanahashi | 0:a88db0ffce6e | 20 | cmd[0] = WHO_AM_I; |
| tanahashi | 0:a88db0ffce6e | 21 | _i2c->write(_addr, cmd, 1); |
| tanahashi | 2:2a98c184354f | 22 | _i2c->read(_addr, buff, 1); |
| tanahashi | 2:2a98c184354f | 23 | if(buff[0] == 0x22) { |
| tanahashi | 0:a88db0ffce6e | 24 | return true; |
| tanahashi | 0:a88db0ffce6e | 25 | } else { |
| tanahashi | 0:a88db0ffce6e | 26 | return false; |
| tanahashi | 0:a88db0ffce6e | 27 | } |
| tanahashi | 0:a88db0ffce6e | 28 | } |
| tanahashi | 0:a88db0ffce6e | 29 | |
| tanahashi | 0:a88db0ffce6e | 30 | void INA226::read(float *voltage, float *current, float *power) |
| tanahashi | 0:a88db0ffce6e | 31 | { |
| tanahashi | 0:a88db0ffce6e | 32 | read_voltage(voltage); |
| tanahashi | 0:a88db0ffce6e | 33 | read_current(current); |
| tanahashi | 0:a88db0ffce6e | 34 | read_power(power); |
| tanahashi | 0:a88db0ffce6e | 35 | } |
| tanahashi | 0:a88db0ffce6e | 36 | |
| tanahashi | 0:a88db0ffce6e | 37 | void INA226::read_voltage(float *voltage){ |
| tanahashi | 0:a88db0ffce6e | 38 | cmd[0] = BUS_VOLTAGE; |
| tanahashi | 0:a88db0ffce6e | 39 | _i2c->write(_addr, cmd, 1); |
| tanahashi | 0:a88db0ffce6e | 40 | _i2c->read(_addr, buff, 2); |
| tanahashi | 0:a88db0ffce6e | 41 | *voltage = (short)(buff[0] << 8 | buff[1]) * VOLTAGE_LSB; |
| tanahashi | 0:a88db0ffce6e | 42 | } |
| tanahashi | 0:a88db0ffce6e | 43 | |
| tanahashi | 0:a88db0ffce6e | 44 | void INA226::read_current(float *current){ |
| tanahashi | 0:a88db0ffce6e | 45 | cmd[0] = CURRENT; |
| tanahashi | 0:a88db0ffce6e | 46 | _i2c->write(_addr, cmd, 1); |
| tanahashi | 0:a88db0ffce6e | 47 | _i2c->read(_addr, buff, 2); |
| tanahashi | 0:a88db0ffce6e | 48 | *current = (short)(buff[0] << 8 | buff[1]); |
| tanahashi | 0:a88db0ffce6e | 49 | } |
| tanahashi | 0:a88db0ffce6e | 50 | |
| tanahashi | 0:a88db0ffce6e | 51 | void INA226::read_power(float *power) |
| tanahashi | 0:a88db0ffce6e | 52 | { |
| tanahashi | 0:a88db0ffce6e | 53 | cmd[0] = POWER; |
| tanahashi | 0:a88db0ffce6e | 54 | _i2c->write(_addr, cmd, 1); |
| tanahashi | 0:a88db0ffce6e | 55 | _i2c->read(_addr, buff, 2); |
| tanahashi | 0:a88db0ffce6e | 56 | *power = (short)(buff[0] << 8 | buff[1]); |
| tanahashi | 0:a88db0ffce6e | 57 | } |