forked from RobTillaart/INA228
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathINA228.cpp
More file actions
513 lines (429 loc) · 11.2 KB
/
Copy pathINA228.cpp
File metadata and controls
513 lines (429 loc) · 11.2 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
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
// FILE: INA228.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.1
// DATE: 2024-05-09
// PURPOSE: Arduino library for INA228 voltage, current and power sensor.
// URL: https://github.com/RobTillaart/INA228
//
// Read the datasheet for the details
#include "INA228.h"
// REGISTERS ADDRESS BITS RW
#define INA228_CONFIG 0x00 // 16 RW
#define INA228_ADC_CONFIG 0x01 // 16 RW
#define INA228_SHUNT_CAL 0x02 // 16 RW
#define INA228_SHUNT_TEMP_CO 0x03 // 16 RW
#define INA228_SHUNT_VOLTAGE 0x04 // 24 R-
#define INA228_BUS_VOLTAGE 0x05 // 24 R-
#define INA228_TEMPERATURE 0x06 // 16 R-
#define INA228_CURRENT 0x07 // 24 R-
#define INA228_POWER 0x08 // 24 R-
#define INA228_ENERGY 0x09 // 40 R-
#define INA228_CHARGE 0x0A // 40 R-
#define INA228_DIAG_ALERT 0x0B // 16 RW
#define INA228_SOVL 0x0C // 16 RW
#define INA228_SUVL 0x0D // 16 RW
#define INA228_BOVL 0x0E // 16 RW
#define INA228_BUVL 0x0F // 16 RW
#define INA228_TEMP_LIMIT 0x10 // 16 RW
#define INA228_POWER_LIMIT 0x11 // 16 RW
#define INA228_MANUFACTURER 0x3E // 16 R-
#define INA228_DEVICE_ID 0x3F // 16 R-
////////////////////////////////////////////////////////
//
// CONSTRUCTOR
//
INA228::INA228(const uint8_t address, TwoWire *wire)
{
_address = address;
_wire = wire;
// no calibrated values by default.
_shunt = 0.015;
_maxCurrent = 10.0;
_current_LSB = _maxCurrent * pow(2, -19);
}
bool INA228::begin()
{
if (! isConnected()) return false;
return true;
}
bool INA228::isConnected()
{
_wire->beginTransmission(_address);
return ( _wire->endTransmission() == 0);
}
uint8_t INA228::getAddress()
{
return _address;
}
////////////////////////////////////////////////////////
//
// CORE FUNCTIONS
//
// PAGE 25
float INA228::getBusVoltage()
{
uint32_t value = _readRegister(INA228_BUS_VOLTAGE, 3);
value >>= 4;
return value * 195.3125e-6;
}
// PAGE 25
float INA228::getShuntVoltage()
{
uint32_t value = _readRegister(INA228_SHUNT_VOLTAGE, 3);
value >>= 4;
// depends on ADCRANGE in INA228_CONFIG register.
uint16_t config = _readRegister(INA228_CONFIG, 2);
if (config & 0x0008) return value * 78.125e-9;
return value * 312.5e-9;
}
// PAGE 25 + 8.1.2
float INA228::getCurrent()
{
uint32_t value = _readRegister(INA228_CURRENT, 3);
// PAGE 31 (8.1.2)
float shunt_cal = 13107.2e6 * _current_LSB * _shunt;
// depends on ADCRANGE in INA228_CONFIG register.
uint16_t config = _readRegister(INA228_CONFIG, 2);
if (config & 0x0008) shunt_cal *= 4;
return value * shunt_cal;
}
// PAGE 26 + 8.1.2
float INA228::getPower()
{
uint32_t value = _readRegister(INA228_POWER, 3);
// PAGE 31 (8.1.2)
return value * 3.2 * _current_LSB;
}
// PAGE25
float INA228::getTemperature()
{
uint32_t value = _readRegister(INA228_TEMPERATURE, 2);
return value * 7.8125e-3;
}
// PAGE 26 + 8.1.2
float INA228::getEnergy()
{
float value = _readRegisterF(INA228_ENERGY, 5);
// PAGE 31 (8.1.2)
return value * 16 * 3.2 * _current_LSB;
}
float INA228::getCharge()
{
float value = _readRegisterF(INA228_CHARGE, 5);
// PAGE 32 (8.1.2)
return value * _current_LSB;
}
////////////////////////////////////////////////////////
//
// CONFIG REGISTER 0
//
void INA228::reset()
{
uint16_t value = _readRegister(INA228_CONFIG, 2);
value |= 0x8000;
_writeRegister(INA228_CONFIG, value);
}
bool INA228::setAccumulation(uint8_t value)
{
if (value > 1) return false;
uint16_t reg = _readRegister(INA228_CONFIG, 2);
reg &= ~0x4000;
if (value == 1) reg |= 0x4000;
_writeRegister(INA228_CONFIG, reg);
return true;
}
bool INA228::getAccumulation()
{
uint16_t value = _readRegister(INA228_CONFIG, 2);
return (value & 0x4000) > 0;
}
void INA228::setConversionDelay(uint8_t steps)
{
uint16_t value = _readRegister(INA228_CONFIG, 2);
value &= ~0x3FC0;
value |= (steps << 6);
_writeRegister(INA228_CONFIG, value);
}
uint8_t INA228::getConversionDelay()
{
uint16_t value = _readRegister(INA228_CONFIG, 2);
return (value >> 6) & 0xFF;
}
void INA228::setTemperatureCompensation(bool on)
{
uint16_t value = _readRegister(INA228_CONFIG, 2);
value &= ~0x0020;
if (on) value |= 0x0020;
_writeRegister(INA228_CONFIG, value);
}
bool INA228::getTemperatureCompensation()
{
uint16_t value = _readRegister(INA228_CONFIG, 2);
return value & 0x0020;
}
void INA228::setADCRange(bool flag)
{
uint16_t value = _readRegister(INA228_CONFIG, 2);
value &= ~0x0010;
if (flag) value |= 0x0010;
_writeRegister(INA228_CONFIG, value);
}
bool INA228::getADCRange()
{
uint16_t value = _readRegister(INA228_CONFIG, 2);
return value & 0x0010;
}
////////////////////////////////////////////////////////
//
// CONFIG ADC REGISTER 1
//
bool INA228::setMode(uint8_t mode)
{
if (mode > 0x0F) return false;
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
value &= 0x0FFF; // ~(0x0F << 12);
value |= (mode << 12);
_writeRegister(INA228_ADC_CONFIG, value);
return true;
}
uint8_t INA228::getMode()
{
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
return (value >> 12) & 0x0F;
}
bool INA228::setBusVoltageConversionTime(uint8_t bvct)
{
if (bvct > 7) return false;
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
value &= 0xF1FF; // ~(0x07 << 9);
value |= (bvct << 9);
_writeRegister(INA228_ADC_CONFIG, value);
return true;
}
uint8_t INA228::getBusVoltageConversionTime()
{
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
return (value >> 9) & 0x07;
}
bool INA228::setShuntVoltageConversionTime(uint8_t svct)
{
if (svct > 7) return false;
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
value &= 0xFE3F; // ~(0x07 << 6);
value |= (svct << 6);
_writeRegister(INA228_ADC_CONFIG, value);
return true;
}
uint8_t INA228::getShuntVoltageConversionTime()
{
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
return (value >> 6) & 0x07;
}
bool INA228::setTemperatureConversionTime(uint8_t tct)
{
if (tct > 7) return false;
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
value &= 0xFFC7; // ~(0x07 << 3);
value |= (tct << 3);
_writeRegister(INA228_ADC_CONFIG, value);
return true;
}
uint8_t INA228::getTemperatureConversionTime()
{
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
return (value >> 3) & 0x07;
}
bool INA228::setAverage(uint8_t avg)
{
if (avg > 7) return false;
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
value &= 0xF1FF;
value |= avg;
_writeRegister(INA228_ADC_CONFIG, value);
return true;
}
uint8_t INA228::getAverage()
{
uint16_t value = _readRegister(INA228_ADC_CONFIG, 2);
return value & 0x0010;
}
////////////////////////////////////////////////////////
//
// SHUNT CALIBRATION REGISTER 2
//
int INA228::setMaxCurrentShunt(float maxCurrent, float shunt)
{
if (maxCurrent > 10) return -1;
if (shunt < 0.005) return -2;
_maxCurrent = maxCurrent;
_shunt = shunt;
_current_LSB = _maxCurrent * 1.9073486328125e-6; // pow(2, -19);
return 0;
}
float INA228::getMaxCurrent()
{
return _maxCurrent;
}
float INA228::getShunt()
{
return _shunt;
}
////////////////////////////////////////////////////////
//
// SHUNT TEMPERATURE COEFFICIENT REGISTER 3
//
bool INA228::setShuntTemperatureCoefficent(uint16_t ppm)
{
if (ppm > 16383) return false;
_writeRegister(INA228_SHUNT_TEMP_CO, ppm);
return true;
}
uint16_t INA228::getShuntTemperatureCoefficent()
{
uint16_t value = _readRegister(INA228_SHUNT_TEMP_CO, 2);
return value;
}
////////////////////////////////////////////////////////
//
// DIAGNOSE ALERT REGISTER 11
//
void INA228::setDiagnoseAlert(uint16_t flags)
{
_writeRegister(INA228_DIAG_ALERT, flags);
}
uint16_t INA228::getDiagnoseAlert()
{
return _readRegister(INA228_DIAG_ALERT, 2);
}
// INA228.h has an enum for the bit fields.
void INA228::setDiagnoseAlertBit(uint8_t bit)
{
uint16_t value = _readRegister(INA228_DIAG_ALERT, 2);
value |= (1 << bit);
_writeRegister(INA228_DIAG_ALERT, value);
}
void INA228::clrDiagnoseAlertBit(uint8_t bit)
{
uint16_t value = _readRegister(INA228_DIAG_ALERT, 2);
value &= ~(1 << bit);
_writeRegister(INA228_DIAG_ALERT, value);
}
uint16_t INA228::getDiagnoseAlertBit(uint8_t bit)
{
uint16_t value = _readRegister(INA228_DIAG_ALERT, 2);
return (value >> bit) & 0x01;
}
////////////////////////////////////////////////////////
//
// THRESHOLD AND LIMIT REGISTERS 12-17
//
void INA228::setShuntOvervoltageTH(uint16_t threshold)
{
_writeRegister(INA228_SOVL, threshold);
}
uint16_t INA228::getShuntOvervoltageTH()
{
return _readRegister(INA228_SOVL, 2);
}
void INA228::setShuntUndervoltageTH(uint16_t threshold)
{
_writeRegister(INA228_SUVL, threshold);
}
uint16_t INA228::getShuntUndervoltageTH()
{
return _readRegister(INA228_SUVL, 2);
}
void INA228::setBusOvervoltageTH(uint16_t threshold)
{
if (threshold > 0x7FFF) return;
_writeRegister(INA228_BOVL, threshold);
}
uint16_t INA228::getBusOvervoltageTH()
{
return _readRegister(INA228_BOVL, 2);
}
void INA228::setBusUndervoltageTH(uint16_t threshold)
{
if (threshold > 0x7FFF) return;
_writeRegister(INA228_BUVL, threshold);
}
uint16_t INA228::getBusUndervoltageTH()
{
return _readRegister(INA228_BUVL, 2);
}
void INA228::setTemperatureOverLimitTH(uint16_t threshold)
{
_writeRegister(INA228_TEMP_LIMIT, threshold);
}
uint16_t INA228::getTemperatureOverLimitTH()
{
return _readRegister(INA228_TEMP_LIMIT, 2);
}
void INA228::setPowerOverLimitTH(uint16_t threshold)
{
_writeRegister(INA228_POWER_LIMIT, threshold);
}
uint16_t INA228::getPowerOverLimitTH()
{
return _readRegister(INA228_POWER_LIMIT, 2);
}
////////////////////////////////////////////////////////
//
// MANUFACTURER and ID REGISTER 3E/3F
//
bool INA228::getManufacturer()
{
uint16_t value = _readRegister(INA228_MANUFACTURER, 2);
return value;
}
uint16_t INA228::getDieID()
{
uint16_t value = _readRegister(INA228_DEVICE_ID, 2);
return (value >> 4) & 0xFFF0;
}
uint16_t INA228::getRevision()
{
uint16_t value = _readRegister(INA228_DEVICE_ID, 2);
return value & 0x0F;
}
////////////////////////////////////////////////////////
//
// SHOULD BE PRIVATE
//
uint32_t INA228::_readRegister(uint8_t reg, uint8_t bytes)
{
_wire->beginTransmission(_address);
_wire->write(reg);
_wire->endTransmission();
_wire->requestFrom(_address, (uint8_t)bytes);
uint32_t value = 0;
for ( int i = 0; i < bytes; i++)
{
value <<= 8;
value |= _wire->read();
}
return value;
}
float INA228::_readRegisterF(uint8_t reg, uint8_t bytes)
{
_wire->beginTransmission(_address);
_wire->write(reg);
_wire->endTransmission();
_wire->requestFrom(_address, (uint8_t)bytes);
float value = 0;
for ( int i = 0; i < bytes; i++)
{
value *= 256.0;
value += _wire->read();
}
return value;
}
uint16_t INA228::_writeRegister(uint8_t reg, uint16_t value)
{
_wire->beginTransmission(_address);
_wire->write(reg);
_wire->write(value >> 8);
_wire->write(value & 0xFF);
return _wire->endTransmission();
}
// -- END OF FILE --