Skip to content

Commit

Permalink
Merge pull request #73 from MrF83/master
Browse files Browse the repository at this point in the history
Added support for Differential measuring between AIN0 to AIN3 and AIN1 to AIN3.
  • Loading branch information
caternuson authored Dec 1, 2021
2 parents 67fc673 + 7192a60 commit 56268e3
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
82 changes: 82 additions & 0 deletions Adafruit_ADS1X15.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion Adafruit_ADS1X15.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -186,4 +188,4 @@ class Adafruit_ADS1115 : public Adafruit_ADS1X15 {
Adafruit_ADS1115();
};

#endif
#endif

0 comments on commit 56268e3

Please sign in to comment.