Basic INA219, with set calibaration and functions for reading raw register and write to register

Dependents:   SensorsThingSpeak

Committer:
tsoic
Date:
Fri Nov 20 08:24:43 2015 +0000
Revision:
0:cdfbda214bee
Child:
1:6b9f92e99dd7
Fork of INA219 library , just basic functions, read raw registar and write registar, calibration is changable only in library.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tsoic 0:cdfbda214bee 1 #include "INA219_reg.h"
tsoic 0:cdfbda214bee 2 #include "INA219.h"
tsoic 0:cdfbda214bee 3 #include "I2CR.h"
tsoic 0:cdfbda214bee 4
tsoic 0:cdfbda214bee 5 INA219::INA219() {
tsoic 0:cdfbda214bee 6 calibration();
tsoic 0:cdfbda214bee 7 }
tsoic 0:cdfbda214bee 8
tsoic 0:cdfbda214bee 9
tsoic 0:cdfbda214bee 10 void INA219::calibration(void) {
tsoic 0:cdfbda214bee 11
tsoic 0:cdfbda214bee 12 uint16_t data = 0;
tsoic 0:cdfbda214bee 13
tsoic 0:cdfbda214bee 14 data = (INA219_PAR_V_16V & 0x01) << 13;
tsoic 0:cdfbda214bee 15 data |= (INA219_PAR_G_320MV & 0x03) << 11;
tsoic 0:cdfbda214bee 16 data |= (INA219_PAR_B_12B_X128_68MS & 0x0f) << 6;
tsoic 0:cdfbda214bee 17 data |= (INA219_PAR_S_12B_X128_68MS & 0x0f) << 3;
tsoic 0:cdfbda214bee 18 data |= (INA219_PAR_M_SHNTBUS_CONT & 0x07);
tsoic 0:cdfbda214bee 19
tsoic 0:cdfbda214bee 20 dt[0] = INA219_CONFIG;
tsoic 0:cdfbda214bee 21 dt[1] = data >> 8; // MSB 1st
tsoic 0:cdfbda214bee 22 dt[2] = data & 0xff; // LSB 2nd
tsoic 0:cdfbda214bee 23 i2cr.write(INA219_ADDR_GG, (char *)dt, 3);
tsoic 0:cdfbda214bee 24
tsoic 0:cdfbda214bee 25 dt[0] = INA219_CALBLATION;
tsoic 0:cdfbda214bee 26 dt[1] = 5400 >> 8; // MSB 1st
tsoic 0:cdfbda214bee 27 dt[2] = 5400 & 0xff; // LSB 2nd
tsoic 0:cdfbda214bee 28 i2cr.write(INA219_ADDR_GG, (char *)dt, 3);
tsoic 0:cdfbda214bee 29 };
tsoic 0:cdfbda214bee 30
tsoic 0:cdfbda214bee 31 uint16_t INA219::readRawReg(uint8_t reg = 0x04)
tsoic 0:cdfbda214bee 32 {
tsoic 0:cdfbda214bee 33 dt[0] = reg;
tsoic 0:cdfbda214bee 34 i2cr.write(INA219_ADDR_GG, (char *)dt, 1);
tsoic 0:cdfbda214bee 35 i2cr.read(INA219_ADDR_GG, (char *)dt, 2);
tsoic 0:cdfbda214bee 36 int16_t data = (dt[0] << 8) | dt[1];
tsoic 0:cdfbda214bee 37 return data ;
tsoic 0:cdfbda214bee 38 }
tsoic 0:cdfbda214bee 39
tsoic 0:cdfbda214bee 40 uint8_t INA219::write_reg(uint8_t reg, uint8_t data)
tsoic 0:cdfbda214bee 41 {
tsoic 0:cdfbda214bee 42 dt[0] = reg;
tsoic 0:cdfbda214bee 43 dt[1] = data;
tsoic 0:cdfbda214bee 44 i2cr.write(INA219_ADDR_GG, (char *)dt, 2);
tsoic 0:cdfbda214bee 45 return dt[1];
tsoic 0:cdfbda214bee 46 }