Skip to content

Commit b74bd66

Browse files
committed
0.1.1 INA229
1 parent a5ad481 commit b74bd66

File tree

10 files changed

+281
-52
lines changed

10 files changed

+281
-52
lines changed

libraries/INA229/CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88

9+
## [0.1.1] - 2025-01-30
10+
- fix #3, support for **int32 getShuntVoltageRAW()**
11+
- cache ADCrange to improve **getShuntVoltage()**
12+
- minor edits
13+
914
## [0.1.0] - 2025-01-22
1015
- initial version,
1116
- based upon INA228 (API) + INA239 (SPI)

libraries/INA229/INA229.cpp

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// FILE: INA229.cpp
22
// AUTHOR: Rob Tillaart
3-
// VERSION: 0.1.0
3+
// VERSION: 0.1.1
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
@@ -104,6 +104,8 @@ bool INA229::begin()
104104
digitalWrite(_dataOut, LOW);
105105
digitalWrite(_clock, LOW);
106106
}
107+
108+
getADCRange();
107109
return true;
108110
}
109111

@@ -127,7 +129,7 @@ float INA229::getShuntVoltage()
127129
{
128130
// shunt_LSB depends on ADCRANGE in INA229_CONFIG register.
129131
float shunt_LSB = 312.5e-9; // 312.5 nV
130-
if (getADCRange() == 1)
132+
if (_ADCRange == true)
131133
{
132134
shunt_LSB = 78.125e-9; // 78.125 nV
133135
}
@@ -143,6 +145,18 @@ float INA229::getShuntVoltage()
143145
return voltage;
144146
}
145147

