forked from jarzebski/Arduino-INA219
-
Notifications
You must be signed in to change notification settings - Fork 0
/
INA219.cpp
269 lines (209 loc) · 5.39 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/*
INA219.cpp - Class file for the INA219 Zero-Drift, Bi-directional Current/Power Monitor Arduino Library.
Version: 1.0.0
(c) 2014 Korneliusz Jarzebski
www.jarzebski.pl
This program is free software: you can redistribute it and/or modify
it under the terms of the version 3 GNU General Public License as
published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Wire.h>
#include "INA219.h"
bool INA219::begin(uint8_t address)
{
Wire.begin();
inaAddress = address;
return true;
}
bool INA219::configure(ina219_range_t range, ina219_gain_t gain, ina219_busRes_t busRes, ina219_shuntRes_t shuntRes, ina219_mode_t mode)
{
uint16_t config = 0;
config |= (range << 13 | gain << 11 | busRes << 7 | shuntRes << 3 | mode);
switch(range)
{
case INA219_RANGE_32V:
vBusMax = 32.0f;
break;
case INA219_RANGE_16V:
vBusMax = 16.0f;
break;
}
switch(gain)
{
case INA219_GAIN_320MV:
vShuntMax = 0.32f;
break;
case INA219_GAIN_160MV:
vShuntMax = 0.16f;
break;
case INA219_GAIN_80MV:
vShuntMax = 0.08f;
break;
case INA219_GAIN_40MV:
vShuntMax = 0.04f;
break;
}
writeRegister16(INA219_REG_CONFIG, config);
return true;
}
bool INA219::calibrate(float rShuntValue, float iMaxExpected)
{
uint16_t calibrationValue;
rShunt = rShuntValue;
float iMaxPossible, minimumLSB;
iMaxPossible = vShuntMax / rShunt;
minimumLSB = iMaxExpected / 32767;
currentLSB = (uint16_t)(minimumLSB * 100000000);
currentLSB /= 100000000;
currentLSB /= 0.0001;
currentLSB = ceil(currentLSB);
currentLSB *= 0.0001;
powerLSB = currentLSB * 20;
calibrationValue = (uint16_t)((0.04096) / (currentLSB * rShunt));
writeRegister16(INA219_REG_CALIBRATION, calibrationValue);
return true;
}
float INA219::getMaxPossibleCurrent(void)
{
return (vShuntMax / rShunt);
}
float INA219::getMaxCurrent(void)
{
float maxCurrent = (currentLSB * 32767);
float maxPossible = getMaxPossibleCurrent();
if (maxCurrent > maxPossible)
{
return maxPossible;
} else
{
return maxCurrent;
}
}
float INA219::getMaxShuntVoltage(void)
{
float maxVoltage = getMaxCurrent() * rShunt;
if (maxVoltage >= vShuntMax)
{
return vShuntMax;
} else
{
return maxVoltage;
}
}
float INA219::getMaxPower(void)
{
return (getMaxCurrent() * vBusMax);
}
float INA219::readBusPower(void)
{
return (readRegister16(INA219_REG_POWER) * powerLSB);
}
float INA219::readShuntCurrent(void)
{
return (readRegister16(INA219_REG_CURRENT) * currentLSB);
}
float INA219::readShuntVoltage(void)
{
float voltage;
voltage = readRegister16(INA219_REG_SHUNTVOLTAGE);
return (voltage / 100000);
}
float INA219::readBusVoltage(void)
{
int16_t voltage;
voltage = readRegister16(INA219_REG_BUSVOLTAGE);
voltage >>= 3;
return (voltage * 0.004);
}
ina219_range_t INA219::getRange(void)
{
uint16_t value;
value = readRegister16(INA219_REG_CONFIG);
value &= 0b0010000000000000;
value >>= 13;
return (ina219_range_t)value;
}
ina219_gain_t INA219::getGain(void)
{
uint16_t value;
value = readRegister16(INA219_REG_CONFIG);
value &= 0b0001100000000000;
value >>= 11;
return (ina219_gain_t)value;
}
ina219_busRes_t INA219::getBusRes(void)
{
uint16_t value;
value = readRegister16(INA219_REG_CONFIG);
value &= 0b0000011110000000;
value >>= 7;
return (ina219_busRes_t)value;
}
ina219_shuntRes_t INA219::getShuntRes(void)
{
uint16_t value;
value = readRegister16(INA219_REG_CONFIG);
value &= 0b0000000001111000;
value >>= 3;
return (ina219_shuntRes_t)value;
}
ina219_mode_t INA219::getMode(void)
{
uint16_t value;
value = readRegister16(INA219_REG_CONFIG);
value &= 0b0000000000000111;
return (ina219_mode_t)value;
}
int16_t INA219::readRegister16(uint8_t reg)
{
int16_t value;
Wire.beginTransmission(inaAddress);
#if ARDUINO >= 100
Wire.write(reg);
#else
Wire.send(reg);
#endif
Wire.endTransmission();
delay(1);
Wire.beginTransmission(inaAddress);
Wire.requestFrom(inaAddress, 2);
while(!Wire.available()) {};
#if ARDUINO >= 100
uint8_t vha = Wire.read();
uint8_t vla = Wire.read();
#else
uint8_t vha = Wire.receive();
uint8_t vla = Wire.receive();
#endif;
Wire.endTransmission();
value = vha << 8 | vla;
return value;
}
void INA219::writeRegister16(uint8_t reg, uint16_t val)
{
uint8_t vla;
vla = (uint8_t)val;
val >>= 8;
Wire.beginTransmission(inaAddress);
#if ARDUINO >= 100
Wire.write(reg);
Wire.write((uint8_t)val);
Wire.write(vla);
#else
Wire.send(reg);
Wire.send((uint8_t)val);
Wire.send(vla);
#endif
Wire.endTransmission();
}