-
Notifications
You must be signed in to change notification settings - Fork 0
/
INA219.cpp
167 lines (133 loc) · 4.76 KB
/
INA219.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
Project : LaLiMat project (https://www.youtube.com/playlist?list=PLJBKmE2nNweRXOebZjydkMEiq2pHtBMOS in Chinese)
file : INA219.cpp
Author : ykchau
youtube : youtube.com/ykchau888
Licenese : GPL-3.0
Please let me know if you use it commercial project.
*/
#include "INA219.h"
/*****************************
Private Functions
*****************************/
uint16_t INA219::I2C_Read(byte addr) {
Wire.beginTransmission(INA219_I2C_ADDRESS);
Wire.write(addr);
Wire.endTransmission();
Wire.requestFrom(INA219_I2C_ADDRESS,2);
byte val_H = 0;
byte val_L = 0;
while ( !Wire.available() ) {}
val_H = Wire.read();
val_L = Wire.read();
return ( val_L + val_H * 256 );
}
void INA219::I2C_Write(byte addr, uint16_t data) {
byte data_H = data >> 8;
byte data_L = byte(data);
Wire.beginTransmission(INA219_I2C_ADDRESS);
Wire.write(addr);
Wire.write(data_H);
Wire.write(data_L);
Wire.endTransmission();
}
float INA219::getRawShuntVoltage() {
return static_cast<int16_t>(I2C_Read(INA219_REG_SHUNT_VOLT));
}
float INA219::getRawBusVoltage() {
uint16_t ret = I2C_Read(INA219_REG_BUS_VOLT);
busVoltageOverflow = ret & 0x0001;
busVoltageConversionReady = ret & 0x0002;
return ( ret >> 3 );
}
float INA219::getRawPower() {
setCalibrationValue();
return I2C_Read(INA219_REG_POWER);
}
float INA219::getRawCurrent() {
setCalibrationValue();
return static_cast<int16_t>(I2C_Read(INA219_REG_CURRENT));
}
/*****************************
Public Functions
*****************************/
INA219::INA219() {
Wire.begin();
I2C_Write(INA219_REG_CONFIG, INA219_DEFAULT_CONFIG);
I2C_Write(INA219_REG_CALIBRATION, INA219_DEFAULT_CALIBATION);
}
void INA219::config(int16_t BRNG, int16_t PGA, int16_t BADC, int16_t SADC, int16_t MODE) {
settings.BRNG = BRNG;
settings.PGA = PGA;
settings.BADC = BADC;
settings.SADC = SADC;
settings.MODE = MODE;
I2C_Write(INA219_REG_CONFIG, BRNG | PGA | BADC | SADC | MODE);
}
void INA219::reset() {
I2C_Write(INA219_REG_CONFIG, INA219_RST);
settings.MODE = INA219_SB_CONT;
}
void INA219::setMode(byte mode) {
settings.MODE = mode;
config(settings.BRNG, settings.PGA, settings.BADC, settings.SADC, settings.MODE);
}
void INA219::standby() {
config(settings.BRNG, settings.PGA, settings.BADC, settings.SADC, INA219_POWER_DOWN);
}
void INA219::resume() {
config(settings.BRNG, settings.PGA, settings.BADC, settings.SADC, settings.MODE);
}
void INA219::measure() {
measurement.shuntVoltage = getRawShuntVoltage();
measurement.busVoltage = getRawBusVoltage();
measurement.current = getRawCurrent();
measurement.power = getRawPower();
}
uint16_t INA219::getConfig() {
return I2C_Read(INA219_REG_CONFIG);
}
uint16_t INA219::getCalibrationValue() {
return I2C_Read(INA219_REG_CALIBRATION);
}
void INA219::setCalibrationValue() {
I2C_Write(INA219_REG_CALIBRATION, calculations.calibrationValue);
}
void INA219::calCalibrationValue(byte MaxExpectedCurrent_A ) {
// Data sheet page 12
// Cal = trunc ( 0,04096 / (Current_LSB * RShunt) )
//
// 0.04096 is an internal fixed value used to ensure scaling is maintained properly
// Current_LSB = Maximum Expected Current / 2^15
// R_Shunt = 0.1 ohm
float VBusMax = 16.0; // Max Bus Voltage, V
float VShuntMax = 40.0; // Max Shunt Voltage, mV
// get VBusMax from BRNG config'ed
if ( settings.BRNG == INA219_BRNG_32_FSR ) {
VBusMax = 32.0;
}
// get VShuntMax from PGA config'ed
switch ( settings.PGA ) {
case 0x0000: VShuntMax = 40.0; break;
case 0x1000: VShuntMax = 80.0; break;
case 0x2000: VShuntMax = 160.0; break;
case 0x3000: VShuntMax = 320.0; break;
}
calculations.lsb.current = MaxExpectedCurrent_A / INA219_2_POW_15;
calculations.lsb.power = 20 * calculations.lsb.current;
calculations.MaxPossibleCurrent = VShuntMax / INA219_SHUNT_OHM;
calculations.MaxCurrent = calculations.lsb.current * INA219_2_POW_15;
if ( calculations.MaxCurrent > calculations.MaxPossibleCurrent ) {
calculations.MaxCurrentBeforeOverflow = calculations.MaxPossibleCurrent;
} else {
calculations.MaxCurrentBeforeOverflow = calculations.MaxCurrent;
}
calculations.MaxShuntVoltage = calculations.MaxCurrentBeforeOverflow * INA219_SHUNT_OHM;
if ( calculations.MaxShuntVoltage > VShuntMax ) {
calculations.MaxShuntVoltageBeforeOverflow = VShuntMax;
} else {
calculations.MaxShuntVoltageBeforeOverflow = calculations.MaxShuntVoltage;
}
calculations.MaxPower = calculations.MaxCurrentBeforeOverflow * VBusMax;
calculations.calibrationValue = trunc(0.04096 / (calculations.lsb.current * INA219_SHUNT_OHM));
}