11// FILE: INA229.cpp
22// AUTHOR: Rob Tillaart
3- // VERSION: 0.3 .0
3+ // VERSION: 0.4 .0
44// DATE: 2025-01-22
55// PURPOSE: Arduino library for the INA229, SPI, 20 bit, voltage, current and power sensor.
66// URL: https://github.com/RobTillaart/INA229
@@ -190,10 +190,10 @@ float INA229::getTemperature()
190190// PAGE 24 + 8.1.2
191191double INA229::getEnergy ()
192192{
193- // read 40 bit unsigned as a double to prevent 64 bit integers
193+ // read 40 bit UNSIGNED as a double to prevent 64 bit integers
194194 // double might be 8 or 4 byte, depends on platform
195195 // 40 bit ==> O(10^12)
196- double value = _readRegisterF (INA229_ENERGY);
196+ double value = _readRegisterF (INA229_ENERGY, ' U ' );
197197 // PAGE 31 (8.1.2)
198198 return value * (16 * 3.2 ) * _current_LSB;
199199}
@@ -202,10 +202,10 @@ double INA229::getEnergy()
202202// PAGE 24 + 8.1.2
203203double INA229::getCharge ()
204204{
205- // read 40 bit unsigned as a float to prevent 64 bit integers
205+ // read 40 bit SIGNED as a float to prevent 64 bit integers
206206 // double might be 8 or 4 byte, depends on platform
207207 // 40 bit ==> O(10^12)
208- double value = _readRegisterF (INA229_CHARGE);
208+ double value = _readRegisterF (INA229_CHARGE, ' S ' );
209209 // PAGE 32 (8.1.2)
210210 return value * _current_LSB;
211211}
@@ -650,25 +650,27 @@ uint32_t INA229::_readRegister(uint8_t reg, uint8_t bytes) // bytes = 2 or 3.
650650}
651651
652652
653- double INA229::_readRegisterF (uint8_t reg)
653+ double INA229::_readRegisterF (uint8_t reg, char mode )
654654{
655655 // Dedicated SPI code
656656 double value = 0 ;
657- int32_t ival = 0 ;
658657 uint8_t addr = (reg << 2 ) + 1 ; // 1 = Read flag. P18 datasheet
659658
660659 digitalWrite (_select, LOW);
661660 if (_hwSPI)
662661 {
663662 _mySPI->beginTransaction (_spi_settings);
664- ival = _mySPI->transfer (addr);
663+ uint32_t val = _mySPI->transfer (addr);
665664 // fetch 4 MSB bytes
666665 for (int i = 0 ; i < 4 ; i++)
667666 {
668- ival <<= 8 ;
669- ival |= _mySPI->transfer (0x00 );
667+ val <<= 8 ;
668+ val |= _mySPI->transfer (0x00 );
670669 }
671- value = ival;
670+ // handle signed / unsigned by casting.
671+ if (mode == ' U' ) value = val;
672+ else value = (int32_t ) val;
673+ // process last byte
672674 value *= 256 ;
673675 // fetch last LSB byte
674676 uint8_t n = _mySPI->transfer (0x00 );
@@ -677,14 +679,17 @@ double INA229::_readRegisterF(uint8_t reg)
677679 }
678680 else // Software SPI
679681 {
680- ival = swSPI_transfer (addr);
682+ uint32_t val = swSPI_transfer (addr);
681683 // fetch 4 MSB bytes
682684 for (int i = 0 ; i < 4 ; i++)
683685 {
684- ival <<= 8 ;
685- ival |= swSPI_transfer (0x00 );
686+ val <<= 8 ;
687+ val |= swSPI_transfer (0x00 );
686688 }
687- value = ival;
689+ // handle signed / unsigned by casting.
690+ if (mode == ' U' ) value = val;
691+ else value = (int32_t ) val;
692+ // process last byte
688693 value *= 256 ;
689694 // fetch last LSB byte
690695 uint8_t n = swSPI_transfer (0x00 );
0 commit comments