From a7a1285ef357eceaa991b08dd5d0f8c28010dc4d Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Wed, 17 Jul 2024 21:55:16 +0400 Subject: [PATCH 1/2] =?UTF-8?q?=E2=9C=A8=20(INA219):=20Add=20simple=20conf?= =?UTF-8?q?iguration=20and=20data=20fetching?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- examples/INA219/ReadData/ReadData.ino | 45 +++++ src/i2cdev/ina219.h | 118 +++++++++++++ src/i2cdev/ina219.hpp | 234 ++++++++++++++++++++++++++ 3 files changed, 397 insertions(+) create mode 100644 examples/INA219/ReadData/ReadData.ino create mode 100644 src/i2cdev/ina219.h create mode 100644 src/i2cdev/ina219.hpp diff --git a/examples/INA219/ReadData/ReadData.ino b/examples/INA219/ReadData/ReadData.ino new file mode 100644 index 0000000..ea2817f --- /dev/null +++ b/examples/INA219/ReadData/ReadData.ino @@ -0,0 +1,45 @@ +#include +#include + +using namespace i2cdev; + +INA219 ina219 = INA219(); +// INA219 ina219 = INA219(0x41); +// INA219 ina219 = INA219(0x40, new ArduinoI2CDevBus(&Wire)); + +void setup() { + Serial.begin(115200); + Serial.println("I2CDevLib Example - INA219 Test"); + + if (!Wire.begin()) { + Serial.println("[E] Failed to init Wire!"); + while(true) {}; + } + + auto result = ina219.setCalibration_32V_2A(); + if (result != I2CDEV_RESULT_OK) { + Serial.printf("[E] Failed to setup INA219: %i\n", result); + while(true) {}; + } + + delay(10); +} + +float shuntVoltage, busVoltage, current, power, loadVoltage; + +void loop() { + shuntVoltage = ina219.readShuntVoltage(); + busVoltage = ina219.readBusVoltage(); + current = ina219.readCurrent(); + power = ina219.readPower(); + loadVoltage = busVoltage + (shuntVoltage / 1000); + + Serial.printf("Bus Voltage: %5.3f V\n", busVoltage); + Serial.printf("Shunt Voltage: %5.3f mV\n", shuntVoltage); + Serial.printf("Load Voltage: %5.3f V\n", loadVoltage); + Serial.printf("Current: %5.3f A\n", current); + Serial.printf("Power: %5.3f mW\n", power); + Serial.print('\n'); + + delay(2000); +} \ No newline at end of file diff --git a/src/i2cdev/ina219.h b/src/i2cdev/ina219.h new file mode 100644 index 0000000..4d76fa5 --- /dev/null +++ b/src/i2cdev/ina219.h @@ -0,0 +1,118 @@ +#ifndef __I2CDEVLIB_INA219_H__ +#define __I2CDEVLIB_INA219_H__ + +#include "i2cdevbus.h" + +#define INA219_I2CADDR_BASE (0x40) // A0 and A1 tied to GND +#define INA219_I2CADDR_GET(A0, A1) (0x40 | ((A0) != 0 ? 0x01 : 0x00) | ((A1) != 0 ? 0x04 : 0x00)) + +typedef enum ina219_reg_t { + /// Configuration Register + INA219_REG_CONFIG = (0x00), + /// Shunt Voltage Register + INA219_REG_SHUNT_VOLTAGE = (0x01), + /// Bus Voltage Register + INA219_REG_BUS_VOLTAGE = (0x02), + /// Power Register + INA219_REG_POWER = (0x03), + /// Current Register + INA219_REG_CURRENT = (0x04), + /// Calibration Register + INA219_REG_CALIBRATION = (0x05) +} ina219_reg_t; + +#define INA219_CONFIG_RESET (0x8000) // 0b1000000000000000 + +#define INA219_CONFIG_BUS_VOLTAGE_RANGE_MASK (0x2000) // 0b0010000000000000 +typedef enum ina219_bus_voltage_range_t { + /// 0-16V Range + INA219_CONFIG_BUS_VOLTAGE_RANGE_16V = (0x0000), // 0b0000000000000000 + /// 0-32V Range + INA219_CONFIG_BUS_VOLTAGE_RANGE_32V = (0x2000) // 0b0010000000000000 +} ina219_bus_voltage_range_t; + +#define INA218_CONFIG_GAIN_MASK (0x1800) // 0b0001100000000000 +typedef enum ina219_gain_t { + /// Gain 1, 40mV Range + INA219_CONFIG_GAIN_1_40MV = (0x0000), // 0b0000000000000000 + /// Gain 2, 80mV Range + INA219_CONFIG_GAIN_2_80MV = (0x0800), // 0b0000100000000000 + /// Gain 4, 160mV Range + INA219_CONFIG_GAIN_4_160MV = (0x1000), // 0b0001000000000000 + /// Gain 8, 320mV Range + INA219_CONFIG_GAIN_8_320MV = (0x1800) // 0b0001100000000000 +} ina219_gain_t; + +#define INA219_CONFIG_BUS_ADC_MASK (0x0780) // 0b0000011110000000 +typedef enum ina219_bus_adc_t { + /// 9-bit bus resolution (0-511) + INA219_CONFIG_BUS_ADC_9BIT = (0x0000), + /// 10-bit bus resolution (0-1023) + INA219_CONFIG_BUS_ADC_10BIT = (0x0080), + /// 11-bit bus resolution (0-2047) + INA219_CONFIG_BUS_ADC_11BIT = (0x0100), + /// 12-bit bus resolution (0-4095) + INA219_CONFIG_BUS_ADC_12BIT = (0x0180), + /// 2 x 12-bit bus samples averaged together + INA219_CONFIG_BUS_ADC_12BIT_2S = (0x0480), + /// 4 x 12-bit bus samples averaged together + INA219_CONFIG_BUS_ADC_12BIT_4S = (0x0500), + /// 8 x 12-bit bus samples averaged together + INA219_CONFIG_BUS_ADC_12BIT_8S = (0x0580), + /// 16 x 12-bit bus samples averaged together + INA219_CONFIG_BUS_ADC_12BIT_16S = (0x0600), + /// 32 x 12-bit bus samples averaged together + INA219_CONFIG_BUS_ADC_12BIT_32S = (0x0680), + /// 64 x 12-bit bus samples averaged together + INA219_CONFIG_BUS_ADC_12BIT_64S = (0x0700), + /// 128 x 12-bit bus samples averaged together + INA219_CONFIG_BUS_ADC_12BIT_128S = (0x0780) +} ina219_bus_adc_t; + +#define INA219_CONFIG_SHUNT_ADC_MASK (0x0078) // 0b0000001111000 +typedef enum ina219_shunt_adc_t { + /// 9-bit shunt resolution (0-511) + INA219_CONFIG_SHUNT_ADC_9BIT = (0x0000), + /// 10-bit shunt resolution (0-1023) + INA219_CONFIG_SHUNT_ADC_10BIT = (0x0008), + /// 11-bit shunt resolution (0-2047) + INA219_CONFIG_SHUNT_ADC_11BIT = (0x0010), + /// 12-bit shunt resolution (0-4095) + INA219_CONFIG_SHUNT_ADC_12BIT = (0x0018), + /// 2 x 12-bit shunt samples averaged together + INA219_CONFIG_SHUNT_ADC_12BIT_2S = (0x0048), + /// 4 x 12-bit shunt samples averaged together + INA219_CONFIG_SHUNT_ADC_12BIT_4S = (0x0050), + /// 8 x 12-bit shunt samples averaged together + INA219_CONFIG_SHUNT_ADC_12BIT_8S = (0x0058), + /// 16 x 12-bit shunt samples averaged together + INA219_CONFIG_SHUNT_ADC_12BIT_16S = (0x0060), + /// 32 x 12-bit shunt samples averaged together + INA219_CONFIG_SHUNT_ADC_12BIT_32S = (0x0068), + /// 64 x 12-bit shunt samples averaged together + INA219_CONFIG_SHUNT_ADC_12BIT_64S = (0x0070), + /// 128 x 12-bit shunt samples averaged together + INA219_CONFIG_SHUNT_ADC_12BIT_128S = (0x0078) +} ina219_shunt_adc_t; + +#define INA219_CONFIG_MODE_MASK (0x0007) // 0b0000000000000111 +typedef enum ina219_mode_t { + /// Power-Down + INA219_CONFIG_MODE_POWERDOWN = (0x0000), // 0b0000000000000000 + /// Shunt Voltage, Triggered + INA219_CONFIG_MODE_SHUNT_TRIGGERED = (0x0001), // 0b0000000000000001 + /// Bus Voltage, Triggered + INA219_CONFIG_MODE_BUS_TRIGGERED = (0x0002), // 0b0000000000000010 + /// Shunt and Bus, Triggered + INA219_CONFIG_MODE_SHUNT_BUS_TRIGGERED = (0x0003), // 0b0000000000000011 + /// ADC Off + INA219_CONFIG_MODE_ADC_OFF = (0x0004), // 0b0000000000000100 + /// Shunt Voltage, Continuous + INA219_CONFIG_MODE_SHUNT_CONTINUOUS = (0x0005), // 0b0000000000000101 + /// Bus Voltage, Continuous + INA219_CONFIG_MODE_BUS_CONTINUOUS = (0x0006), // 0b0000000000000110 + /// Shunt and Bus, Continuous + INA219_CONFIG_MODE_SHUNT_BUS_CONTINUOUS = (0x0007) // 0b0000000000000111 +} ina219_mode_t; + +#endif //__I2CDEVLIB_INA219_H__ diff --git a/src/i2cdev/ina219.hpp b/src/i2cdev/ina219.hpp new file mode 100644 index 0000000..6746e26 --- /dev/null +++ b/src/i2cdev/ina219.hpp @@ -0,0 +1,234 @@ +#ifndef __I2CDEVLIB_INA219_HPP__ +#define __I2CDEVLIB_INA219_HPP__ + +#include "i2cdev/ina219.h" +#include "i2cdevbus.hpp" + +#ifdef ARDUINO +#include +#endif + +namespace i2cdev { + class INA219 { + private: + uint8_t _addr; + I2CDevBus* _bus; + + i2cdev_result_t _status; + + uint16_t _config; + + uint32_t _calValue; + uint32_t _currentDivider_mA; + float _powerMultiplier_mW; + + public: +#ifdef I2CDEV_DEFAULT_BUS + INA219(uint8_t addr = INA219_I2CADDR_BASE, I2CDevBus* bus = &I2CDEV_DEFAULT_BUS) : _addr(addr), _bus(bus) {} +#else + INA219(uint8_t addr, I2CDevBus *bus) : _addr(addr), _bus(bus) {} +#endif + + inline auto readShuntVoltage() -> float + { + return this->readRawShuntVoltage() * 0.01f; + } + + inline auto readBusVoltage() -> float + { + return this->readRawBusVoltage() * 0.001f; + } + + inline auto readCurrent() -> float + { + return this->readRawCurrent() / static_cast(this->_currentDivider_mA); + } + + inline auto readPower() -> float + { + return this->readRawPower() * this->_powerMultiplier_mW; + } + + inline auto sleep() -> i2cdev_result_t + { + // write to register without updating internal mode, so we can wake up later + return this->_bus->writeReg16( + this->_addr, + INA219_REG_CONFIG, + static_cast(INA219_CONFIG_MODE_POWERDOWN) + ); + } + + inline auto wakeup() -> i2cdev_result_t + { + return this->setConfig(this->_config); + } + + auto setCalibration_32V_2A() -> i2cdev_result_t + { + uint16_t calValue = 0x1000; + auto result = this->_bus->writeReg16( + this->_addr, + INA219_REG_CALIBRATION, + static_cast(calValue) + ); + if (result != I2CDEV_RESULT_OK) { + return result; + } + + this->_calValue = calValue; + + result = this->setConfig( + (INA219_CONFIG_BUS_VOLTAGE_RANGE_32V | INA219_CONFIG_GAIN_8_320MV | INA219_CONFIG_BUS_ADC_12BIT | INA219_CONFIG_SHUNT_ADC_12BIT | INA219_CONFIG_MODE_SHUNT_BUS_CONTINUOUS) + ); + if (result != I2CDEV_RESULT_OK) { + return result; + } + + this->_currentDivider_mA = 10; + this->_powerMultiplier_mW = 2.0f; + + return result; + } + + auto setCalibration_32V_1A() -> i2cdev_result_t + { + uint16_t calValue = 0x2800; + auto result = this->_bus->writeReg16( + this->_addr, + INA219_REG_CALIBRATION, + static_cast(calValue) + ); + if (result != I2CDEV_RESULT_OK) { + return result; + } + + this->_calValue = calValue; + + result = this->setConfig( + (INA219_CONFIG_BUS_VOLTAGE_RANGE_32V | INA219_CONFIG_GAIN_8_320MV | INA219_CONFIG_BUS_ADC_12BIT | INA219_CONFIG_SHUNT_ADC_12BIT | INA219_CONFIG_MODE_SHUNT_BUS_CONTINUOUS) + ); + if (result != I2CDEV_RESULT_OK) { + return result; + } + + this->_currentDivider_mA = 25; + this->_powerMultiplier_mW = 0.8f; + + return result; + } + + auto setCalibration_16V_400mA() -> i2cdev_result_t + { + uint16_t calValue = 0x2000; + auto result = this->_bus->writeReg16( + this->_addr, + INA219_REG_CALIBRATION, + static_cast(calValue) + ); + if (result != I2CDEV_RESULT_OK) { + return result; + } + + this->_calValue = calValue; + + result = this->setConfig( + (INA219_CONFIG_BUS_VOLTAGE_RANGE_32V | INA219_CONFIG_GAIN_8_320MV | INA219_CONFIG_BUS_ADC_12BIT | INA219_CONFIG_SHUNT_ADC_12BIT | INA219_CONFIG_MODE_SHUNT_BUS_CONTINUOUS) + ); + if (result != I2CDEV_RESULT_OK) { + return result; + } + + this->_currentDivider_mA = 20; + this->_powerMultiplier_mW = 1.0f; + + return result; + } + + auto setConfig(uint16_t config) -> i2cdev_result_t + { + auto result = this->_bus->writeReg16( + this->_addr, + INA219_REG_CONFIG, + config + ); + + if (result != I2CDEV_RESULT_OK) { + return result; + } + + this->_config = config; + + return result; + } + + private: + + auto readRawShuntVoltage() -> int16_t + { + uint16_t value; + + this->_status = this->_bus->readReg16(this->_addr, INA219_REG_SHUNT_VOLTAGE, 1, &value); + if (this->_status != I2CDEV_RESULT_OK) { + return 0; + } + + return static_cast(value); + } + + auto readRawBusVoltage() -> int16_t + { + uint16_t value; + + this->_status = this->_bus->readReg16(this->_addr, INA219_REG_BUS_VOLTAGE, 1, &value); + if (this->_status != I2CDEV_RESULT_OK) { + return 0; + } + + // Shift to the right 3 to drop CNVR and OVF and multiply by LSB + return static_cast((value >> 3) * 4); + } + + auto readRawPower() -> int16_t + { + 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 INA219_REG_CALIBRATION even if it's an unfortunate extra step + this->_status = this->_bus->writeReg16(this->_addr, INA219_REG_CALIBRATION, static_cast(this->_calValue)); + if (this->_status != I2CDEV_RESULT_OK) { + return 0; + } + + this->_status = this->_bus->readReg16(this->_addr, INA219_REG_POWER, 1, &value); + if (this->_status != I2CDEV_RESULT_OK) { + return 0; + } + + return static_cast(value); + } + + auto readRawCurrent() -> int16_t + { + 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 INA219_REG_CALIBRATION even if it's an unfortunate extra step + this->_status = this->_bus->writeReg16(this->_addr, INA219_REG_CALIBRATION, static_cast(this->_calValue)); + if (this->_status != I2CDEV_RESULT_OK) { + return 0; + } + + this->_status = this->_bus->readReg16(this->_addr, INA219_REG_CURRENT, 1, &value); + if (this->_status != I2CDEV_RESULT_OK) { + return 0; + } + + return static_cast(value); + } + }; +} + +#endif //__I2CDEVLIB_INA219_HPP__ From 2ac150d992470ffeac9b8ae86d5d9dc308650d1e Mon Sep 17 00:00:00 2001 From: Leonid Meleshin Date: Wed, 17 Jul 2024 23:52:45 +0400 Subject: [PATCH 2/2] =?UTF-8?q?=E2=9C=A8=20(INA219):=20Add=20functions=20t?= =?UTF-8?q?o=20extract=20concrete=20settings=20from=20config=20reg?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/i2cdev/ina219.h | 76 +++++++++++++++++++++++++++++++++++++++---- src/i2cdev/ina219.hpp | 20 ++++++------ 2 files changed, 80 insertions(+), 16 deletions(-) diff --git a/src/i2cdev/ina219.h b/src/i2cdev/ina219.h index 4d76fa5..0a51970 100644 --- a/src/i2cdev/ina219.h +++ b/src/i2cdev/ina219.h @@ -3,9 +3,15 @@ #include "i2cdevbus.h" +#ifdef __cplusplus +extern "C" { +#endif // __cplusplus + #define INA219_I2CADDR_BASE (0x40) // A0 and A1 tied to GND #define INA219_I2CADDR_GET(A0, A1) (0x40 | ((A0) != 0 ? 0x01 : 0x00) | ((A1) != 0 ? 0x04 : 0x00)) +#define INA219_CREATE_MASK(shift, length) (((1 << (length)) - 1) << (shift)) + typedef enum ina219_reg_t { /// Configuration Register INA219_REG_CONFIG = (0x00), @@ -21,9 +27,15 @@ typedef enum ina219_reg_t { INA219_REG_CALIBRATION = (0x05) } ina219_reg_t; -#define INA219_CONFIG_RESET (0x8000) // 0b1000000000000000 +#define INA219_CONFIG_DEFAULT (0x399F) // Datasheet 8.6.1 -#define INA219_CONFIG_BUS_VOLTAGE_RANGE_MASK (0x2000) // 0b0010000000000000 +#define INA219_CONFIG_RESET_SHIFT (15) +#define INA219_CONFIG_RESET_LENGTH (1) +#define INA219_CONFIG_RESET INA219_CREATE_MASK(INA219_CONFIG_RESET_SHIFT, INA219_CONFIG_RESET_LENGTH) + +#define INA219_CONFIG_BUS_VOLTAGE_RANGE_SHIFT (13) +#define INA219_CONFIG_BUS_VOLTAGE_RANGE_LENGTH (1) +#define INA219_CONFIG_BUS_VOLTAGE_RANGE_MASK INA219_CREATE_MASK(INA219_CONFIG_BUS_VOLTAGE_RANGE_SHIFT, INA219_CONFIG_BUS_VOLTAGE_RANGE_LENGTH) typedef enum ina219_bus_voltage_range_t { /// 0-16V Range INA219_CONFIG_BUS_VOLTAGE_RANGE_16V = (0x0000), // 0b0000000000000000 @@ -31,7 +43,9 @@ typedef enum ina219_bus_voltage_range_t { INA219_CONFIG_BUS_VOLTAGE_RANGE_32V = (0x2000) // 0b0010000000000000 } ina219_bus_voltage_range_t; -#define INA218_CONFIG_GAIN_MASK (0x1800) // 0b0001100000000000 +#define INA219_CONFIG_GAIN_SHIFT (11) +#define INA219_CONFIG_GAIN_LENGTH (2) +#define INA218_CONFIG_GAIN_MASK INA219_CREATE_MASK(INA219_CONFIG_GAIN_SHIFT, INA219_CONFIG_GAIN_LENGTH) typedef enum ina219_gain_t { /// Gain 1, 40mV Range INA219_CONFIG_GAIN_1_40MV = (0x0000), // 0b0000000000000000 @@ -43,7 +57,9 @@ typedef enum ina219_gain_t { INA219_CONFIG_GAIN_8_320MV = (0x1800) // 0b0001100000000000 } ina219_gain_t; -#define INA219_CONFIG_BUS_ADC_MASK (0x0780) // 0b0000011110000000 +#define INA219_CONFIG_BADC_SHIFT (7) +#define INA219_CONFIG_BADC_LENGTH (4) +#define INA219_CONFIG_BUS_ADC_MASK INA219_CREATE_MASK(INA219_CONFIG_BADC_SHIFT, INA219_CONFIG_BADC_LENGTH) typedef enum ina219_bus_adc_t { /// 9-bit bus resolution (0-511) INA219_CONFIG_BUS_ADC_9BIT = (0x0000), @@ -69,7 +85,9 @@ typedef enum ina219_bus_adc_t { INA219_CONFIG_BUS_ADC_12BIT_128S = (0x0780) } ina219_bus_adc_t; -#define INA219_CONFIG_SHUNT_ADC_MASK (0x0078) // 0b0000001111000 +#define INA219_CONFIG_SADC_SHIFT (3) +#define INA219_CONFIG_SADC_LENGTH (4) +#define INA219_CONFIG_SHUNT_ADC_MASK INA219_CREATE_MASK(INA219_CONFIG_SADC_SHIFT, INA219_CONFIG_SADC_LENGTH) typedef enum ina219_shunt_adc_t { /// 9-bit shunt resolution (0-511) INA219_CONFIG_SHUNT_ADC_9BIT = (0x0000), @@ -95,7 +113,9 @@ typedef enum ina219_shunt_adc_t { INA219_CONFIG_SHUNT_ADC_12BIT_128S = (0x0078) } ina219_shunt_adc_t; -#define INA219_CONFIG_MODE_MASK (0x0007) // 0b0000000000000111 +#define INA219_CONFIG_MODE_SHIFT (0) +#define INA219_CONFIG_MODE_LENGTH (3) +#define INA219_CONFIG_MODE_MASK INA219_CREATE_MASK(INA219_CONFIG_MODE_SHIFT, INA219_CONFIG_MODE_LENGTH) typedef enum ina219_mode_t { /// Power-Down INA219_CONFIG_MODE_POWERDOWN = (0x0000), // 0b0000000000000000 @@ -115,4 +135,48 @@ typedef enum ina219_mode_t { INA219_CONFIG_MODE_SHUNT_BUS_CONTINUOUS = (0x0007) // 0b0000000000000111 } ina219_mode_t; +uint16_t ina219_config_set_bus_voltage_range(uint16_t config, ina219_bus_voltage_range_t range) { + return (config & ~INA219_CONFIG_BUS_VOLTAGE_RANGE_MASK) | (static_cast(range)); +} + +ina219_bus_voltage_range_t ina219_config_get_bus_voltage_range(uint16_t config) { + return static_cast(config & INA219_CONFIG_BUS_VOLTAGE_RANGE_MASK); +} + +uint16_t ina219_config_set_gain(uint16_t config, ina219_gain_t gain) { + return (config & ~INA218_CONFIG_GAIN_MASK) | (static_cast(gain)); +} + +ina219_gain_t ina219_config_get_gain(uint16_t config) { + return static_cast(config & INA218_CONFIG_GAIN_MASK); +} + +uint16_t ina219_config_set_bus_adc(uint16_t config, ina219_bus_adc_t adc) { + return (config & ~INA219_CONFIG_BUS_ADC_MASK) | (static_cast(adc)); +} + +ina219_bus_adc_t ina219_config_get_bus_adc(uint16_t config) { + return static_cast(config & INA219_CONFIG_BUS_ADC_MASK); +} + +uint16_t ina219_config_set_shunt_adc(uint16_t config, ina219_shunt_adc_t adc) { + return (config & ~INA219_CONFIG_SHUNT_ADC_MASK) | (static_cast(adc)); +} + +ina219_shunt_adc_t ina219_config_get_shunt_adc(uint16_t config) { + return static_cast(config & INA219_CONFIG_SHUNT_ADC_MASK); +} + +uint16_t ina219_config_set_mode(uint16_t config, ina219_mode_t mode) { + return (config & ~INA219_CONFIG_MODE_MASK) | (static_cast(mode)); +} + +ina219_mode_t ina219_config_get_mode(uint16_t config) { + return static_cast(config & INA219_CONFIG_MODE_MASK); +} + +#ifdef __cplusplus +}; +#endif // __cplusplus + #endif //__I2CDEVLIB_INA219_H__ diff --git a/src/i2cdev/ina219.hpp b/src/i2cdev/ina219.hpp index 6746e26..6e479af 100644 --- a/src/i2cdev/ina219.hpp +++ b/src/i2cdev/ina219.hpp @@ -95,9 +95,9 @@ namespace i2cdev { { uint16_t calValue = 0x2800; auto result = this->_bus->writeReg16( - this->_addr, - INA219_REG_CALIBRATION, - static_cast(calValue) + this->_addr, + INA219_REG_CALIBRATION, + static_cast(calValue) ); if (result != I2CDEV_RESULT_OK) { return result; @@ -106,7 +106,7 @@ namespace i2cdev { this->_calValue = calValue; result = this->setConfig( - (INA219_CONFIG_BUS_VOLTAGE_RANGE_32V | INA219_CONFIG_GAIN_8_320MV | INA219_CONFIG_BUS_ADC_12BIT | INA219_CONFIG_SHUNT_ADC_12BIT | INA219_CONFIG_MODE_SHUNT_BUS_CONTINUOUS) + (INA219_CONFIG_BUS_VOLTAGE_RANGE_32V | INA219_CONFIG_GAIN_8_320MV | INA219_CONFIG_BUS_ADC_12BIT | INA219_CONFIG_SHUNT_ADC_12BIT | INA219_CONFIG_MODE_SHUNT_BUS_CONTINUOUS) ); if (result != I2CDEV_RESULT_OK) { return result; @@ -122,9 +122,9 @@ namespace i2cdev { { uint16_t calValue = 0x2000; auto result = this->_bus->writeReg16( - this->_addr, - INA219_REG_CALIBRATION, - static_cast(calValue) + this->_addr, + INA219_REG_CALIBRATION, + static_cast(calValue) ); if (result != I2CDEV_RESULT_OK) { return result; @@ -133,7 +133,7 @@ namespace i2cdev { this->_calValue = calValue; result = this->setConfig( - (INA219_CONFIG_BUS_VOLTAGE_RANGE_32V | INA219_CONFIG_GAIN_8_320MV | INA219_CONFIG_BUS_ADC_12BIT | INA219_CONFIG_SHUNT_ADC_12BIT | INA219_CONFIG_MODE_SHUNT_BUS_CONTINUOUS) + (INA219_CONFIG_BUS_VOLTAGE_RANGE_32V | INA219_CONFIG_GAIN_8_320MV | INA219_CONFIG_BUS_ADC_12BIT | INA219_CONFIG_SHUNT_ADC_12BIT | INA219_CONFIG_MODE_SHUNT_BUS_CONTINUOUS) ); if (result != I2CDEV_RESULT_OK) { return result; @@ -145,6 +145,8 @@ namespace i2cdev { return result; } + private: + auto setConfig(uint16_t config) -> i2cdev_result_t { auto result = this->_bus->writeReg16( @@ -162,8 +164,6 @@ namespace i2cdev { return result; } - private: - auto readRawShuntVoltage() -> int16_t { uint16_t value;