forked from adafruit/Adafruit_INA219
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAdafruit_INA219.cpp
765 lines (671 loc) · 24.6 KB
/
Adafruit_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
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
/*!
* @file Adafruit_INA219.cpp
*
* @mainpage Adafruit INA219 current/power monitor IC
*
* @section intro_sec Introduction
*
* Driver for the INA219 current sensor
*
* This is a library for the Adafruit INA219 breakout
* ----> https://www.adafruit.com/product/904
*
* Adafruit invests time and resources providing this open source code,
* please support Adafruit and open-source hardware by purchasing
* products from Adafruit!
*
* @section author Author
*
* Written by Bryan Siepert and Kevin "KTOWN" Townsend for Adafruit Industries.
*
* @section license License
*
* BSD license, all text here must be included in any redistribution.
*
*/
#include "Arduino.h"
#include <Wire.h>
#include "Adafruit_INA219.h"
/*!
* @brief Instantiates a new INA219 class
* @param addr the I2C address the device can be found on. Default is 0x40
*/
Adafruit_INA219::Adafruit_INA219(uint8_t addr)
{
ina219_i2caddr = addr;
ina219_currentDivider_mA = 0;
ina219_powerMultiplier_mW = 0.0f;
}
/*!
* @brief Sets up the HW (defaults to 32V and 2A for calibration values)
* @param theWire the TwoWire object to use
* @return true: success false: Failed to start I2C
*/
bool Adafruit_INA219::begin(TwoWire *theWire)
{
i2c_dev = new Adafruit_I2CDevice(ina219_i2caddr, theWire);
if (!i2c_dev->begin())
{
return false;
}
init();
return true;
}
/*!
* @brief begin I2C and set up the hardware
*/
void Adafruit_INA219::init()
{
// Set chip to large range config values to start
setCalibration_32V_2A();
}
/*!
* @brief Gets the raw bus voltage (16-bit signed integer, so +-32767)
* @return the raw bus voltage reading
*/
int16_t Adafruit_INA219::getBusVoltage_raw()
{
uint16_t value;
Adafruit_BusIO_Register bus_voltage_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_BUSVOLTAGE, 2, MSBFIRST);
bus_voltage_reg.read(&value);
// Shift to the right 3 to drop CNVR and OVF and multiply by LSB
return (int16_t)((value >> 3) * 4);
}
/*!
* @brief Gets the raw shunt voltage (16-bit signed integer, so +-32767)
* @return the raw shunt voltage reading
*/
int16_t Adafruit_INA219::getShuntVoltage_raw()
{
uint16_t value;
Adafruit_BusIO_Register shunt_voltage_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_SHUNTVOLTAGE, 2, MSBFIRST);
shunt_voltage_reg.read(&value);
return value;
}
/*!
* @brief Gets the raw current value (16-bit signed integer, so +-32767)
* @return the raw current reading
*/
int16_t Adafruit_INA219::getCurrent_raw()
{
uint16_t value;
// Sometimes a sharp load will reset the INA219, which will
// reset the cal register, meaning CURRENT and POWER will
// not be available ... avoid this by always setting a cal
// value even if it's an unfortunate extra step
Adafruit_BusIO_Register calibration_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_CALIBRATION, 2, MSBFIRST);
calibration_reg.write(ina219_calValue, 2);
// Now we can safely read the CURRENT register!
Adafruit_BusIO_Register current_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_CURRENT, 2, MSBFIRST);
current_reg.read(&value);
return value;
}
/*!
* @brief Gets the raw power value (16-bit signed integer, so +-32767)
* @return raw power reading
*/
int16_t Adafruit_INA219::getPower_raw()
{
uint16_t value;
// Sometimes a sharp load will reset the INA219, which will
// reset the cal register, meaning CURRENT and POWER will
// not be available ... avoid this by always setting a cal
// value even if it's an unfortunate extra step
Adafruit_BusIO_Register calibration_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_CALIBRATION, 2, MSBFIRST);
calibration_reg.write(ina219_calValue, 2);
// Now we can safely read the POWER register!
Adafruit_BusIO_Register power_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_POWER, 2, MSBFIRST);
power_reg.read(&value);
return value;
}
/*!
* @brief Gets the shunt voltage in mV (so +-327mV)
* @return the shunt voltage converted to millivolts
*/
float Adafruit_INA219::getShuntVoltage_mV()
{
int16_t value;
value = getShuntVoltage_raw();
return value * 0.01;
}
/*!
* @brief Gets the shunt voltage in volts
* @return the bus voltage converted to volts
*/
float Adafruit_INA219::getBusVoltage_V()
{
int16_t value = getBusVoltage_raw();
return value * 0.001;
}
/*!
* @brief Gets the current value in mA, taking into account the
* config settings and current LSB
* @return the current reading convereted to milliamps
*/
float Adafruit_INA219::getCurrent_mA()
{
float valueDec = getCurrent_raw();
valueDec /= ina219_currentDivider_mA;
return valueDec;
}
/*!
* @brief Gets the power value in mW, taking into account the
* config settings and current LSB
* @return power reading converted to milliwatts
*/
float Adafruit_INA219::getPower_mW()
{
float valueDec = getPower_raw();
valueDec *= ina219_powerMultiplier_mW;
return valueDec;
}
/*!
* @brief Configures to INA219 to be able to measure up to 32V and 2A
* of current. Each unit of current corresponds to 100uA, and
* each unit of power corresponds to 2mW. Counter overflow
* occurs at 3.2A.
* @note These calculations assume a 0.1 ohm resistor is present
*/
void Adafruit_INA219::setCalibration_32V_2A()
{
// By default we use a pretty huge range for the input voltage,
// which probably isn't the most appropriate choice for system
// that don't use a lot of power. But all of the calculations
// are shown below if you want to change the settings. You will
// also need to change any relevant register settings, such as
// setting the VBUS_MAX to 16V instead of 32V, etc.
// VBUS_MAX = 32V (Assumes 32V, can also be set to 16V)
// VSHUNT_MAX = 0.32 (Assumes Gain 8, 320mV, can also be 0.16, 0.08,
// 0.04) RSHUNT = 0.1 (Resistor value in ohms)
// 1. Determine max possible current
// MaxPossible_I = VSHUNT_MAX / RSHUNT
// MaxPossible_I = 3.2A
// 2. Determine max expected current
// MaxExpected_I = 2.0A
// 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit)
// MinimumLSB = MaxExpected_I/32767
// MinimumLSB = 0.000061 (61uA per bit)
// MaximumLSB = MaxExpected_I/4096
// MaximumLSB = 0,000488 (488uA per bit)
// 4. Choose an LSB between the min and max values
// (Preferrably a roundish number close to MinLSB)
// CurrentLSB = 0.0001 (100uA per bit)
// 5. Compute the calibration register
// Cal = trunc (0.04096 / (Current_LSB * RSHUNT))
// Cal = 4096 (0x1000)
ina219_calValue = 4096;
// 6. Calculate the power LSB
// PowerLSB = 20 * CurrentLSB
// PowerLSB = 0.002 (2mW per bit)
// 7. Compute the maximum current and shunt voltage values before overflow
//
// Max_Current = Current_LSB * 32767
// Max_Current = 3.2767A before overflow
//
// If Max_Current > Max_Possible_I then
// Max_Current_Before_Overflow = MaxPossible_I
// Else
// Max_Current_Before_Overflow = Max_Current
// End If
//
// Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT
// Max_ShuntVoltage = 0.32V
//
// If Max_ShuntVoltage >= VSHUNT_MAX
// Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX
// Else
// Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage
// End If
// 8. Compute the Maximum Power
// MaximumPower = Max_Current_Before_Overflow * VBUS_MAX
// MaximumPower = 3.2 * 32V
// MaximumPower = 102.4W
// Set multipliers to convert raw current/power values
ina219_currentDivider_mA = 10; // Current LSB = 100uA per bit (1000/100 = 10)
ina219_powerMultiplier_mW = 2; // Power LSB = 1mW per bit (2/1)
// Set Calibration register to 'Cal' calculated above
Adafruit_BusIO_Register calibration_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_CALIBRATION, 2, MSBFIRST);
calibration_reg.write(ina219_calValue, 2);
// Set Config register to take into account the settings above
uint16_t config = INA219_CONFIG_BVOLTAGERANGE_32V |
INA219_CONFIG_GAIN_8_320MV | INA219_CONFIG_BADCRES_12BIT |
INA219_CONFIG_SADCRES_12BIT_1S_532US |
INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
Adafruit_BusIO_Register config_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_CONFIG, 2, MSBFIRST);
config_reg.write(config, 2);
}
/*!
* @brief Set power save mode according to parameters
* @param on
* boolean value
*/
void Adafruit_INA219::powerSave(bool on)
{
if (on)
{
Adafruit_BusIO_Register config_reg = Adafruit_BusIO_Register(i2c_dev, INA219_REG_CONFIG, 2, MSBFIRST);
Adafruit_BusIO_RegisterBits mode_bits = Adafruit_BusIO_RegisterBits(&config_reg, 3, 0);
mode_bits.write(INA219_CONFIG_MODE_POWERDOWN);
}
else
{
if (ina219_mode == INA219_CONFIG_MODE_POWERDOWN)
setMode(INA219_CONFIG_MODE_SANDBVOLT_TRIGGERED); // default revert
writeConfig();
}
}
/*!
* @brief Writes stored values to the config register
*/
void Adafruit_INA219::writeConfig()
{
uint16_t config = ina219_mode |
ina219_adcResShunt |
ina219_adcResBus |
ina219_gainShunt |
ina219_busVoltRange;
Adafruit_BusIO_Register config_reg = Adafruit_BusIO_Register(i2c_dev, INA219_REG_CONFIG, 2, MSBFIRST);
config_reg.write(config, 2);
}
/*!
* @brief Set trigger mode according to parameters for later config writing
* @param mode the operational mode, e.g. continous, triggered, powered down.
*/
void Adafruit_INA219::setMode(uint32_t mode)
{
ina219_mode = mode;
}
/*!
* @brief Set bus and shunt resolutions and samples for later config writing
* @param adcShunt new shunt voltage adc resolution / sample averaging to use
*/
void Adafruit_INA219::setResShunt(uint32_t adcRes)
{
ina219_adcResShunt = adcRes;
}
/*!
* @brief Set bus and shunt resolutions and samples for later config writing
* @param adcBus new bus voltage adc resolution / sample averaging to use
*/
void Adafruit_INA219::setResBus(uint32_t adcRes)
{
ina219_adcResBus = adcRes;
}
/*!
* @brief Set shunt gain for later config writing
* @param gain - shunt gain 40mV, 80mV, 160mV or 320mV.
*/
void Adafruit_INA219::setGainShunt(uint32_t gain)
{
ina219_gainShunt = gain;
}
/*!
* @brief Set bus voltage range for later config writing
* @param range - bus voltage range 16V or 32V.
*/
void Adafruit_INA219::setBusVRange(uint32_t range)
{
ina219_busVoltRange = range;
}
/*!
* @brief Set bus voltage range for later config writing
* @param range - bus voltage range 16V or 32V.
*/
void Adafruit_INA219::triggerRead(bool block)
{
// setMode(ina219_mode);
writeConfig();
if (!block)
return;
uint32_t us = getConvTimeS_us() < getConvTimeB_us() ? getConvTimeB_us() : getConvTimeS_us();
delayMicroseconds(us);
while (!isConversionReady())
delayMicroseconds(INA219_CONFIG_ADCRES_9BIT_1S); // little more...
}
/*!
* @brief Configures to INA219 to be able to measure up to 32V and 1A
* of current. Each unit of current corresponds to 40uA, and each
* unit of power corresponds to 800uW. Counter overflow occurs at
* 1.3A.
* @note These calculations assume a 0.1 ohm resistor is present
*/
void Adafruit_INA219::setCalibration_32V_1A()
{
// By default we use a pretty huge range for the input voltage,
// which probably isn't the most appropriate choice for system
// that don't use a lot of power. But all of the calculations
// are shown below if you want to change the settings. You will
// also need to change any relevant register settings, such as
// setting the VBUS_MAX to 16V instead of 32V, etc.
// VBUS_MAX = 32V (Assumes 32V, can also be set to 16V)
// VSHUNT_MAX = 0.32 (Assumes Gain 8, 320mV, can also be 0.16, 0.08, 0.04)
// RSHUNT = 0.1 (Resistor value in ohms)
// 1. Determine max possible current
// MaxPossible_I = VSHUNT_MAX / RSHUNT
// MaxPossible_I = 3.2A
// 2. Determine max expected current
// MaxExpected_I = 1.0A
// 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit)
// MinimumLSB = MaxExpected_I/32767
// MinimumLSB = 0.0000305 (30.5uA per bit)
// MaximumLSB = MaxExpected_I/4096
// MaximumLSB = 0.000244 (244uA per bit)
// 4. Choose an LSB between the min and max values
// (Preferrably a roundish number close to MinLSB)
// CurrentLSB = 0.0000400 (40uA per bit)
// 5. Compute the calibration register
// Cal = trunc (0.04096 / (Current_LSB * RSHUNT))
// Cal = 10240 (0x2800)
ina219_calValue = 10240;
// 6. Calculate the power LSB
// PowerLSB = 20 * CurrentLSB
// PowerLSB = 0.0008 (800uW per bit)
// 7. Compute the maximum current and shunt voltage values before overflow
//
// Max_Current = Current_LSB * 32767
// Max_Current = 1.31068A before overflow
//
// If Max_Current > Max_Possible_I then
// Max_Current_Before_Overflow = MaxPossible_I
// Else
// Max_Current_Before_Overflow = Max_Current
// End If
//
// ... In this case, we're good though since Max_Current is less than
// MaxPossible_I
//
// Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT
// Max_ShuntVoltage = 0.131068V
//
// If Max_ShuntVoltage >= VSHUNT_MAX
// Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX
// Else
// Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage
// End If
// 8. Compute the Maximum Power
// MaximumPower = Max_Current_Before_Overflow * VBUS_MAX
// MaximumPower = 1.31068 * 32V
// MaximumPower = 41.94176W
// Set multipliers to convert raw current/power values
ina219_currentDivider_mA = 25; // Current LSB = 40uA per bit (1000/40 = 25)
ina219_powerMultiplier_mW = 0.8f; // Power LSB = 800uW per bit
// Set Calibration register to 'Cal' calculated above
Adafruit_BusIO_Register calibration_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_CALIBRATION, 2, MSBFIRST);
calibration_reg.write(ina219_calValue, 2);
// Set Config register to take into account the settings above
uint16_t config = INA219_CONFIG_BVOLTAGERANGE_32V |
INA219_CONFIG_GAIN_8_320MV |
INA219_CONFIG_BADCRES_12BIT |
INA219_CONFIG_SADCRES_12BIT_1S_532US |
INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
Adafruit_BusIO_Register config_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_CONFIG, 2, MSBFIRST);
config_reg.write(config, 2);
}
/*!
* @brief set device to alibration which uses the highest precision for
* current measurement (0.1mA), at the expense of
* only supporting 16V at 400mA max.
*/
void Adafruit_INA219::setCalibration_16V_400mA()
{
// Calibration which uses the highest precision for
// current measurement (0.1mA), at the expense of
// only supporting 16V at 400mA max.
// VBUS_MAX = 16V
// VSHUNT_MAX = 0.04 (Assumes Gain 1, 40mV)
// RSHUNT = 0.1 (Resistor value in ohms)
// 1. Determine max possible current
// MaxPossible_I = VSHUNT_MAX / RSHUNT
// MaxPossible_I = 0.4A
// 2. Determine max expected current
// MaxExpected_I = 0.4A
// 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit)
// MinimumLSB = MaxExpected_I/32767
// MinimumLSB = 0.0000122 (12uA per bit)
// MaximumLSB = MaxExpected_I/4096
// MaximumLSB = 0.0000977 (98uA per bit)
// 4. Choose an LSB between the min and max values
// (Preferrably a roundish number close to MinLSB)
// CurrentLSB = 0.00005 (50uA per bit)
// 5. Compute the calibration register
// Cal = trunc (0.04096 / (Current_LSB * RSHUNT))
// Cal = 8192 (0x2000)
ina219_calValue = 8192;
// 6. Calculate the power LSB
// PowerLSB = 20 * CurrentLSB
// PowerLSB = 0.001 (1mW per bit)
// 7. Compute the maximum current and shunt voltage values before overflow
//
// Max_Current = Current_LSB * 32767
// Max_Current = 1.63835A before overflow
//
// If Max_Current > Max_Possible_I then
// Max_Current_Before_Overflow = MaxPossible_I
// Else
// Max_Current_Before_Overflow = Max_Current
// End If
//
// Max_Current_Before_Overflow = MaxPossible_I
// Max_Current_Before_Overflow = 0.4
//
// Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT
// Max_ShuntVoltage = 0.04V
//
// If Max_ShuntVoltage >= VSHUNT_MAX
// Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX
// Else
// Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage
// End If
//
// Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX
// Max_ShuntVoltage_Before_Overflow = 0.04V
// 8. Compute the Maximum Power
// MaximumPower = Max_Current_Before_Overflow * VBUS_MAX
// MaximumPower = 0.4 * 16V
// MaximumPower = 6.4W
// Set multipliers to convert raw current/power values
ina219_currentDivider_mA = 20; // Current LSB = 50uA per bit (1000/50 = 20)
ina219_powerMultiplier_mW = 1.0f; // Power LSB = 1mW per bit
// Set Calibration register to 'Cal' calculated above
Adafruit_BusIO_Register calibration_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_CALIBRATION, 2, MSBFIRST);
calibration_reg.write(ina219_calValue, 2);
// Set Config register to take into account the settings above
uint16_t config = INA219_CONFIG_BVOLTAGERANGE_16V |
INA219_CONFIG_GAIN_1_40MV |
INA219_CONFIG_BADCRES_12BIT |
INA219_CONFIG_SADCRES_12BIT_1S_532US |
INA219_CONFIG_MODE_SANDBVOLT_CONTINUOUS;
Adafruit_BusIO_Register config_reg =
Adafruit_BusIO_Register(i2c_dev, INA219_REG_CONFIG, 2, MSBFIRST);
config_reg.write(config, 2);
}
/*!
* @brief Configures to INA219 to be able to measure up to 16V and 40mA
* of current. Each unit of current corresponds to 1uA, and each
* unit of power corresponds to 20uW. Counter overflow occurs at
* 32.767mA.
* @note These calculations assume a 1.0 ohm resistor is present
*/
void Adafruit_INA219::setCalibration_16V_40mA()
{
// Calibration which uses the highest precision for
// current measurement (0.01mA), at the expense of
// only supporting 16V at 40mA max.
// VBUS_MAX = 16V
// VSHUNT_MAX = 0.04 (Assumes Gain 1, 40mV)
// RSHUNT = 1 (Resistor value in ohms)
// 1. Determine max possible current
// MaxPossible_I = VSHUNT_MAX / RSHUNT
// MaxPossible_I = 0.04A
// 2. Determine max expected current
// MaxExpected_I = 0.02A
// 3. Calculate possible range of LSBs (Min = 15-bit, Max = 12-bit)
// MinimumLSB = MaxExpected_I/32767
// MinimumLSB = 0.0000006104 (0.61uA per bit)
// MaximumLSB = MaxExpected_I/4096
// MaximumLSB = 0.00000488281 (4.88uA per bit)
// 4. Choose an LSB between the min and max values
// (Preferrably a roundish number close to MinLSB)
// CurrentLSB = 0.000001 (1uA per bit)
// 5. Compute the calibration register
// Cal = trunc (0.04096 / (CurrentLSB * RSHUNT))
// Cal = 40960 (0x2000)
ina219_calValue = 40960;
// 6. Calculate the power LSB
// PowerLSB = 20 * CurrentLSB
// PowerLSB = 0.00002 (0.02mW per bit)
// 7. Compute the maximum current and shunt voltage values before overflow
//
// Max_Current = CurrentLSB * 32767
// Max_Current = 0.032767A before overflow
//
// If Max_Current > Max_Possible_I then
// Max_Current_Before_Overflow = MaxPossible_I
// Else
// Max_Current_Before_Overflow = Max_Current
// End If
//
// Max_Current_Before_Overflow = Max_Current
// Max_Current_Before_Overflow = 0.032767A
//
// Max_ShuntVoltage = Max_Current_Before_Overflow * RSHUNT
// Max_ShuntVoltage = 0.032767V
//
// If Max_ShuntVoltage >= VSHUNT_MAX
// Max_ShuntVoltage_Before_Overflow = VSHUNT_MAX
// Else
// Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage
// End If
//
// Max_ShuntVoltage_Before_Overflow = Max_ShuntVoltage
// Max_ShuntVoltage_Before_Overflow = 0.032767V
// 8. Compute the Maximum Power
// MaximumPower = Max_Current_Before_Overflow * VBUS_MAX
// MaximumPower = 0.032767A * 16V
// MaximumPower = 0.524W
// Set multipliers to convert raw current/power values
ina219_currentDivider_mA = 1000; // Current LSB = 1uA per bit (1000/1uA = 1000)
ina219_powerMultiplier_mW = 0.02f; // Power LSB = 1mW per bit (0.02mW)
// Set Calibration register to 'Cal' calculated above
Adafruit_BusIO_Register calibration_reg = Adafruit_BusIO_Register(i2c_dev, INA219_REG_CALIBRATION, 2, MSBFIRST);
calibration_reg.write(ina219_calValue, 2);
// Set Config register to take into account the settings above
setBusVRange(INA219_CONFIG_BVOLTAGERANGE_16V);
setGainShunt(INA219_CONFIG_GAIN_1_40MV);
writeConfig();
}
/*!
* @brief Retrieves the conversion ready bit from the Bus Voltage
* register, indicating that data from a conversion in available in the
* output registers.
* @note The CNVR bit is set after all conversions, averaging,
* and multiplications are complete. It will clear when:
* 1) Writing a new mode into the Operating Mode bits in the
* Configuration Register (except for Power-Down or Disable)
* 2) Reading the Power Register
*/
bool Adafruit_INA219::isConversionReady()
{
uint16_t value;
Adafruit_BusIO_Register bus_voltage_reg = Adafruit_BusIO_Register(i2c_dev, INA219_REG_BUSVOLTAGE, 2, MSBFIRST);
bus_voltage_reg.read(&value);
// Shift to the right 3 to drop CNVR and OVF and multiply by LSB
return (bool)(value & INA219_CNVR_MASK);
}
/*!
* @brief This Look up table returns the conversion time in us for
* a given Bus Voltage operating mode.
*/
uint32_t Adafruit_INA219::getConvTimeB_us()
{
switch (ina219_adcResBus)
{
case INA219_CONFIG_BADCRES_9BIT:
return INA219_CONFIG_ADCRES_9BIT_1S;
break;
case INA219_CONFIG_BADCRES_10BIT:
return INA219_CONFIG_ADCRES_10BIT_1S;
break;
case INA219_CONFIG_BADCRES_11BIT:
return INA219_CONFIG_ADCRES_11BIT_1S;
break;
case INA219_CONFIG_BADCRES_12BIT:
return INA219_CONFIG_ADCRES_12BIT_1S;
break;
case INA219_CONFIG_BADCRES_12BIT_2S_1060US:
return INA219_CONFIG_ADCRES_12BIT_2S;
break;
case INA219_CONFIG_BADCRES_12BIT_4S_2130US:
return INA219_CONFIG_ADCRES_12BIT_4S;
break;
case INA219_CONFIG_BADCRES_12BIT_8S_4260US:
return INA219_CONFIG_ADCRES_12BIT_8S;
break;
case INA219_CONFIG_BADCRES_12BIT_16S_8510US:
return INA219_CONFIG_ADCRES_12BIT_16S;
break;
case INA219_CONFIG_BADCRES_12BIT_32S_17MS:
return INA219_CONFIG_ADCRES_12BIT_32S;
break;
case INA219_CONFIG_BADCRES_12BIT_64S_34MS:
return INA219_CONFIG_ADCRES_12BIT_64S;
break;
case INA219_CONFIG_BADCRES_12BIT_128S_69MS:
return INA219_CONFIG_ADCRES_12BIT_128S;
break;
}
}
/*!
* @brief This Look up table returns the conversion time in us for
* a given Shunt Voltage operating mode.
*/
uint32_t Adafruit_INA219::getConvTimeS_us()
{
switch (ina219_adcResBus)
{
case INA219_CONFIG_SADCRES_9BIT_1S_84US:
return INA219_CONFIG_ADCRES_9BIT_1S;
break;
case INA219_CONFIG_SADCRES_10BIT_1S_148US:
return INA219_CONFIG_ADCRES_10BIT_1S;
break;
case INA219_CONFIG_SADCRES_11BIT_1S_276US:
return INA219_CONFIG_ADCRES_11BIT_1S;
break;
case INA219_CONFIG_SADCRES_12BIT_1S_532US:
return INA219_CONFIG_ADCRES_12BIT_1S;
break;
case INA219_CONFIG_SADCRES_12BIT_2S_1060US:
return INA219_CONFIG_ADCRES_12BIT_2S;
break;
case INA219_CONFIG_SADCRES_12BIT_4S_2130US:
return INA219_CONFIG_ADCRES_12BIT_4S;
break;
case INA219_CONFIG_SADCRES_12BIT_8S_4260US:
return INA219_CONFIG_ADCRES_12BIT_8S;
break;
case INA219_CONFIG_SADCRES_12BIT_16S_8510US:
return INA219_CONFIG_ADCRES_12BIT_16S;
break;
case INA219_CONFIG_SADCRES_12BIT_32S_17MS:
return INA219_CONFIG_ADCRES_12BIT_32S;
break;
case INA219_CONFIG_SADCRES_12BIT_64S_34MS:
return INA219_CONFIG_ADCRES_12BIT_64S;
break;
case INA219_CONFIG_SADCRES_12BIT_128S_69MS:
return INA219_CONFIG_ADCRES_12BIT_128S;
break;
}
}