Skip to content
Newer
Older
100644 372 lines (324 sloc) 12.2 KB
bea2aa6 First commit
K.Townsend authored Jun 21, 2012
1 /**************************************************************************/
2 /*!
3 @file Adafruit_ADS1015.cpp
4 @author K.Townsend (Adafruit Industries)
5 @license BSD (see license.txt)
6
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
7 Driver for the ADS1015/ADS1115 ADC
bea2aa6 First commit
K.Townsend authored Jun 21, 2012
8
9 This is a library for the Adafruit MPL115A2 breakout
10 ----> https://www.adafruit.com/products/???
11
12 Adafruit invests time and resources providing this open source code,
13 please support Adafruit and open-source hardware by purchasing
14 products from Adafruit!
15
16 @section HISTORY
17
18 v1.0 - First release
19 */
20 /**************************************************************************/
21 #if ARDUINO >= 100
22 #include "Arduino.h"
23 #else
24 #include "WProgram.h"
25 #endif
26
27 #include <Wire.h>
28
29 #include "Adafruit_ADS1015.h"
30
31 /**************************************************************************/
32 /*!
33 @brief Abstract away platform differences in Arduino wire library
34 */
35 /**************************************************************************/
36 static uint8_t i2cread(void) {
37 #if ARDUINO >= 100
38 return Wire.read();
39 #else
40 return Wire.receive();
41 #endif
42 }
43
44 /**************************************************************************/
45 /*!
46 @brief Abstract away platform differences in Arduino wire library
47 */
48 /**************************************************************************/
49 static void i2cwrite(uint8_t x) {
50 #if ARDUINO >= 100
51 Wire.write((uint8_t)x);
52 #else
53 Wire.send(x);
54 #endif
55 }
56
57 /**************************************************************************/
58 /*!
59 @brief Writes 16-bits to the specified destination register
60 */
61 /**************************************************************************/
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
62 static void writeRegister(uint8_t i2cAddress, uint8_t reg, uint16_t value) {
63 Wire.beginTransmission(i2cAddress);
bea2aa6 First commit
K.Townsend authored Jun 21, 2012
64 i2cwrite((uint8_t)reg);
65 i2cwrite((uint8_t)(value>>8));
66 i2cwrite((uint8_t)(value & 0xFF));
67 Wire.endTransmission();
68 }
69
70 /**************************************************************************/
71 /*!
72 @brief Writes 16-bits to the specified destination register
73 */
74 /**************************************************************************/
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
75 static uint16_t readRegister(uint8_t i2cAddress, uint8_t reg) {
76 Wire.beginTransmission(i2cAddress);
bea2aa6 First commit
K.Townsend authored Jun 21, 2012
77 i2cwrite(ADS1015_REG_POINTER_CONVERT);
78 Wire.endTransmission();
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
79 Wire.requestFrom(i2cAddress, (uint8_t)2);
bea2aa6 First commit
K.Townsend authored Jun 21, 2012
80 return ((i2cread() << 8) | i2cread());
81 }
82
83 /**************************************************************************/
84 /*!
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
85 @brief Instantiates a new ADS1015 class w/appropriate properties
bea2aa6 First commit
K.Townsend authored Jun 21, 2012
86 */
87 /**************************************************************************/
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
88 Adafruit_ADS1015::Adafruit_ADS1015(uint8_t i2cAddress)
89 {
90 m_i2cAddress = i2cAddress;
91 m_conversionDelay = ADS1015_CONVERSIONDELAY;
92 m_bitShift = 4;
f5d1f54 @microbuilder Added configurable gain/input voltage range
microbuilder authored Aug 31, 2013
93 m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
94 }
95
96 /**************************************************************************/
97 /*!
98 @brief Instantiates a new ADS1115 class w/appropriate properties
99 */
100 /**************************************************************************/
101 Adafruit_ADS1115::Adafruit_ADS1115(uint8_t i2cAddress)
102 {
103 m_i2cAddress = i2cAddress;
104 m_conversionDelay = ADS1115_CONVERSIONDELAY;
105 m_bitShift = 0;
f5d1f54 @microbuilder Added configurable gain/input voltage range
microbuilder authored Aug 30, 2013
106 m_gain = GAIN_TWOTHIRDS; /* +/- 6.144V range (limited to VDD +0.3V max!) */
bea2aa6 First commit
K.Townsend authored Jun 21, 2012
107 }
108
109 /**************************************************************************/
110 /*!
f5d1f54 @microbuilder Added configurable gain/input voltage range
microbuilder authored Aug 30, 2013
111 @brief Sets up the HW (reads coefficients values, etc.)
bea2aa6 First commit
K.Townsend authored Jun 21, 2012
112 */
113 /**************************************************************************/
114 void Adafruit_ADS1015::begin() {
115 Wire.begin();
116 }
117
118 /**************************************************************************/
119 /*!
f5d1f54 @microbuilder Added configurable gain/input voltage range
microbuilder authored Aug 30, 2013
120 @brief Sets the gain and input voltage range
121 */
122 /**************************************************************************/
123 void Adafruit_ADS1015::setGain(adsGain_t gain)
124 {
125 m_gain = gain;
126 }
127
128 /**************************************************************************/
129 /*!
130 @brief Gets a gain and input voltage range
131 */
132 /**************************************************************************/
133 adsGain_t Adafruit_ADS1015::getGain()
134 {
135 return m_gain;
136 }
137
138 /**************************************************************************/
139 /*!
bea2aa6 First commit
K.Townsend authored Jun 21, 2012
140 @brief Gets a single-ended ADC reading from the specified channel
141 */
142 /**************************************************************************/
143 uint16_t Adafruit_ADS1015::readADC_SingleEnded(uint8_t channel) {
144 if (channel > 3)
145 {
146 return 0;
147 }
148
149 // Start with default values
150 uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
151 ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
152 ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
153 ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
154 ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
155 ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
156
157 // Set PGA/voltage range
f5d1f54 @microbuilder Added configurable gain/input voltage range
microbuilder authored Aug 30, 2013
158 config |= m_gain;
bea2aa6 First commit
K.Townsend authored Jun 21, 2012
159
160 // Set single-ended input channel
161 switch (channel)
162 {
163 case (0):
164 config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
165 break;
166 case (1):
167 config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
168 break;
169 case (2):
170 config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
171 break;
172 case (3):
173 config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
174 break;
175 }
176
177 // Set 'start single-conversion' bit
178 config |= ADS1015_REG_CONFIG_OS_SINGLE;
179
180 // Write config register to the ADC
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
181 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);
bea2aa6 First commit
K.Townsend authored Jun 21, 2012
182
183 // Wait for the conversion to complete
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
184 delay(m_conversionDelay);
bea2aa6 First commit
K.Townsend authored Jun 21, 2012
185
186 // Read the conversion results
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
187 // Shift 12-bit results right 4 bits for the ADS1015
188 return readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
bea2aa6 First commit
K.Townsend authored Jun 21, 2012
189 }
190
e2a5e4c Added differential support
K.Townsend authored Jun 22, 2012
191 /**************************************************************************/
192 /*!
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
193 @brief Reads the conversion results, measuring the voltage
e2a5e4c Added differential support
K.Townsend authored Jun 22, 2012
194 difference between the P (AIN0) and N (AIN1) input. Generates
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
195 a signed value since the difference can be either
e2a5e4c Added differential support
K.Townsend authored Jun 22, 2012
196 positive or negative.
197 */
198 /**************************************************************************/
199 int16_t Adafruit_ADS1015::readADC_Differential_0_1() {
200 // Start with default values
201 uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
202 ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
203 ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
204 ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
205 ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
206 ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
207
208 // Set PGA/voltage range
f5d1f54 @microbuilder Added configurable gain/input voltage range
microbuilder authored Aug 30, 2013
209 config |= m_gain;
210
e2a5e4c Added differential support
K.Townsend authored Jun 22, 2012
211 // Set channels
212 config |= ADS1015_REG_CONFIG_MUX_DIFF_0_1; // AIN0 = P, AIN1 = N
213
214 // Set 'start single-conversion' bit
215 config |= ADS1015_REG_CONFIG_OS_SINGLE;
216
217 // Write config register to the ADC
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
218 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);
e2a5e4c Added differential support
K.Townsend authored Jun 22, 2012
219
220 // Wait for the conversion to complete
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
221 delay(m_conversionDelay);
e2a5e4c Added differential support
K.Townsend authored Jun 22, 2012
222
223 // Read the conversion results
f321d6b @microbuilder Fixed sign issues with differential code and better examples
microbuilder authored Oct 18, 2013
224 uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
225 if (m_bitShift == 0)
226 {
227 return (int16_t)res;
228 }
229 else
230 {
231 // Shift 12-bit results right 4 bits for the ADS1015,
232 // making sure we keep the sign bit intact
233 if (res > 0x07FF)
234 {
235 // negative number - extend the sign to 16th bit
236 res |= 0xF000;
237 }
238 return (int16_t)res;
239 }
e2a5e4c Added differential support
K.Townsend authored Jun 22, 2012
240 }
241
242 /**************************************************************************/
243 /*!
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
244 @brief Reads the conversion results, measuring the voltage
e2a5e4c Added differential support
K.Townsend authored Jun 22, 2012
245 difference between the P (AIN2) and N (AIN3) input. Generates
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
246 a signed value since the difference can be either
e2a5e4c Added differential support
K.Townsend authored Jun 22, 2012
247 positive or negative.
248 */
249 /**************************************************************************/
250 int16_t Adafruit_ADS1015::readADC_Differential_2_3() {
251 // Start with default values
252 uint16_t config = ADS1015_REG_CONFIG_CQUE_NONE | // Disable the comparator (default val)
253 ADS1015_REG_CONFIG_CLAT_NONLAT | // Non-latching (default val)
254 ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
255 ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
256 ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
257 ADS1015_REG_CONFIG_MODE_SINGLE; // Single-shot mode (default)
258
259 // Set PGA/voltage range
f5d1f54 @microbuilder Added configurable gain/input voltage range
microbuilder authored Aug 30, 2013
260 config |= m_gain;
e2a5e4c Added differential support
K.Townsend authored Jun 22, 2012
261
262 // Set channels
263 config |= ADS1015_REG_CONFIG_MUX_DIFF_2_3; // AIN2 = P, AIN3 = N
264
265 // Set 'start single-conversion' bit
266 config |= ADS1015_REG_CONFIG_OS_SINGLE;
267
268 // Write config register to the ADC
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
269 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);
e2a5e4c Added differential support
K.Townsend authored Jun 22, 2012
270
271 // Wait for the conversion to complete
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
272 delay(m_conversionDelay);
e2a5e4c Added differential support
K.Townsend authored Jun 22, 2012
273
f321d6b @microbuilder Fixed sign issues with differential code and better examples
microbuilder authored Oct 18, 2013
274 // Read the conversion results
275 uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
276 if (m_bitShift == 0)
277 {
278 return (int16_t)res;
279 }
280 else
281 {
282 // Shift 12-bit results right 4 bits for the ADS1015,
283 // making sure we keep the sign bit intact
284 if (res > 0x07FF)
285 {
286 // negative number - extend the sign to 16th bit
287 res |= 0xF000;
288 }
289 return (int16_t)res;
290 }
e2a5e4c Added differential support
K.Townsend authored Jun 22, 2012
291 }
30d3ad7 Added comparator example
K.Townsend authored Jun 26, 2012
292
293 /**************************************************************************/
294 /*!
295 @brief Sets up the comparator to operate in basic mode, causing the
296 ALERT/RDY pin to assert (go from high to low) when the ADC
297 value exceeds the specified threshold.
298
299 This will also set the ADC in continuous conversion mode.
300 */
301 /**************************************************************************/
302 void Adafruit_ADS1015::startComparator_SingleEnded(uint8_t channel, int16_t threshold)
303 {
304 // Start with default values
305 uint16_t config = ADS1015_REG_CONFIG_CQUE_1CONV | // Comparator enabled and asserts on 1 match
306 ADS1015_REG_CONFIG_CLAT_LATCH | // Latching mode
307 ADS1015_REG_CONFIG_CPOL_ACTVLOW | // Alert/Rdy active low (default val)
308 ADS1015_REG_CONFIG_CMODE_TRAD | // Traditional comparator (default val)
309 ADS1015_REG_CONFIG_DR_1600SPS | // 1600 samples per second (default)
310 ADS1015_REG_CONFIG_MODE_CONTIN | // Continuous conversion mode
311 ADS1015_REG_CONFIG_MODE_CONTIN; // Continuous conversion mode
312
f5d1f54 @microbuilder Added configurable gain/input voltage range
microbuilder authored Aug 30, 2013
313 // Set PGA/voltage range
314 config |= m_gain;
315
30d3ad7 Added comparator example
K.Townsend authored Jun 25, 2012
316 // Set single-ended input channel
317 switch (channel)
318 {
319 case (0):
320 config |= ADS1015_REG_CONFIG_MUX_SINGLE_0;
321 break;
322 case (1):
323 config |= ADS1015_REG_CONFIG_MUX_SINGLE_1;
324 break;
325 case (2):
326 config |= ADS1015_REG_CONFIG_MUX_SINGLE_2;
327 break;
328 case (3):
329 config |= ADS1015_REG_CONFIG_MUX_SINGLE_3;
330 break;
331 }
332
333 // Set the high threshold register
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
334 // Shift 12-bit results left 4 bits for the ADS1015
335 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_HITHRESH, threshold << m_bitShift);
30d3ad7 Added comparator example
K.Townsend authored Jun 25, 2012
336
337 // Write config register to the ADC
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
338 writeRegister(m_i2cAddress, ADS1015_REG_POINTER_CONFIG, config);
30d3ad7 Added comparator example
K.Townsend authored Jun 25, 2012
339 }
340
341 /**************************************************************************/
342 /*!
343 @brief In order to clear the comparator, we need to read the
344 conversion results. This function reads the last conversion
345 results without changing the config value.
346 */
347 /**************************************************************************/
348 int16_t Adafruit_ADS1015::getLastConversionResults()
349 {
350 // Wait for the conversion to complete
b166946 ADS1115 support added (W. Earl)
K.Townsend authored Nov 29, 2012
351 delay(m_conversionDelay);
30d3ad7 Added comparator example
K.Townsend authored Jun 25, 2012
352
353 // Read the conversion results
f321d6b @microbuilder Fixed sign issues with differential code and better examples
microbuilder authored Oct 18, 2013
354 uint16_t res = readRegister(m_i2cAddress, ADS1015_REG_POINTER_CONVERT) >> m_bitShift;
355 if (m_bitShift == 0)
356 {
357 return (int16_t)res;
358 }
359 else
360 {
361 // Shift 12-bit results right 4 bits for the ADS1015,
362 // making sure we keep the sign bit intact
363 if (res > 0x07FF)
364 {
365 // negative number - extend the sign to 16th bit
366 res |= 0xF000;
367 }
368 return (int16_t)res;
369 }
30d3ad7 Added comparator example
K.Townsend authored Jun 25, 2012
370 }
371
Something went wrong with that request. Please try again.