diff --git a/Adafruit_ADS1X15.cpp b/Adafruit_ADS1X15.cpp index ce386d9..29149dd 100644 --- a/Adafruit_ADS1X15.cpp +++ b/Adafruit_ADS1X15.cpp @@ -203,6 +203,88 @@ int16_t Adafruit_ADS1X15::readADC_Differential_0_1() { return getLastConversionResults(); } +/**************************************************************************/ +/*! + @brief Reads the conversion results, measuring the voltage + difference between the P (AIN0) and N (AIN3) input. Generates + a signed value since the difference can be either + positive or negative. + @return the ADC reading +*/ +/**************************************************************************/ +int16_t Adafruit_ADS1X15::readADC_Differential_0_3() { + // Start with default values + uint16_t config = + ADS1X15_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) + ADS1X15_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) + ADS1X15_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) + ADS1X15_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) + ADS1X15_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) + + // Set PGA/voltage range + config |= m_gain; + + // Set data rate + config |= m_dataRate; + + // Set channels + config |= ADS1X15_REG_CONFIG_MUX_DIFF_0_3; // AIN0 = P, AIN3 = N + + // Set 'start single-conversion' bit + config |= ADS1X15_REG_CONFIG_OS_SINGLE; + + // Write config register to the ADC + writeRegister(ADS1X15_REG_POINTER_CONFIG, config); + + // Wait for the conversion to complete + while (!conversionComplete()) + ; + + // Read the conversion results + return getLastConversionResults(); +} + +/**************************************************************************/ +/*! + @brief Reads the conversion results, measuring the voltage + difference between the P (AIN1) and N (AIN3) input. Generates + a signed value since the difference can be either + positive or negative. + @return the ADC reading +*/ +/**************************************************************************/ +int16_t Adafruit_ADS1X15::readADC_Differential_1_3() { + // Start with default values + uint16_t config = + ADS1X15_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val) + ADS1X15_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val) + ADS1X15_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val) + ADS1X15_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val) + ADS1X15_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default) + + // Set PGA/voltage range + config |= m_gain; + + // Set data rate + config |= m_dataRate; + + // Set channels + config |= ADS1X15_REG_CONFIG_MUX_DIFF_1_3; // AIN1 = P, AIN3 = N + + // Set 'start single-conversion' bit + config |= ADS1X15_REG_CONFIG_OS_SINGLE; + + // Write config register to the ADC + writeRegister(ADS1X15_REG_POINTER_CONFIG, config); + + // Wait for the conversion to complete + while (!conversionComplete()) + ; + + // Read the conversion results + return getLastConversionResults(); +} + /**************************************************************************/ /*! @brief Reads the conversion results, measuring the voltage diff --git a/Adafruit_ADS1X15.h b/Adafruit_ADS1X15.h index 89eadd6..d9baa73 100644 --- a/Adafruit_ADS1X15.h +++ b/Adafruit_ADS1X15.h @@ -150,6 +150,8 @@ class Adafruit_ADS1X15 { bool begin(uint8_t i2c_addr = ADS1X15_ADDRESS, TwoWire *wire = &Wire); int16_t readADC_SingleEnded(uint8_t channel); int16_t readADC_Differential_0_1(); + int16_t readADC_Differential_0_3(); + int16_t readADC_Differential_1_3(); int16_t readADC_Differential_2_3(); void startComparator_SingleEnded(uint8_t channel, int16_t threshold); int16_t getLastConversionResults(); @@ -186,4 +188,4 @@ class Adafruit_ADS1115 : public Adafruit_ADS1X15 { Adafruit_ADS1115(); }; -#endif \ No newline at end of file +#endif