Skip to content

Commit 2afb654

Browse files
authored
Merge pull request #59 from caternuson/refactor
Add data rate and remove fixed conversion delay
2 parents d997451 + 4edbf19 commit 2afb654

File tree

2 files changed

+73
-42
lines changed

2 files changed

+73
-42
lines changed

Adafruit_ADS1X15.cpp

Lines changed: 47 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@
3636
*/
3737
/**************************************************************************/
3838
Adafruit_ADS1015::Adafruit_ADS1015() {
39-
m_conversionDelay = ADS1015_CONVERSIONDELAY;
4039
m_bitShift = 4;
4140
m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */
41+
m_dataRate = RATE_ADS1015_1600SPS;
4242
}
4343

4444
/**************************************************************************/
@@ -47,9 +47,9 @@ Adafruit_ADS1015::Adafruit_ADS1015() {
4747
*/
4848
/**************************************************************************/
4949
Adafruit_ADS1115::Adafruit_ADS1115() {
50-
m_conversionDelay = ADS1115_CONVERSIONDELAY;
5150
m_bitShift = 0;
5251
m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */
52+
m_dataRate = RATE_ADS1115_128SPS;
5353
}
5454

5555
/**************************************************************************/
@@ -83,6 +83,24 @@ void Adafruit_ADS1X15::setGain(adsGain_t gain) { m_gain = gain; }
8383
/**************************************************************************/
8484
adsGain_t Adafruit_ADS1X15::getGain() { return m_gain; }
8585

86+
/**************************************************************************/
87+
/*!
88+
@brief Sets the data rate
89+
90+
@param rate the data rate to use
91+
*/
92+
/**************************************************************************/
93+
void Adafruit_ADS1X15::setDataRate(uint16_t rate) { m_dataRate = rate; }
94+
95+
/**************************************************************************/
96+
/*!
97+
@brief Gets the current data rate
98+
99+
@return the data rate
100+
*/
101+
/**************************************************************************/
102+
uint16_t Adafruit_ADS1X15::getDataRate() { return m_dataRate; }
103+
86104
/**************************************************************************/
87105
/*!
88106
@brief Gets a single-ended ADC reading from the specified channel
@@ -103,12 +121,14 @@ uint16_t Adafruit_ADS1X15::readADC_SingleEnded(uint8_t channel) {
103121
ADS1X15_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
104122
ADS1X15_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
105123
ADS1X15_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
106-
ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
107124
ADS1X15_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
108125

109126
// Set PGA/voltage range
110127
config |= m_gain;
111128

129+
// Set data rate
130+
config |= m_dataRate;
131+
112132
// Set single-ended input channel
113133
switch (channel) {
114134
case (0):
@@ -132,7 +152,8 @@ uint16_t Adafruit_ADS1X15::readADC_SingleEnded(uint8_t channel) {
132152
writeRegister(ADS1X15_REG_POINTER_CONFIG, config);
133153

134154
// Wait for the conversion to complete
135-
delay(m_conversionDelay);
155+
while (!conversionComplete())
156+
;
136157

137158
// Read the conversion results
138159
// Shift 12-bit results right 4 bits for the ADS1015
@@ -156,12 +177,14 @@ int16_t Adafruit_ADS1X15::readADC_Differential_0_1() {
156177
ADS1X15_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
157178
ADS1X15_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
158179
ADS1X15_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
159-
ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
160180
ADS1X15_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
161181

162182
// Set PGA/voltage range
163183
config |= m_gain;
164184

185+
// Set data rate
186+
config |= m_dataRate;
187+
165188
// Set channels
166189
config |= ADS1X15_REG_CONFIG_MUX_DIFF_0_1; // AIN0 = P, AIN1 = N
167190

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

174197
// Wait for the conversion to complete
175-
delay(m_conversionDelay);
198+
while (!conversionComplete())
199+
;
176200

177201
// Read the conversion results
178202
uint16_t res = readRegister(ADS1X15_REG_POINTER_CONVERT) >> m_bitShift;
@@ -206,12 +230,14 @@ int16_t Adafruit_ADS1X15::readADC_Differential_2_3() {
206230
ADS1X15_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
207231
ADS1X15_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
208232
ADS1X15_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
209-
ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
210233
ADS1X15_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
211234

212235
// Set PGA/voltage range
213236
config |= m_gain;
214237

238+
// Set data rate
239+
config |= m_dataRate;
240+
215241
// Set channels
216242
config |= ADS1X15_REG_CONFIG_MUX_DIFF_2_3; // AIN2 = P, AIN3 = N
217243

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

224250
// Wait for the conversion to complete
225-
delay(m_conversionDelay);
251+
while (!conversionComplete())
252+
;
226253

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

267293
// Set PGA/voltage range
268294
config |= m_gain;
269295

296+
// Set data rate
297+
config |= m_dataRate;
298+
270299
// Set single-ended input channel
271300
switch (channel) {
272301
case (0):
@@ -301,9 +330,6 @@ void Adafruit_ADS1X15::startComparator_SingleEnded(uint8_t channel,
301330
*/
302331
/**************************************************************************/
303332
int16_t Adafruit_ADS1X15::getLastConversionResults() {
304-
// Wait for the conversion to complete
305-
delay(m_conversionDelay);
306-
307333
// Read the conversion results
308334
uint16_t res = readRegister(ADS1X15_REG_POINTER_CONVERT) >> m_bitShift;
309335
if (m_bitShift == 0) {
@@ -319,6 +345,15 @@ int16_t Adafruit_ADS1X15::getLastConversionResults() {
319345
}
320346
}
321347

348+
/**************************************************************************/
349+
/*!
350+
@brief Returns true if conversion is complete, false otherwise.
351+
*/
352+
/**************************************************************************/
353+
bool Adafruit_ADS1X15::conversionComplete() {
354+
return readRegister(ADS1X15_REG_POINTER_CONFIG) & 0x8000 != 0;
355+
}
356+
322357
/**************************************************************************/
323358
/*!
324359
@brief Writes 16-bits to the specified destination register

Adafruit_ADS1X15.h

Lines changed: 26 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@
2626
#define ADS1X15_ADDRESS (0x48) ///< 1001 000 (ADDR = GND)
2727
/*=========================================================================*/
2828

29-
/*=========================================================================
30-
CONVERSION DELAY (in mS)
31-
-----------------------------------------------------------------------*/
32-
#define ADS1015_CONVERSIONDELAY (1) ///< Conversion delay
33-
#define ADS1115_CONVERSIONDELAY (9) ///< Conversion delay
34-
/*=========================================================================*/
35-
3629
/*=========================================================================
3730
POINTER REGISTER
3831
-----------------------------------------------------------------------*/
@@ -82,25 +75,7 @@
8275
#define ADS1X15_REG_CONFIG_MODE_SINGLE \
8376
(0x0100) ///< Power-down single-shot mode (default)
8477

85-
#define ADS1X15_REG_CONFIG_DR_MASK (0x00E0) ///< Data Rate Mask
86-
#define ADS1015_REG_CONFIG_DR_128SPS (0x0000) ///< 128 samples per second
87-
#define ADS1015_REG_CONFIG_DR_250SPS (0x0020) ///< 250 samples per second
88-
#define ADS1015_REG_CONFIG_DR_490SPS (0x0040) ///< 490 samples per second
89-
#define ADS1015_REG_CONFIG_DR_920SPS (0x0060) ///< 920 samples per second
90-
#define ADS1015_REG_CONFIG_DR_1600SPS \
91-
(0x0080) ///< 1600 samples per second (default)
92-
#define ADS1015_REG_CONFIG_DR_2400SPS (0x00A0) ///< 2400 samples per second
93-
#define ADS1015_REG_CONFIG_DR_3300SPS (0x00C0) ///< 3300 samples per second
94-
95-
#define ADS1115_REG_CONFIG_DR_8SPS (0x0000) ///< 8 samples per second
96-
#define ADS1115_REG_CONFIG_DR_16SPS (0x0020) ///< 16 samples per second
97-
#define ADS1115_REG_CONFIG_DR_32SPS (0x0040) ///< 32 samples per second
98-
#define ADS1115_REG_CONFIG_DR_64SPS (0x0060) ///< 64 samples per second
99-
#define ADS1115_REG_CONFIG_DR_128SPS \
100-
(0x0080) ///< 128 samples per second (default)
101-
#define ADS1115_REG_CONFIG_DR_250SPS (0x00A0) ///< 250 samples per second
102-
#define ADS1115_REG_CONFIG_DR_475SPS (0x00C0) ///< 475 samples per second
103-
#define ADS1115_REG_CONFIG_DR_860SPS (0x00C0) ///< 860 samples per second
78+
#define ADS1X15_REG_CONFIG_RATE_MASK (0x00E0) ///< Data Rate Mask
10479

10580
#define ADS1X15_REG_CONFIG_CMODE_MASK (0x0010) ///< CMode Mask
10681
#define ADS1X15_REG_CONFIG_CMODE_TRAD \
@@ -140,6 +115,24 @@ typedef enum {
140115
GAIN_SIXTEEN = ADS1X15_REG_CONFIG_PGA_0_256V
141116
} adsGain_t;
142117

118+
/** Data rates */
119+
#define RATE_ADS1015_128SPS (0x0000) ///< 128 samples per second
120+
#define RATE_ADS1015_250SPS (0x0020) ///< 250 samples per second
121+
#define RATE_ADS1015_490SPS (0x0040) ///< 490 samples per second
122+
#define RATE_ADS1015_920SPS (0x0060) ///< 920 samples per second
123+
#define RATE_ADS1015_1600SPS (0x0080) ///< 1600 samples per second (default)
124+
#define RATE_ADS1015_2400SPS (0x00A0) ///< 2400 samples per second
125+
#define RATE_ADS1015_3300SPS (0x00C0) ///< 3300 samples per second
126+
127+
#define RATE_ADS1115_8SPS (0x0000) ///< 8 samples per second
128+
#define RATE_ADS1115_16SPS (0x0020) ///< 16 samples per second
129+
#define RATE_ADS1115_32SPS (0x0040) ///< 32 samples per second
130+
#define RATE_ADS1115_64SPS (0x0060) ///< 64 samples per second
131+
#define RATE_ADS1115_128SPS (0x0080) ///< 128 samples per second (default)
132+
#define RATE_ADS1115_250SPS (0x00A0) ///< 250 samples per second
133+
#define RATE_ADS1115_475SPS (0x00C0) ///< 475 samples per second
134+
#define RATE_ADS1115_860SPS (0x00E0) ///< 860 samples per second
135+
143136
/**************************************************************************/
144137
/*!
145138
@brief Sensor driver for the Adafruit ADS1X15 ADC breakouts.
@@ -149,21 +142,24 @@ class Adafruit_ADS1X15 {
149142
protected:
150143
// Instance-specific properties
151144
Adafruit_I2CDevice *m_i2c_dev; ///< I2C bus device
152-
uint8_t m_conversionDelay; ///< conversion deay
153145
uint8_t m_bitShift; ///< bit shift amount
154146
adsGain_t m_gain; ///< ADC gain
147+
uint16_t m_dataRate; ///< Data rate
155148

156149
public:
157150
void begin(uint8_t i2c_addr = ADS1X15_ADDRESS, TwoWire *wire = &Wire);
158151
uint16_t readADC_SingleEnded(uint8_t channel);
159-
int16_t readADC_Differential_0_1(void);
160-
int16_t readADC_Differential_2_3(void);
152+
int16_t readADC_Differential_0_1();
153+
int16_t readADC_Differential_2_3();
161154
void startComparator_SingleEnded(uint8_t channel, int16_t threshold);
162155
int16_t getLastConversionResults();
163156
void setGain(adsGain_t gain);
164-
adsGain_t getGain(void);
157+
adsGain_t getGain();
158+
void setDataRate(uint16_t rate);
159+
uint16_t getDataRate();
165160

166161
private:
162+
bool conversionComplete();
167163
void writeRegister(uint8_t reg, uint16_t value);
168164
uint16_t readRegister(uint8_t reg);
169165
uint8_t buffer[3];

0 commit comments

Comments
 (0)