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.
ina226.cpp@0:4318f664a8e1, 2015-12-02 (annotated)
- Committer:
- Caconym
- Date:
- Wed Dec 02 18:01:38 2015 +0000
- Revision:
- 0:4318f664a8e1
- Child:
- 1:5dc94660e311
e
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Caconym | 0:4318f664a8e1 | 1 | #include "ina226.h" |
Caconym | 0:4318f664a8e1 | 2 | |
Caconym | 0:4318f664a8e1 | 3 | INA226::INA226(I2C* theI2C, char address) |
Caconym | 0:4318f664a8e1 | 4 | { |
Caconym | 0:4318f664a8e1 | 5 | //set up config register //256 averages //continuous shunt voltage conversion |
Caconym | 0:4318f664a8e1 | 6 | short write_byte = CONF_DEFAULT |_BV(AVG2) | _BV(AVG0) | _BV(MODE3) | _BV(MODE1); |
Caconym | 0:4318f664a8e1 | 7 | |
Caconym | 0:4318f664a8e1 | 8 | write(REG_CONF, write_byte); |
Caconym | 0:4318f664a8e1 | 9 | //set up calibration (full range ~1.6A) |
Caconym | 0:4318f664a8e1 | 10 | write(REG_CAL, 0x0800); |
Caconym | 0:4318f664a8e1 | 11 | } |
Caconym | 0:4318f664a8e1 | 12 | |
Caconym | 0:4318f664a8e1 | 13 | void INA226::write(INA226REG reg, short value) |
Caconym | 0:4318f664a8e1 | 14 | { |
Caconym | 0:4318f664a8e1 | 15 | shortToChar stc; |
Caconym | 0:4318f664a8e1 | 16 | char cmd[3]; |
Caconym | 0:4318f664a8e1 | 17 | stc.u16 = value; |
Caconym | 0:4318f664a8e1 | 18 | |
Caconym | 0:4318f664a8e1 | 19 | cmd[0] = reg; |
Caconym | 0:4318f664a8e1 | 20 | cmd[1] = stc.u8[1]; |
Caconym | 0:4318f664a8e1 | 21 | cmd[2] = stc.u8[0]; |
Caconym | 0:4318f664a8e1 | 22 | |
Caconym | 0:4318f664a8e1 | 23 | i2c->write(i2cAddress,cmd,3); |
Caconym | 0:4318f664a8e1 | 24 | } |
Caconym | 0:4318f664a8e1 | 25 | |
Caconym | 0:4318f664a8e1 | 26 | short INA226::read(INA226REG reg) |
Caconym | 0:4318f664a8e1 | 27 | { |
Caconym | 0:4318f664a8e1 | 28 | shortToChar stc; |
Caconym | 0:4318f664a8e1 | 29 | |
Caconym | 0:4318f664a8e1 | 30 | char cmd[2]; |
Caconym | 0:4318f664a8e1 | 31 | cmd[0] = reg; |
Caconym | 0:4318f664a8e1 | 32 | i2c->write(i2cAddress,cmd,1); |
Caconym | 0:4318f664a8e1 | 33 | |
Caconym | 0:4318f664a8e1 | 34 | i2c->read(i2cAddress, cmd, 2); |
Caconym | 0:4318f664a8e1 | 35 | stc.u8[1] = cmd[0]; |
Caconym | 0:4318f664a8e1 | 36 | stc.u8[0] = cmd[1]; |
Caconym | 0:4318f664a8e1 | 37 | |
Caconym | 0:4318f664a8e1 | 38 | return (stc.u16); |
Caconym | 0:4318f664a8e1 | 39 | } |