-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRoot4root_INA219.cpp
More file actions
executable file
·317 lines (272 loc) · 7.56 KB
/
Root4root_INA219.cpp
File metadata and controls
executable file
·317 lines (272 loc) · 7.56 KB
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
/*
Root4root_INA219 - library for Texas Instruments INA219 - Bidirectional Current/Power Monitor
Project page: https://github.com/root4root/Root4root_INA219
*/
/*
MIT License
Copyright (c) 2020 root4root
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "Root4root_INA219.h"
/**
* A constructor.
*
* @param addr the I2C address of a device. Default is 0x40
* @param theWire the TwoWire interface pointer
*/
Root4root_INA219::Root4root_INA219(uint8_t addr, TwoWire *theWire)
{
this->ina219_i2caddr = addr;
this->i2c = theWire;
}
/**
* Initialize I2C, set expected max current and shunt resistance
*
* @param expected max current in milliAmps. Default is 2000.
* @param rshunt the shutn resistance in milliOhms. Default is 100.
*
* @return void
*/
void Root4root_INA219::begin(uint16_t expected, uint8_t rshunt)
{
this->i2c->begin();
setCalibration(expected, rshunt);
}
/**
* Calculate calibration value, current LSB abd power LSB
* See datasheet for more details.
*
* @param expected max current in milliAmps.
* @param rshunt the shutn resistance in milliOhms.
*
* @return void
*/
void Root4root_INA219::setCalibration(uint16_t expected, uint8_t rshunt)
{
this->ina219_currentLSB = (float)expected/32768.0;
this->ina219_calibrationValue = (uint32_t)(40960.0/(this->ina219_currentLSB * rshunt));
this->ina219_powerLSB = 20 * this->ina219_currentLSB;
writeRegister(INA219_CALIBRATION_REGISTER, this->ina219_calibrationValue);
}
/**
* Changes configuration register in binary format.
*
* @param value 16 bit of config value
* @param mask for isolating unchanged bits.
*
* @return void
*/
void Root4root_INA219::changeConfig(uint16_t value, uint16_t mask)
{
this->ina219_config &= ~mask;
this->ina219_config |= value;
writeRegister(INA219_CONFIG_REGISTER, this->ina219_config);
}
/**
* Get bus voltage.
*
* @return 16 bit value in milliVolts.
*/
uint16_t Root4root_INA219::getBusVoltage_mV()
{
uint16_t value;
readRegister(INA219_BUS_VOLTAGE_REGISTER, &value);
// Shift to the right 3 to drop CNVR and OVF and multiply by LSB
return (uint16_t)((value >> 3) * 4);
}
/**
* Get bus voltage in Volts
*
* @return float value in Volts
*/
float Root4root_INA219::getBusVoltage_V()
{
int16_t value = getBusVoltage_mV();
return value * 0.001; // x/1000
}
/**
* Get shunt voltage in milliVolts
*
* @return float value in mV
*/
float Root4root_INA219::getShuntVoltage_mV()
{
int16_t value;
value = getShuntVoltage_raw();
//LSB 10uV, x = value*10/1000 = value/100mV or value * 0.01
return value * 0.01;
}
/**
* Get content of shunt register. (raw, not in Volts)
*
* @return 16 bit shunt voltage register value
*/
int16_t Root4root_INA219::getShuntVoltage_raw()
{
uint16_t value;
readRegister(INA219_SHUNT_VOLTAGE_REGISTER, &value);
return (int16_t)value;
}
/**
* Get current value in milliApms
*
* @return float mAmp
*/
float Root4root_INA219::getCurrent_mA()
{
float valueDec = getCurrent_raw();
valueDec *= this->ina219_currentLSB;
return valueDec;
}
/**
* Get current register value (raw, not in Amps).
* If value is 0x00, initiates calibration register checking/updating.
* And call itself again recursively.
* @see checkConfig()
*
* @return 16 bit register value (raw, not in Amps)
*/
int16_t Root4root_INA219::getCurrent_raw()
{
uint16_t value;
readRegister(INA219_CURRENT_REGISTER, &value);
if (!value) {
if(!checkConfig()) {
delay(10);
return getCurrent_raw();
}
}
return (int16_t)value;
}
/**
* Get power value in milliWatts
*
* @return float power value
*/
float Root4root_INA219::getPower_mW()
{
float valueDec = getPower_raw();
valueDec *= this->ina219_powerLSB;
return valueDec;
}
/**
* Get power register value (raw, not in Watts)
* If value is 0x00 initiates calibration register checking/updating.
* And call itself again recursively.
* @see checkConfig()
*
* @return 16 bit power register value
*/
int16_t Root4root_INA219::getPower_raw()
{
uint16_t value;
readRegister(INA219_POWER_REGISTER, &value);
if (!value) {
if(!checkConfig()) {
delay(10);
return getPower_raw();
}
}
return (int16_t)value;
}
/**
* Enable or disable power save mode
*
* @param on boolean true - enable, false - disable
*
* @return void
*/
void Root4root_INA219::powerSave(bool on)
{
if (on) {
uint16_t save = this->ina219_config & ~INA219_CONFIG_MODE_MASK;
writeRegister(INA219_CONFIG_REGISTER, save);
} else {
writeRegister(INA219_CONFIG_REGISTER, this->ina219_config);
}
}
/**
* Resets INA219 IC.
*
* @return void
*/
void Root4root_INA219::reset()
{
writeRegister(INA219_CONFIG_REGISTER, INA219_CONFIG_RESET);
delay(10);
}
/**
* Write register value to IC
*
* @param reg 8 bit register address
* @param value 16 bit register value
*
* @return void
*/
void Root4root_INA219::writeRegister(uint8_t reg, uint16_t value)
{
this->i2c->beginTransmission(ina219_i2caddr);
this->i2c->write(reg); // Register
this->i2c->write((uint8_t)(value >> 8)); // Upper 8-bit
this->i2c->write((uint8_t)(value & 0xFF)); // Lower 8-bit
this->i2c->endTransmission();
}
/**
* Reads register value from IC
*
* @param reg 8 bit register address
* @param value pointer to 16 bit variable to store read value
*
* @return void
*/
void Root4root_INA219::readRegister(uint8_t reg, uint16_t *value)
{
this->i2c->beginTransmission(ina219_i2caddr);
this->i2c->write(reg); // Register
this->i2c->endTransmission();
delay(5); // Max 12-bit conversion time is 586us per sample
this->i2c->requestFrom(ina219_i2caddr, (uint8_t)2);
*value = ((i2c->read() << 8) | i2c->read());
}
/**
* Check calibration registers for correct value
* in case unwanted resets, which may be occour switching on/off a sharp load.
* If so, calibration and configuration registers will be written up again.
*
* @return void
*/
bool Root4root_INA219::checkConfig()
{
uint16_t calibrationRegister = 0;
readRegister(INA219_CALIBRATION_REGISTER, &calibrationRegister);
if (!calibrationRegister) {
writeRegister(INA219_CONFIG_REGISTER, this->ina219_config);
writeRegister(INA219_CALIBRATION_REGISTER, this->ina219_calibrationValue);
return false;
}
return true;
}
/**
* Measure in "triggered" mode.
* According to datasheet, we have to write configuration register to do so every time.
*
* @return void
*/
void Root4root_INA219::trigger()
{
writeRegister(INA219_CONFIG_REGISTER, this->ina219_config);
}