11// FILE: INA239.cpp
22// AUTHOR: Rob Tillaart
3- // VERSION: 0.1.0
3+ // VERSION: 0.1.1
44// DATE: 2024-12-05
55// PURPOSE: Arduino library for the INA239, SPI, 16 bit, voltage, current and power sensor.
66// URL: https://github.com/RobTillaart/INA239
@@ -99,6 +99,8 @@ bool INA239::begin()
9999 digitalWrite (_dataOut, LOW);
100100 digitalWrite (_clock, LOW);
101101 }
102+
103+ getADCRange ();
102104 return true ;
103105}
104106
@@ -107,7 +109,7 @@ bool INA239::begin()
107109//
108110// CORE FUNCTIONS
109111//
110- // PAGE 22 DONE
112+ // PAGE 22
111113float INA239::getBusVoltage ()
112114{
113115 // always positive, remove reserved bits.
@@ -117,12 +119,12 @@ float INA239::getBusVoltage()
117119 return voltage;
118120}
119121
120- // PAGE 22 DONE
122+ // PAGE 22
121123float INA239::getShuntVoltage ()
122124{
123125 // shunt_LSB depends on ADCRANGE in INA239_CONFIG register.
124126 float shunt_LSB = 5e-6 ; // 5.0 uV/LSB
125- if (getADCRange () == 1 )
127+ if (_ADCRange == true )
126128 {
127129 shunt_LSB = 1.25e-6 ; // 1.25 uV/LSB
128130 }
@@ -138,15 +140,15 @@ float INA239::getShuntVoltage()
138140 return voltage;
139141}
140142
141- // PAGE 23 + 8.1.2 DONE
143+ // PAGE 23 + 8.1.2
142144float INA239::getCurrent ()
143145{
144146 int16_t value = _readRegister (INA239_CURRENT, 2 );
145147 float current = value * _current_LSB;
146148 return current;
147149}
148150
149- // PAGE 23 + 8.1.2 DONE
151+ // PAGE 23 + 8.1.2
150152float INA239::getPower ()
151153{
152154 uint32_t value = _readRegister (INA239_POWER, 3 );
@@ -167,15 +169,15 @@ float INA239::getTemperature()
167169//
168170// CONFIG REGISTER 0
169171//
170- // PAGE 20 DONE
172+ // PAGE 20
171173void INA239::reset ()
172174{
173175 uint16_t value = _readRegister (INA239_CONFIG, 2 );
174176 value |= INA239_CFG_RST;
175177 _writeRegister (INA239_CONFIG, value);
176178}
177179
178- // PAGE 20 DONE
180+ // PAGE 20
179181void INA239::setConversionDelay (uint8_t steps)
180182{
181183 uint16_t value = _readRegister (INA239_CONFIG, 2 );
@@ -190,27 +192,31 @@ uint8_t INA239::getConversionDelay()
190192 return (value >> 6 ) & 0xFF ;
191193}
192194
193- // PAGE 20 DONE
195+ // PAGE 20
194196void INA239::setADCRange (bool flag)
195197{
198+ // if (flag == _ADCRange) return;
199+ _ADCRange = flag;
196200 uint16_t value = _readRegister (INA239_CONFIG, 2 );
197- value &= ~INA239_CFG_ADCRANGE;
198201 if (flag) value |= INA239_CFG_ADCRANGE;
202+ else value &= ~INA239_CFG_ADCRANGE;
203+ // if value has not changed we do not need to write it back.
199204 _writeRegister (INA239_CONFIG, value);
200205}
201206
202207bool INA239::getADCRange ()
203208{
204209 uint16_t value = _readRegister (INA239_CONFIG, 2 );
205- return (value & INA239_CFG_ADCRANGE) > 0 ;
210+ _ADCRange = (value & INA239_CFG_ADCRANGE) > 0 ;
211+ return _ADCRange;
206212}
207213
208214
209215// //////////////////////////////////////////////////////
210216//
211217// CONFIG ADC REGISTER 1
212218//
213- // PAGE 21 + 22 DONE
219+ // PAGE 21 + 22
214220bool INA239::setMode (uint8_t mode)
215221{
216222 if (mode > 0x0F ) return false ;
@@ -296,19 +302,19 @@ uint8_t INA239::getAverage()
296302//
297303// SHUNT CALIBRATION REGISTER 2
298304//
299- // PAGE 28 8.1.2
305+ // PAGE 28 + 8.1.2
300306int INA239::setMaxCurrentShunt (float maxCurrent, float shunt)
301307{
302308 // Shunt can be really small
303- if (shunt < 0.0001 ) return -2 ; // error code
309+ if (shunt < 0.0001 ) return -2 ; // TODO error code
304310 _maxCurrent = maxCurrent;
305311 _shunt = shunt;
306312 _current_LSB = _maxCurrent * 3.0517578125e-5 ; // pow(2, -15);
307313
308314 // PAGE 31 (8.1.2)
309315 float shunt_cal = 819.2e6 * _current_LSB * _shunt;
310316 // depends on ADCRANGE in INA239_CONFIG register.
311- if (getADCRange () == 1 )
317+ if (_ADCRange == true )
312318 {
313319 shunt_cal *= 4 ;
314320 }
@@ -339,7 +345,7 @@ float INA239::getCurrentLSB()
339345//
340346// DIAGNOSE ALERT REGISTER 11
341347//
342- // PAGE 23 DONE
348+ // PAGE 23
343349void INA239::setDiagnoseAlert (uint16_t flags)
344350{
345351 _writeRegister (INA239_DIAG_ALERT, flags);
@@ -452,14 +458,14 @@ uint16_t INA239::getTemperatureOverLimitTH()
452458
453459void INA239::setPowerOverLimitTH (uint16_t threshold)
454460{
455- // P 26
461+ // PAGE 26
456462 // Conversion factor: 256 × Power LSB.
457463 _writeRegister (INA239_POWER_LIMIT, threshold);
458464}
459465
460466uint16_t INA239::getPowerOverLimitTH ()
461467{
462- // P 26
468+ // PAGE 26
463469 // Conversion factor: 256 × Power LSB.
464470 return _readRegister (INA239_POWER_LIMIT, 2 );
465471}
@@ -469,6 +475,7 @@ uint16_t INA239::getPowerOverLimitTH()
469475//
470476// MANUFACTURER and ID REGISTER 3E/3F
471477//
478+ // PAGE 26
472479uint16_t INA239::getManufacturer ()
473480{
474481 uint16_t value = _readRegister (INA239_MANUFACTURER, 2 );
@@ -514,7 +521,7 @@ bool INA239::usesHWSPI()
514521
515522// //////////////////////////////////////////////////////
516523//
517- // SHOULD BE PROTECTED
524+ // PRIVATE
518525//
519526uint32_t INA239::_readRegister (uint8_t reg, uint8_t bytes) // bytes = 2 or 3.
520527{
@@ -576,11 +583,6 @@ uint16_t INA239::_writeRegister(uint8_t reg, uint16_t value)
576583}
577584
578585
579- // ///////////////////////////////////////////////////////////////////////////
580- //
581- // PROTECTED
582- //
583-
584586uint8_t INA239::swSPI_transfer (uint8_t value)
585587{
586588 uint8_t rv = 0 ;
0 commit comments