148+
int32_t INA229::getShuntVoltageRAW()
149+
{
150+
// remove reserved bits.
151+
uint32_t value = _readRegister(INA229_SHUNT_VOLTAGE, 3) >> 4;
152+
// handle negative values (20 bit)
153+
if (value & 0x00080000)
154+
{
155+
value |= 0xFFF00000;
156+
}
157+
return (int32_t)value;
158+
}
159+
146160
// PAGE 24 + 8.1.2
147161
float INA229::getCurrent()
148162
{
@@ -176,7 +190,7 @@ float INA229::getTemperature()
176190
// PAGE 24 + 8.1.2
177191
double INA229::getEnergy()
178192
{
179-
// read 40 bit unsigned as a double to prevent 64 bit ints
193+
// read 40 bit unsigned as a double to prevent 64 bit integers
180194
// double might be 8 or 4 byte, depends on platform
181195
// 40 bit ==> O(10^12)
182196
double value = _readRegisterF(INA229_ENERGY, 5);
@@ -188,7 +202,7 @@ double INA229::getEnergy()
188202
// PAGE 24 + 8.1.2
189203
double INA229::getCharge()
190204
{
191-
// read 40 bit unsigned as a float to prevent 64 bit ints
205+
// read 40 bit unsigned as a float to prevent 64 bit integers
192206
// double might be 8 or 4 byte, depends on platform
193207
// 40 bit ==> O(10^12)
194208
double value = _readRegisterF(INA229_CHARGE, 5);
@@ -213,8 +227,8 @@ bool INA229::setAccumulation(uint8_t value)
213227
{
214228
if (value > 1) return false;
215229
uint16_t reg = _readRegister(INA229_CONFIG, 2);
216-
reg &= ~INA229_CFG_RSTACC;
217230
if (value == 1) reg |= INA229_CFG_RSTACC;
231+
else reg &= ~INA229_CFG_RSTACC;
218232
_writeRegister(INA229_CONFIG, reg);
219233
return true;
220234
}
@@ -243,8 +257,8 @@ uint8_t INA229::getConversionDelay()
243257
void INA229::setTemperatureCompensation(bool on)
244258
{
245259
uint16_t value = _readRegister(INA229_CONFIG, 2);
246-
value &= ~INA229_CFG_TEMPCOMP;
247260
if (on) value |= INA229_CFG_TEMPCOMP;
261+
else value &= ~INA229_CFG_TEMPCOMP;
248262
_writeRegister(INA229_CONFIG, value);
249263
}
250264

@@ -257,16 +271,20 @@ bool INA229::getTemperatureCompensation()
257271
// PAGE 20
258272
void INA229::setADCRange(bool flag)
259273
{
274+
// if (flag == _ADCRange) return;
275+
_ADCRange = flag;
260276
uint16_t value = _readRegister(INA229_CONFIG, 2);
261-
value &= ~INA229_CFG_ADCRANGE;
262277
if (flag) value |= INA229_CFG_ADCRANGE;
278+
else value &= ~INA229_CFG_ADCRANGE;
279+
// if value has not changed we do not need to write it back.
263280
_writeRegister(INA229_CONFIG, value);
264281
}
265282

266283
bool INA229::getADCRange()
267284
{
268285
uint16_t value = _readRegister(INA229_CONFIG, 2);
269-
return (value & INA229_CFG_ADCRANGE) > 0;
286+
_ADCRange = (value & INA229_CFG_ADCRANGE) > 0;
287+
return _ADCRange;
270288
}
271289

272290

@@ -372,7 +390,7 @@ int INA229::setMaxCurrentShunt(float maxCurrent, float shunt)
372390
// PAGE 31 (8.1.2)
373391
float shunt_cal = 13107.2e6 * _current_LSB * _shunt;
374392
// depends on ADCRANGE in INA229_CONFIG register.
375-
if (getADCRange() == 1)
393+
if (_ADCRange == true)
376394
{
377395
shunt_cal *= 4;
378396
}
@@ -598,7 +616,7 @@ bool INA229::usesHWSPI()
598616

599617
////////////////////////////////////////////////////////
600618
//
601-
// SHOULD BE PROTECTED
619+
// PRIVATE
602620
//
603621
uint32_t INA229::_readRegister(uint8_t reg, uint8_t bytes) // bytes = 2 or 3.
604622
{
@@ -692,11 +710,6 @@ uint16_t INA229::_writeRegister(uint8_t reg, uint16_t value)
692710
}
693711

694712

695-
/////////////////////////////////////////////////////////////////////////////
696-
//
697-
// PROTECTED
698-
//
699-
700713
uint8_t INA229::swSPI_transfer(uint8_t value)
701714
{
702715
uint8_t rv = 0;

libraries/INA229/INA229.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#pragma once
22
// FILE: INA229.h
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.0
4+
// VERSION: 0.1.1
55
// DATE: 2025-01-22
66
// PURPOSE: Arduino library for the INA229, SPI, 20 bit, voltage, current and power sensor.
77
// URL: https://github.com/RobTillaart/INA229
@@ -13,7 +13,7 @@
1313
#include "SPI.h"
1414

1515

16-
#define INA229_LIB_VERSION (F("0.1.0"))
16+
#define INA229_LIB_VERSION (F("0.1.1"))
1717

1818

1919
#ifndef __SPI_CLASS__
@@ -123,6 +123,10 @@ class INA229
123123
float getShuntMilliVolt() { return getShuntVoltage() * 1e3; };
124124
float getShuntMicroVolt() { return getShuntVoltage() * 1e6; };
125125

126+
// raw integer interface.
127+
int32_t getShuntVoltageRAW();
128+
129+
126130
// SHUNT CURRENT
127131
float getCurrent(); // Ampere
128132
float getAmpere() { return getCurrent(); };
@@ -155,6 +159,7 @@ class INA229
155159
double getCoulomb() { return getCharge(); };
156160
double getMilliCoulomb() { return getCharge() * 1e3; };
157161
double getMicroCoulomb() { return getCharge() * 1e6; };
162+
158163
//
159164
// CONFIG REGISTER 0
160165
// read datasheet for details, section 7.6.1.1, page 21
@@ -267,6 +272,7 @@ class INA229
267272
float _current_LSB;
268273
float _shunt;
269274
float _maxCurrent;
275+
bool _ADCRange;
270276

271277
uint8_t _dataIn = 255;
272278
uint8_t _dataOut= 255;

libraries/INA229/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ Note the value can be positive or negative as the INA229 is bidirectional.
199199
- **float getShuntVolt()**
200200
- **float getShuntMilliVolt()**
201201
- **float getShuntMicroVolt()**
202+
- **int32_t getShuntVoltageRAW()** integer version requested in issue #3.
203+
Returns raw ADC value, 20 bits with sign extended.
202204

203205
### SHUNT CURRENT
204206

0 commit comments

Comments
 (0)