From b09eb07ae038b1950664a4c4203ae9ac60e2ed2c Mon Sep 17 00:00:00 2001 From: Bob Moss Date: Mon, 1 Dec 2025 13:36:23 +0000 Subject: [PATCH 1/5] Added functiosn to set bus and shunt resolution and averaging --- Adafruit_INA219.cpp | 34 ++++++++++++++++++++++++++++++++++ Adafruit_INA219.h | 2 ++ 2 files changed, 36 insertions(+) diff --git a/Adafruit_INA219.cpp b/Adafruit_INA219.cpp index 47f480c..da8fdc6 100644 --- a/Adafruit_INA219.cpp +++ b/Adafruit_INA219.cpp @@ -290,6 +290,40 @@ void Adafruit_INA219::powerSave(bool on) { } } +/*! + * @brief Set Bus ADC resolution and averaging options + * @param resolution + * resolution option enum + */ +void Adafruit_INA219::setBusADCResolution(uint16_t resolution) { + // See the INA219_CONFIG_BADCRES enums for the various options + // The default set by setCalibration functions is NA219_CONFIG_BADCRES_12BIT + + 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, 4, 3); + _success = mode_bits.write(resolution); +} + +/*! + * @brief Set Shunt ADC resolution and averaging options + * @param resolution + * resolution option enum + */ +void Adafruit_INA219::setShuntADCResolution(uint16_t resolution) { + // See the INA219_CONFIG_SADCRES enums for the various options + // The default set by setCalibration functions is INA219_CONFIG_SADCRES_12BIT_1S_532US + + 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, 4, 7); + _success = mode_bits.write(resolution); +} + /*! * @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 diff --git a/Adafruit_INA219.h b/Adafruit_INA219.h index 6b982d5..fa3e7e5 100644 --- a/Adafruit_INA219.h +++ b/Adafruit_INA219.h @@ -174,6 +174,8 @@ class Adafruit_INA219 { float getCurrent_mA(); float getPower_mW(); void powerSave(bool on); + void setBusADCResolution(uint16_t resolution) + void setShuntADCResolution(uint16_t resolution) bool success(); private: From 51d7695205aeeef5ae7a683a342a666254d5b6a9 Mon Sep 17 00:00:00 2001 From: Bob Moss Date: Mon, 1 Dec 2025 15:02:54 +0000 Subject: [PATCH 2/5] Changed bit masking --- Adafruit_INA219.cpp | 16 ++++++++++------ Adafruit_INA219.h | 4 ++-- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/Adafruit_INA219.cpp b/Adafruit_INA219.cpp index da8fdc6..df6279c 100644 --- a/Adafruit_INA219.cpp +++ b/Adafruit_INA219.cpp @@ -302,9 +302,11 @@ void Adafruit_INA219::setBusADCResolution(uint16_t resolution) { 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, 4, 3); - _success = mode_bits.write(resolution); + uint32_t val = config_reg.read(); + // Mask off area and set our new values + val &= ~INA219_CONFIG_BADCRES_MASK; + val |= resolution; + _success = config_reg.write(val, 2); } /*! @@ -319,9 +321,11 @@ void Adafruit_INA219::setShuntADCResolution(uint16_t resolution) { 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, 4, 7); - _success = mode_bits.write(resolution); + uint32_t val = config_reg.read(); + // Mask off area and set our new values + val &= ~INA219_CONFIG_SADCRES_MASK; + val |= resolution; + _success = config_reg.write(val, 2); } /*! diff --git a/Adafruit_INA219.h b/Adafruit_INA219.h index fa3e7e5..cec3edf 100644 --- a/Adafruit_INA219.h +++ b/Adafruit_INA219.h @@ -174,8 +174,8 @@ class Adafruit_INA219 { float getCurrent_mA(); float getPower_mW(); void powerSave(bool on); - void setBusADCResolution(uint16_t resolution) - void setShuntADCResolution(uint16_t resolution) + void setBusADCResolution(uint16_t resolution); + void setShuntADCResolution(uint16_t resolution); bool success(); private: From 424bf12a0d80c13fa023358f1ff9f4b17a13d871 Mon Sep 17 00:00:00 2001 From: Bob Moss Date: Mon, 1 Dec 2025 19:27:45 +0000 Subject: [PATCH 3/5] Fixed formatting for clang --- Adafruit_INA219.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Adafruit_INA219.h b/Adafruit_INA219.h index cec3edf..565ef9a 100644 --- a/Adafruit_INA219.h +++ b/Adafruit_INA219.h @@ -174,8 +174,8 @@ class Adafruit_INA219 { float getCurrent_mA(); float getPower_mW(); void powerSave(bool on); - void setBusADCResolution(uint16_t resolution); - void setShuntADCResolution(uint16_t resolution); + void setBusADCResolution(uint16_t resolution); + void setShuntADCResolution(uint16_t resolution); bool success(); private: From e2b70c350c7934c151bad050dcda258dcc41eef1 Mon Sep 17 00:00:00 2001 From: Bob Moss Date: Mon, 1 Dec 2025 19:35:46 +0000 Subject: [PATCH 4/5] Fixed formatting for clang --- Adafruit_INA219.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Adafruit_INA219.cpp b/Adafruit_INA219.cpp index df6279c..c2fe706 100644 --- a/Adafruit_INA219.cpp +++ b/Adafruit_INA219.cpp @@ -297,7 +297,7 @@ void Adafruit_INA219::powerSave(bool on) { */ void Adafruit_INA219::setBusADCResolution(uint16_t resolution) { // See the INA219_CONFIG_BADCRES enums for the various options - // The default set by setCalibration functions is NA219_CONFIG_BADCRES_12BIT + // The default set by setCalibration functions is NA219_CONFIG_BADCRES_12BIT Adafruit_BusIO_Register config_reg = Adafruit_BusIO_Register(i2c_dev, INA219_REG_CONFIG, 2, MSBFIRST); @@ -316,7 +316,8 @@ void Adafruit_INA219::setBusADCResolution(uint16_t resolution) { */ void Adafruit_INA219::setShuntADCResolution(uint16_t resolution) { // See the INA219_CONFIG_SADCRES enums for the various options - // The default set by setCalibration functions is INA219_CONFIG_SADCRES_12BIT_1S_532US + // The default set by setCalibration functions is + // INA219_CONFIG_SADCRES_12BIT_1S_532US Adafruit_BusIO_Register config_reg = Adafruit_BusIO_Register(i2c_dev, INA219_REG_CONFIG, 2, MSBFIRST); From c4bea2793952703c462fbebfeeea2ef9dccedc86 Mon Sep 17 00:00:00 2001 From: Bob Moss Date: Mon, 1 Dec 2025 19:49:26 +0000 Subject: [PATCH 5/5] Fixed more formatting for clang --- Adafruit_INA219.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Adafruit_INA219.cpp b/Adafruit_INA219.cpp index c2fe706..5d61056 100644 --- a/Adafruit_INA219.cpp +++ b/Adafruit_INA219.cpp @@ -298,7 +298,7 @@ void Adafruit_INA219::powerSave(bool on) { void Adafruit_INA219::setBusADCResolution(uint16_t resolution) { // See the INA219_CONFIG_BADCRES enums for the various options // The default set by setCalibration functions is NA219_CONFIG_BADCRES_12BIT - + Adafruit_BusIO_Register config_reg = Adafruit_BusIO_Register(i2c_dev, INA219_REG_CONFIG, 2, MSBFIRST); @@ -318,7 +318,7 @@ void Adafruit_INA219::setShuntADCResolution(uint16_t resolution) { // See the INA219_CONFIG_SADCRES enums for the various options // The default set by setCalibration functions is // INA219_CONFIG_SADCRES_12BIT_1S_532US - + Adafruit_BusIO_Register config_reg = Adafruit_BusIO_Register(i2c_dev, INA219_REG_CONFIG, 2, MSBFIRST);