From 5cf847aac01cbe255a0e87fa509088e00edc5fc0 Mon Sep 17 00:00:00 2001 From: Chip Schnarel Date: Mon, 7 Oct 2013 16:58:44 -0700 Subject: [PATCH 1/2] Added commands for averaging. Four commands allow switching V and A measurements between single-sample and 128-sample averaging. --- Adafruit_INA219.cpp | 77 +++++++++++++++++++++++++++++++++++++++++++++ Adafruit_INA219.h | 5 +++ 2 files changed, 82 insertions(+) diff --git a/Adafruit_INA219.cpp b/Adafruit_INA219.cpp index 42a1e69..787ff0b 100644 --- a/Adafruit_INA219.cpp +++ b/Adafruit_INA219.cpp @@ -358,3 +358,80 @@ float Adafruit_INA219::getCurrent_mA() { valueDec /= ina219_currentDivider_mA; return valueDec; } + +/**************************************************************************/ +/*! + @brief Change the config register so that the INA219 returns current + measurement values from single samples. +*/ +/**************************************************************************/ +void Adafruit_INA219::setAmpInstant() { + uint16_t value; + + // get the current configuration data + wireReadRegister(INA219_REG_CONFIG, &value); + + // change out the bits to average 1 samples at 12 bits per sample + value = value & ~INA219_CONFIG_SADCRES_MASK | INA219_CONFIG_SADCRES_12BIT_1S_532US ; + + // write the changed config value back again + wireWriteRegister(INA219_REG_CONFIG, value); +} + +/**************************************************************************/ +/*! + @brief Change the config register so that the INA219 returns a current + measurement value that is an average over 128 samples. +*/ +/**************************************************************************/ +void Adafruit_INA219::setAmpAverage() { + uint16_t value; + + // get the current configuration data + wireReadRegister(INA219_REG_CONFIG, &value); + + // change out the bits to average 128 samples at 12 bits per sample + value = value & ~INA219_CONFIG_SADCRES_MASK | INA219_CONFIG_SADCRES_12BIT_128S_69MS; + + // write the changed config value back again + wireWriteRegister(INA219_REG_CONFIG, value); +} + +/**************************************************************************/ +/*! + @brief Change the config register so that the INA219 returns voltage + measurement values from single samples. +*/ +/**************************************************************************/ +void Adafruit_INA219::setVoltInstant() { + uint16_t value; + + // get the current configuration data + wireReadRegister(INA219_REG_CONFIG, &value); + + // change out the bits to average 1 samples at 12 bits per sample + value = value & ~INA219_CONFIG_BADCRES_MASK | INA219_CONFIG_BADCRES_12BIT ; + + // write the changed config value back again + wireWriteRegister(INA219_REG_CONFIG, value); +} + +/**************************************************************************/ +/*! + @brief Change the config register so that the INA219 returns a current + measurement value that is an average over 128 samples. +*/ +/**************************************************************************/ +void Adafruit_INA219::setVoltAverage() { + uint16_t value; + + // get the current configuration data + wireReadRegister(INA219_REG_CONFIG, &value); + + // change out the bits to average 128 samples at 12 bits per sample + value = value & ~INA219_CONFIG_BADCRES_MASK | INA219_CONFIG_BADCRES_12BIT_128S_69MS ; + + // write the changed config value back again + wireWriteRegister(INA219_REG_CONFIG, value); +} + diff --git a/Adafruit_INA219.h b/Adafruit_INA219.h index d669377..1180cce 100644 --- a/Adafruit_INA219.h +++ b/Adafruit_INA219.h @@ -54,6 +54,7 @@ #define INA219_CONFIG_BADCRES_10BIT (0x0100) // 10-bit bus res = 0..1023 #define INA219_CONFIG_BADCRES_11BIT (0x0200) // 11-bit bus res = 0..2047 #define INA219_CONFIG_BADCRES_12BIT (0x0400) // 12-bit bus res = 0..4097 + #define INA219_CONFIG_BADCRES_12BIT_128S_69MS (0x0780) // 128 x 12-bit bus samples averaged together #define INA219_CONFIG_SADCRES_MASK (0x0078) // Shunt ADC Resolution and Averaging Mask #define INA219_CONFIG_SADCRES_9BIT_1S_84US (0x0000) // 1 x 9-bit shunt sample @@ -116,6 +117,10 @@ class Adafruit_INA219{ float getBusVoltage_V(void); float getShuntVoltage_mV(void); float getCurrent_mA(void); + void setAmpInstant(void); + void setAmpAverage(void); + void setVoltInstant(void); + void setVoltAverage(void); private: uint8_t ina219_i2caddr; From 1cf191e5a40253081fcfac61cbf46ba2cd21d1f3 Mon Sep 17 00:00:00 2001 From: Chip Schnarel Date: Wed, 9 Oct 2013 07:49:17 -0700 Subject: [PATCH 2/2] Added delay appropriate for averaging Modified averaging commands to add longer delay for first sample. Also tweaked README a bit. --- Adafruit_INA219.cpp | 6 ++++++ README.md | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Adafruit_INA219.cpp b/Adafruit_INA219.cpp index 787ff0b..4102a99 100644 --- a/Adafruit_INA219.cpp +++ b/Adafruit_INA219.cpp @@ -395,6 +395,9 @@ void Adafruit_INA219::setAmpAverage() { // write the changed config value back again wireWriteRegister(INA219_REG_CONFIG, value); + + delay(69); // Max 12-bit 128S conversion time is 69mS per sample, but + // read can happen more frequently } /**************************************************************************/ @@ -433,5 +436,8 @@ void Adafruit_INA219::setVoltAverage() { // write the changed config value back again wireWriteRegister(INA219_REG_CONFIG, value); + + delay(69); // Max 12-bit 128S conversion time is 69mS per sample, but + // read can happen more frequently } diff --git a/README.md b/README.md index 02a17f3..456619b 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ Adafruit_INA219 =============== -INA219 Current Sensor \ No newline at end of file +Arduino library to talk to the INA219 Current Sensor over I2C. \ No newline at end of file