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@1:5dc94660e311, 2016-03-17 (annotated)
- Committer:
- Caconym
- Date:
- Thu Mar 17 14:53:03 2016 +0000
- Revision:
- 1:5dc94660e311
- Parent:
- 0:4318f664a8e1
yo
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 | 1:5dc94660e311 | 3 | INA226::INA226(I2C& theI2C, char address) |
Caconym | 1:5dc94660e311 | 4 | :i2c(theI2C), i2cAddress(address) |
Caconym | 1:5dc94660e311 | 5 | { |
Caconym | 1:5dc94660e311 | 6 | init(); |
Caconym | 1:5dc94660e311 | 7 | } |
Caconym | 1:5dc94660e311 | 8 | |
Caconym | 1:5dc94660e311 | 9 | void INA226::init() |
Caconym | 0:4318f664a8e1 | 10 | { |
Caconym | 0:4318f664a8e1 | 11 | //set up config register //256 averages //continuous shunt voltage conversion |
Caconym | 0:4318f664a8e1 | 12 | short write_byte = CONF_DEFAULT |_BV(AVG2) | _BV(AVG0) | _BV(MODE3) | _BV(MODE1); |
Caconym | 0:4318f664a8e1 | 13 | |
Caconym | 0:4318f664a8e1 | 14 | write(REG_CONF, write_byte); |
Caconym | 0:4318f664a8e1 | 15 | //set up calibration (full range ~1.6A) |
Caconym | 0:4318f664a8e1 | 16 | write(REG_CAL, 0x0800); |
Caconym | 0:4318f664a8e1 | 17 | } |
Caconym | 0:4318f664a8e1 | 18 | |
Caconym | 0:4318f664a8e1 | 19 | void INA226::write(INA226REG reg, short value) |
Caconym | 0:4318f664a8e1 | 20 | { |
Caconym | 0:4318f664a8e1 | 21 | shortToChar stc; |
Caconym | 0:4318f664a8e1 | 22 | char cmd[3]; |
Caconym | 0:4318f664a8e1 | 23 | stc.u16 = value; |
Caconym | 0:4318f664a8e1 | 24 | |
Caconym | 0:4318f664a8e1 | 25 | cmd[0] = reg; |
Caconym | 0:4318f664a8e1 | 26 | cmd[1] = stc.u8[1]; |
Caconym | 0:4318f664a8e1 | 27 | cmd[2] = stc.u8[0]; |
Caconym | 0:4318f664a8e1 | 28 | |
Caconym | 1:5dc94660e311 | 29 | i2c.write(i2cAddress,cmd,3); |
Caconym | 0:4318f664a8e1 | 30 | } |
Caconym | 0:4318f664a8e1 | 31 | |
Caconym | 0:4318f664a8e1 | 32 | short INA226::read(INA226REG reg) |
Caconym | 0:4318f664a8e1 | 33 | { |
Caconym | 0:4318f664a8e1 | 34 | shortToChar stc; |
Caconym | 0:4318f664a8e1 | 35 | |
Caconym | 0:4318f664a8e1 | 36 | char cmd[2]; |
Caconym | 0:4318f664a8e1 | 37 | cmd[0] = reg; |
Caconym | 1:5dc94660e311 | 38 | i2c.write(i2cAddress,cmd,1); |
Caconym | 0:4318f664a8e1 | 39 | |
Caconym | 1:5dc94660e311 | 40 | i2c.read(i2cAddress, cmd, 2); |
Caconym | 0:4318f664a8e1 | 41 | stc.u8[1] = cmd[0]; |
Caconym | 0:4318f664a8e1 | 42 | stc.u8[0] = cmd[1]; |
Caconym | 0:4318f664a8e1 | 43 | |
Caconym | 0:4318f664a8e1 | 44 | return (stc.u16); |
Caconym | 1:5dc94660e311 | 45 | } |
Caconym | 1:5dc94660e311 | 46 | |
Caconym | 1:5dc94660e311 | 47 | float INA226::getCurrent() |
Caconym | 1:5dc94660e311 | 48 | { |
Caconym | 1:5dc94660e311 | 49 | short curr = read(REG_CURR); |
Caconym | 1:5dc94660e311 | 50 | return curr/20; |
Caconym | 0:4318f664a8e1 | 51 | } |