Skip to content

Commit

Permalink
Merge pull request #59 from caternuson/refactor
Browse files Browse the repository at this point in the history
Add data rate and remove fixed conversion delay
  • Loading branch information
caternuson authored Mar 19, 2021
2 parents d997451 + 4edbf19 commit 2afb654
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 42 deletions.
59 changes: 47 additions & 12 deletions Adafruit_ADS1X15.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@
*/
/**************************************************************************/
Adafruit_ADS1015::Adafruit_ADS1015() {
m_conversionDelay = ADS1015_CONVERSIONDELAY;
m_bitShift = 4;
m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */
m_dataRate = RATE_ADS1015_1600SPS;
}

/**************************************************************************/
Expand All @@ -47,9 +47,9 @@ Adafruit_ADS1015::Adafruit_ADS1015() {
*/
/**************************************************************************/
Adafruit_ADS1115::Adafruit_ADS1115() {
m_conversionDelay = ADS1115_CONVERSIONDELAY;
m_bitShift = 0;
m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */
m_dataRate = RATE_ADS1115_128SPS;
}

/**************************************************************************/
Expand Down Expand Up @@ -83,6 +83,24 @@ void Adafruit_ADS1X15::setGain(adsGain_t gain) { m_gain = gain; }
/**************************************************************************/
adsGain_t Adafruit_ADS1X15::getGain() { return m_gain; }

/**************************************************************************/
/*!
@brief Sets the data rate
@param rate the data rate to use
*/
/**************************************************************************/
void Adafruit_ADS1X15::setDataRate(uint16_t rate) { m_dataRate = rate; }

/**************************************************************************/
/*!
@brief Gets the current data rate
@return the data rate
*/
/**************************************************************************/
uint16_t Adafruit_ADS1X15::getDataRate() { return m_dataRate; }

/**************************************************************************/
/*!
@brief Gets a single-ended ADC reading from the specified channel
Expand All @@ -103,12 +121,14 @@ uint16_t Adafruit_ADS1X15::readADC_SingleEnded(uint8_t channel) {
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)
ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
ADS1X15_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)

// Set PGA/voltage range
config |= m_gain;

// Set data rate
config |= m_dataRate;

// Set single-ended input channel
switch (channel) {
case (0):
Expand All @@ -132,7 +152,8 @@ uint16_t Adafruit_ADS1X15::readADC_SingleEnded(uint8_t channel) {
writeRegister(ADS1X15_REG_POINTER_CONFIG, config);

// Wait for the conversion to complete
delay(m_conversionDelay);
while (!conversionComplete())
;

// Read the conversion results
// Shift 12-bit results right 4 bits for the ADS1015
Expand All @@ -156,12 +177,14 @@ int16_t Adafruit_ADS1X15::readADC_Differential_0_1() {
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)
ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
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_1; // AIN0 = P, AIN1 = N

Expand All @@ -172,7 +195,8 @@ int16_t Adafruit_ADS1X15::readADC_Differential_0_1() {
writeRegister(ADS1X15_REG_POINTER_CONFIG, config);

// Wait for the conversion to complete
delay(m_conversionDelay);
while (!conversionComplete())
;

// Read the conversion results
uint16_t res = readRegister(ADS1X15_REG_POINTER_CONVERT) >> m_bitShift;
Expand Down Expand Up @@ -206,12 +230,14 @@ int16_t Adafruit_ADS1X15::readADC_Differential_2_3() {
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)
ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
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_2_3; // AIN2 = P, AIN3 = N

Expand All @@ -222,7 +248,8 @@ int16_t Adafruit_ADS1X15::readADC_Differential_2_3() {
writeRegister(ADS1X15_REG_POINTER_CONFIG, config);

// Wait for the conversion to complete
delay(m_conversionDelay);
while (!conversionComplete())
;

// Read the conversion results
uint16_t res = readRegister(ADS1X15_REG_POINTER_CONVERT) >> m_bitShift;
Expand Down Expand Up @@ -260,13 +287,15 @@ void Adafruit_ADS1X15::startComparator_SingleEnded(uint8_t channel,
ADS1X15_REG_CONFIG_CLAT_LATCH | // Latching mode
ADS1X15_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
ADS1X15_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
ADS1X15_REG_CONFIG_MODE_CONTIN | // Continuous conversion mode
ADS1X15_REG_CONFIG_MODE_CONTIN; // Continuous conversion mode

// Set PGA/voltage range
config |= m_gain;

// Set data rate
config |= m_dataRate;

// Set single-ended input channel
switch (channel) {
case (0):
Expand Down Expand Up @@ -301,9 +330,6 @@ void Adafruit_ADS1X15::startComparator_SingleEnded(uint8_t channel,
*/
/**************************************************************************/
int16_t Adafruit_ADS1X15::getLastConversionResults() {
// Wait for the conversion to complete
delay(m_conversionDelay);

// Read the conversion results
uint16_t res = readRegister(ADS1X15_REG_POINTER_CONVERT) >> m_bitShift;
if (m_bitShift == 0) {
Expand All @@ -319,6 +345,15 @@ int16_t Adafruit_ADS1X15::getLastConversionResults() {
}
}

/**************************************************************************/
/*!
@brief Returns true if conversion is complete, false otherwise.
*/
/**************************************************************************/
bool Adafruit_ADS1X15::conversionComplete() {
return readRegister(ADS1X15_REG_POINTER_CONFIG) & 0x8000 != 0;
}

/**************************************************************************/
/*!
@brief Writes 16-bits to the specified destination register
Expand Down
56 changes: 26 additions & 30 deletions Adafruit_ADS1X15.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@
#define ADS1X15_ADDRESS (0x48) ///< 1001 000 (ADDR = GND)
/*=========================================================================*/

/*=========================================================================
CONVERSION DELAY (in mS)
-----------------------------------------------------------------------*/
#define ADS1015_CONVERSIONDELAY (1) ///< Conversion delay
#define ADS1115_CONVERSIONDELAY (9) ///< Conversion delay
/*=========================================================================*/

/*=========================================================================
POINTER REGISTER
-----------------------------------------------------------------------*/
Expand Down Expand Up @@ -82,25 +75,7 @@
#define ADS1X15_REG_CONFIG_MODE_SINGLE \
(0x0100) ///< Power-down single-shot mode (default)

#define ADS1X15_REG_CONFIG_DR_MASK (0x00E0) ///< Data Rate Mask
#define ADS1015_REG_CONFIG_DR_128SPS (0x0000) ///< 128 samples per second
#define ADS1015_REG_CONFIG_DR_250SPS (0x0020) ///< 250 samples per second
#define ADS1015_REG_CONFIG_DR_490SPS (0x0040) ///< 490 samples per second
#define ADS1015_REG_CONFIG_DR_920SPS (0x0060) ///< 920 samples per second
#define ADS1015_REG_CONFIG_DR_1600SPS \
(0x0080) ///< 1600 samples per second (default)
#define ADS1015_REG_CONFIG_DR_2400SPS (0x00A0) ///< 2400 samples per second
#define ADS1015_REG_CONFIG_DR_3300SPS (0x00C0) ///< 3300 samples per second

#define ADS1115_REG_CONFIG_DR_8SPS (0x0000) ///< 8 samples per second
#define ADS1115_REG_CONFIG_DR_16SPS (0x0020) ///< 16 samples per second
#define ADS1115_REG_CONFIG_DR_32SPS (0x0040) ///< 32 samples per second
#define ADS1115_REG_CONFIG_DR_64SPS (0x0060) ///< 64 samples per second
#define ADS1115_REG_CONFIG_DR_128SPS \
(0x0080) ///< 128 samples per second (default)
#define ADS1115_REG_CONFIG_DR_250SPS (0x00A0) ///< 250 samples per second
#define ADS1115_REG_CONFIG_DR_475SPS (0x00C0) ///< 475 samples per second
#define ADS1115_REG_CONFIG_DR_860SPS (0x00C0) ///< 860 samples per second
#define ADS1X15_REG_CONFIG_RATE_MASK (0x00E0) ///< Data Rate Mask

#define ADS1X15_REG_CONFIG_CMODE_MASK (0x0010) ///< CMode Mask
#define ADS1X15_REG_CONFIG_CMODE_TRAD \
Expand Down Expand Up @@ -140,6 +115,24 @@ typedef enum {
GAIN_SIXTEEN = ADS1X15_REG_CONFIG_PGA_0_256V
} adsGain_t;

/** Data rates */
#define RATE_ADS1015_128SPS (0x0000) ///< 128 samples per second
#define RATE_ADS1015_250SPS (0x0020) ///< 250 samples per second
#define RATE_ADS1015_490SPS (0x0040) ///< 490 samples per second
#define RATE_ADS1015_920SPS (0x0060) ///< 920 samples per second
#define RATE_ADS1015_1600SPS (0x0080) ///< 1600 samples per second (default)
#define RATE_ADS1015_2400SPS (0x00A0) ///< 2400 samples per second
#define RATE_ADS1015_3300SPS (0x00C0) ///< 3300 samples per second

#define RATE_ADS1115_8SPS (0x0000) ///< 8 samples per second
#define RATE_ADS1115_16SPS (0x0020) ///< 16 samples per second
#define RATE_ADS1115_32SPS (0x0040) ///< 32 samples per second
#define RATE_ADS1115_64SPS (0x0060) ///< 64 samples per second
#define RATE_ADS1115_128SPS (0x0080) ///< 128 samples per second (default)
#define RATE_ADS1115_250SPS (0x00A0) ///< 250 samples per second
#define RATE_ADS1115_475SPS (0x00C0) ///< 475 samples per second
#define RATE_ADS1115_860SPS (0x00E0) ///< 860 samples per second

/**************************************************************************/
/*!
@brief Sensor driver for the Adafruit ADS1X15 ADC breakouts.
Expand All @@ -149,21 +142,24 @@ class Adafruit_ADS1X15 {
protected:
// Instance-specific properties
Adafruit_I2CDevice *m_i2c_dev; ///< I2C bus device
uint8_t m_conversionDelay; ///< conversion deay
uint8_t m_bitShift; ///< bit shift amount
adsGain_t m_gain; ///< ADC gain
uint16_t m_dataRate; ///< Data rate

public:
void begin(uint8_t i2c_addr = ADS1X15_ADDRESS, TwoWire *wire = &Wire);
uint16_t readADC_SingleEnded(uint8_t channel);
int16_t readADC_Differential_0_1(void);
int16_t readADC_Differential_2_3(void);
int16_t readADC_Differential_0_1();
int16_t readADC_Differential_2_3();
void startComparator_SingleEnded(uint8_t channel, int16_t threshold);
int16_t getLastConversionResults();
void setGain(adsGain_t gain);
adsGain_t getGain(void);
adsGain_t getGain();
void setDataRate(uint16_t rate);
uint16_t getDataRate();

private:
bool conversionComplete();
void writeRegister(uint8_t reg, uint16_t value);
uint16_t readRegister(uint8_t reg);
uint8_t buffer[3];
Expand Down

0 comments on commit 2afb654

Please sign in to comment.