diff --git a/Adafruit_INA219.cpp b/Adafruit_INA219.cpp index 42a1e69..4102a99 100644 --- a/Adafruit_INA219.cpp +++ b/Adafruit_INA219.cpp @@ -358,3 +358,86 @@ 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); + + delay(69); // Max 12-bit 128S conversion time is 69mS per sample, but + // read can happen more frequently +} + +/**************************************************************************/ +/*! + @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); + + delay(69); // Max 12-bit 128S conversion time is 69mS per sample, but + // read can happen more frequently +} + 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; 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