Basic INA219, with set calibaration and functions for reading raw register and write to register
INA219.cpp@1:6b9f92e99dd7, 2015-11-29 (annotated)
- Committer:
- tsoic
- Date:
- Sun Nov 29 13:40:51 2015 +0000
- Revision:
- 1:6b9f92e99dd7
- Parent:
- 0:cdfbda214bee
Comments
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
tsoic | 1:6b9f92e99dd7 | 1 | /** This is |
tsoic | 1:6b9f92e99dd7 | 2 | ** |
tsoic | 1:6b9f92e99dd7 | 3 | */ |
tsoic | 1:6b9f92e99dd7 | 4 | |
tsoic | 0:cdfbda214bee | 5 | #include "INA219_reg.h" |
tsoic | 0:cdfbda214bee | 6 | #include "INA219.h" |
tsoic | 0:cdfbda214bee | 7 | #include "I2CR.h" |
tsoic | 0:cdfbda214bee | 8 | |
tsoic | 0:cdfbda214bee | 9 | INA219::INA219() { |
tsoic | 0:cdfbda214bee | 10 | calibration(); |
tsoic | 0:cdfbda214bee | 11 | } |
tsoic | 0:cdfbda214bee | 12 | |
tsoic | 0:cdfbda214bee | 13 | |
tsoic | 0:cdfbda214bee | 14 | void INA219::calibration(void) { |
tsoic | 0:cdfbda214bee | 15 | |
tsoic | 0:cdfbda214bee | 16 | uint16_t data = 0; |
tsoic | 0:cdfbda214bee | 17 | |
tsoic | 0:cdfbda214bee | 18 | data = (INA219_PAR_V_16V & 0x01) << 13; |
tsoic | 0:cdfbda214bee | 19 | data |= (INA219_PAR_G_320MV & 0x03) << 11; |
tsoic | 0:cdfbda214bee | 20 | data |= (INA219_PAR_B_12B_X128_68MS & 0x0f) << 6; |
tsoic | 0:cdfbda214bee | 21 | data |= (INA219_PAR_S_12B_X128_68MS & 0x0f) << 3; |
tsoic | 0:cdfbda214bee | 22 | data |= (INA219_PAR_M_SHNTBUS_CONT & 0x07); |
tsoic | 0:cdfbda214bee | 23 | |
tsoic | 0:cdfbda214bee | 24 | dt[0] = INA219_CONFIG; |
tsoic | 0:cdfbda214bee | 25 | dt[1] = data >> 8; // MSB 1st |
tsoic | 0:cdfbda214bee | 26 | dt[2] = data & 0xff; // LSB 2nd |
tsoic | 0:cdfbda214bee | 27 | i2cr.write(INA219_ADDR_GG, (char *)dt, 3); |
tsoic | 0:cdfbda214bee | 28 | |
tsoic | 0:cdfbda214bee | 29 | dt[0] = INA219_CALBLATION; |
tsoic | 0:cdfbda214bee | 30 | dt[1] = 5400 >> 8; // MSB 1st |
tsoic | 0:cdfbda214bee | 31 | dt[2] = 5400 & 0xff; // LSB 2nd |
tsoic | 0:cdfbda214bee | 32 | i2cr.write(INA219_ADDR_GG, (char *)dt, 3); |
tsoic | 0:cdfbda214bee | 33 | }; |
tsoic | 0:cdfbda214bee | 34 | |
tsoic | 0:cdfbda214bee | 35 | uint16_t INA219::readRawReg(uint8_t reg = 0x04) |
tsoic | 0:cdfbda214bee | 36 | { |
tsoic | 0:cdfbda214bee | 37 | dt[0] = reg; |
tsoic | 0:cdfbda214bee | 38 | i2cr.write(INA219_ADDR_GG, (char *)dt, 1); |
tsoic | 0:cdfbda214bee | 39 | i2cr.read(INA219_ADDR_GG, (char *)dt, 2); |
tsoic | 0:cdfbda214bee | 40 | int16_t data = (dt[0] << 8) | dt[1]; |
tsoic | 0:cdfbda214bee | 41 | return data ; |
tsoic | 0:cdfbda214bee | 42 | } |
tsoic | 0:cdfbda214bee | 43 | |
tsoic | 1:6b9f92e99dd7 | 44 | void INA219::write_reg(uint8_t reg, uint8_t data) |
tsoic | 0:cdfbda214bee | 45 | { |
tsoic | 0:cdfbda214bee | 46 | dt[0] = reg; |
tsoic | 0:cdfbda214bee | 47 | dt[1] = data; |
tsoic | 0:cdfbda214bee | 48 | i2cr.write(INA219_ADDR_GG, (char *)dt, 2); |
tsoic | 0:cdfbda214bee | 49 | } |