1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
6 @brief Drivers for the TI ADS1015 12-Bit I2C ADC
10 The ADS1015 is a 4-channel, 12-bit I2C ADC with a programmable
11 comparator and an internal PGA (from 2/3 to 16x gain). It can be
12 configured for four single-ended channels or two differential inputs.
13 The comparator can be used in both traditional and windowed mode.
17 Software License Agreement (BSD License)
19 Copyright (c) 2012, K. Townsend
22 Redistribution and use in source and binary forms, with or without
23 modification, are permitted provided that the following conditions are met:
24 1. Redistributions of source code must retain the above copyright
25 notice, this list of conditions and the following disclaimer.
26 2. Redistributions in binary form must reproduce the above copyright
27 notice, this list of conditions and the following disclaimer in the
28 documentation and/or other materials provided with the distribution.
29 3. Neither the name of the copyright holders nor the
30 names of its contributors may be used to endorse or promote products
31 derived from this software without specific prior written permission.
33 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
34 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
35 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
37 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
38 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
39 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
40 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
42 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
44 /**************************************************************************/
46 #include "core/systick/systick.h"
48 extern volatile uint8_t I2CMasterBuffer
[I2C_BUFSIZE
];
49 extern volatile uint8_t I2CSlaveBuffer
[I2C_BUFSIZE
];
50 extern volatile uint32_t I2CReadLength
, I2CWriteLength
;
52 static bool _ads1015Initialised
= false;
54 /**************************************************************************/
56 @brief Sends a single command byte over I2C
58 /**************************************************************************/
59 static ads1015Error_t
ads1015WriteRegister (uint8_t reg
, uint16_t value
)
61 ads1015Error_t error
= ADS1015_ERROR_OK
;
63 // Clear write buffers
65 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
67 I2CMasterBuffer
[i
] = 0x00;
72 I2CMasterBuffer
[0] = ADS1015_ADDRESS
; // I2C device address
73 I2CMasterBuffer
[1] = reg
; // Register
74 I2CMasterBuffer
[2] = value
>> 8; // Upper 8-bits
75 I2CMasterBuffer
[3] = value
& 0xFF; // Lower 8-bits
78 // ToDo: Add in proper I2C error-checking
82 /**************************************************************************/
84 @brief Reads a 16 bit values over I2C
86 /**************************************************************************/
87 static ads1015Error_t
ina219Read16(uint8_t reg
, uint16_t *value
)
89 ads1015Error_t error
= ADS1015_ERROR_OK
;
91 // Clear write buffers
93 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
95 I2CMasterBuffer
[i
] = 0x00;
100 I2CMasterBuffer
[0] = ADS1015_ADDRESS
; // I2C device address
101 I2CMasterBuffer
[1] = reg
; // Command register
102 // Append address w/read bit
103 I2CMasterBuffer
[2] = ADS1015_ADDRESS
| ADS1015_READBIT
;
106 // Shift values to create properly formed integer
107 *value
= ((I2CSlaveBuffer
[0] << 8) | I2CSlaveBuffer
[1]);
109 // ToDo: Add in proper I2C error-checking
113 /**************************************************************************/
115 @brief Initialises the I2C block
117 /**************************************************************************/
118 ads1015Error_t
ads1015Init(void)
120 ads1015Error_t error
= ADS1015_ERROR_OK
;
123 if (i2cInit(I2CMASTER
) == false)
125 return ADS1015_ERROR_I2CINIT
; /* Fatal error */
128 _ads1015Initialised
= true;
132 /**************************************************************************/
134 @brief Reads the 12-bit conversion results from the specified channel
136 /**************************************************************************/
137 ads1015Error_t
ads1015ReadADC_SingleEnded(uint8_t channel
, uint16_t *value
)
139 ads1015Error_t error
= ADS1015_ERROR_OK
;
141 if (!(_ads1015Initialised
))
149 return ADS1015_ERROR_INVALIDCHANNEL
;
152 // Start with default values
153 uint16_t config
= ADS1015_REG_CONFIG_CQUE_NONE
| // Disable the comparator (default val)
154 ADS1015_REG_CONFIG_CLAT_NONLAT
| // Non-latching (default val)
155 ADS1015_REG_CONFIG_CPOL_ACTVLOW
| // Alert/Rdy active low (default val)
156 ADS1015_REG_CONFIG_CMODE_TRAD
| // Traditional comparator (default val)
157 ADS1015_REG_CONFIG_DR_1600SPS
| // 1600 samples per second (default)
158 ADS1015_REG_CONFIG_MODE_SINGLE
; // Single-shot mode (default)
160 // Set PGA/voltage range
161 config
|= ADS1015_REG_CONFIG_PGA_6_144V
; // +/- 6.144V range (limited to VDD +0.3V max!)
163 // Set single-ended input channel
167 config
|= ADS1015_REG_CONFIG_MUX_SINGLE_0
;
170 config
|= ADS1015_REG_CONFIG_MUX_SINGLE_1
;
173 config
|= ADS1015_REG_CONFIG_MUX_SINGLE_2
;
176 config
|= ADS1015_REG_CONFIG_MUX_SINGLE_3
;
180 // Set 'start single-conversion' bit
181 config
|= ADS1015_REG_CONFIG_OS_SINGLE
;
183 // Write config register to the ADC
184 error
= ads1015WriteRegister(ADS1015_REG_POINTER_CONFIG
, config
);
185 if (error
) return error
;
187 // Wait for the conversion to complete
190 // Read the conversion results
191 error
= ina219Read16(ADS1015_REG_POINTER_CONVERT
, value
);
192 if (error
) return error
;
194 // Shift results 4-bits to the right
195 *value
= *value
>> 4;
200 /**************************************************************************/
202 @brief Reads the 12-bit conversion results, measuring the voltage
203 difference between the P (AIN0) and N (AIN1) input. Generates
204 a signed 12-bit value since the difference can be either
205 positive or negative.
207 /**************************************************************************/
208 ads1015Error_t
ads1015ReadADC_Differential_0_1(int16_t *value
)
210 ads1015Error_t error
= ADS1015_ERROR_OK
;
212 if (!(_ads1015Initialised
))
217 // Start with default values
218 uint16_t config
= ADS1015_REG_CONFIG_CQUE_NONE
| // Disable the comparator (default val)
219 ADS1015_REG_CONFIG_CLAT_NONLAT
| // Non-latching (default val)
220 ADS1015_REG_CONFIG_CPOL_ACTVLOW
| // Alert/Rdy active low (default val)
221 ADS1015_REG_CONFIG_CMODE_TRAD
| // Traditional comparator (default val)
222 ADS1015_REG_CONFIG_DR_1600SPS
| // 1600 samples per second (default)
223 ADS1015_REG_CONFIG_MODE_SINGLE
; // Single-shot mode (default)
225 // Set PGA/voltage range
226 config
|= ADS1015_REG_CONFIG_PGA_6_144V
; // +/- 6.144V range (limited to VDD +0.3V max!)
229 config
|= ADS1015_REG_CONFIG_MUX_DIFF_0_1
; // AIN0 = P, AIN1 = N
231 // Set 'start single-conversion' bit
232 config
|= ADS1015_REG_CONFIG_OS_SINGLE
;
234 // Write config register to the ADC
235 error
= ads1015WriteRegister(ADS1015_REG_POINTER_CONFIG
, config
);
236 if (error
) return error
;
238 // Wait for the conversion to complete
241 // Read the conversion results
242 error
= ina219Read16(ADS1015_REG_POINTER_CONVERT
, value
);
243 if (error
) return error
;
245 // Shift results 4-bits to the right
246 *value
= *value
>> 4;
251 /**************************************************************************/
253 @brief Reads the 12-bit conversion results, measuring the voltage
254 difference between the P (AIN2) and N (AIN3) input. Generates
255 a signed 12-bit value since the difference can be either
256 positive or negative.
258 /**************************************************************************/
259 ads1015Error_t
ads1015ReadADC_Differential_2_3(int16_t *value
)
261 ads1015Error_t error
= ADS1015_ERROR_OK
;
263 if (!(_ads1015Initialised
))
268 // Start with default values
269 uint16_t config
= ADS1015_REG_CONFIG_CQUE_NONE
| // Disable the comparator (default val)
270 ADS1015_REG_CONFIG_CLAT_NONLAT
| // Non-latching (default val)
271 ADS1015_REG_CONFIG_CPOL_ACTVLOW
| // Alert/Rdy active low (default val)
272 ADS1015_REG_CONFIG_CMODE_TRAD
| // Traditional comparator (default val)
273 ADS1015_REG_CONFIG_DR_1600SPS
| // 1600 samples per second (default)
274 ADS1015_REG_CONFIG_MODE_SINGLE
; // Single-shot mode (default)
276 // Set PGA/voltage range
277 config
|= ADS1015_REG_CONFIG_PGA_6_144V
; // +/- 6.144V range (limited to VDD +0.3V max!)
280 config
|= ADS1015_REG_CONFIG_MUX_DIFF_2_3
; // AIN2 = P, AIN3 = N
282 // Set 'start single-conversion' bit
283 config
|= ADS1015_REG_CONFIG_OS_SINGLE
;
285 // Write config register to the ADC
286 error
= ads1015WriteRegister(ADS1015_REG_POINTER_CONFIG
, config
);
287 if (error
) return error
;
289 // Wait for the conversion to complete
292 // Read the conversion results
293 error
= ina219Read16(ADS1015_REG_POINTER_CONVERT
, value
);
294 if (error
) return error
;
296 // Shift results 4-bits to the right
297 *value
= *value
>> 4;
302 /**************************************************************************/
304 @brief Sets up the comparator to operate in basic mode, causing the
305 ALERT/RDY pin to assert (go from high to low) when the ADC
306 value exceeds the specified threshold.
308 This will also set the ADC in continuous conversion mode.
315 // Setup 3V comparator on channel 0
316 ads1015StartComparator_SingleEnded(0, 1000);
321 // Need to read to clear com bit once it's set
322 ads1015GetLastConversionResults(&results);
323 printf("%d\r\n", results);
327 /**************************************************************************/
328 ads1015Error_t
ads1015StartComparator_SingleEnded(uint8_t channel
, int16_t threshold
)
332 ads1015Error_t error
= ADS1015_ERROR_OK
;
334 if (!(_ads1015Initialised
))
339 // Start with default values
340 uint16_t config
= ADS1015_REG_CONFIG_CQUE_1CONV
| // Comparator enabled and asserts on 1 match
341 ADS1015_REG_CONFIG_CLAT_LATCH
| // Latching mode
342 ADS1015_REG_CONFIG_CPOL_ACTVLOW
| // Alert/Rdy active low (default val)
343 ADS1015_REG_CONFIG_CMODE_TRAD
| // Traditional comparator (default val)
344 ADS1015_REG_CONFIG_DR_1600SPS
| // 1600 samples per second (default)
345 ADS1015_REG_CONFIG_MODE_CONTIN
| // Continuous conversion mode
346 ADS1015_REG_CONFIG_PGA_6_144V
| // +/- 6.144V range (limited to VDD +0.3V max!)
347 ADS1015_REG_CONFIG_MODE_CONTIN
; // Continuous conversion mode
349 // Set single-ended input channel
353 config
|= ADS1015_REG_CONFIG_MUX_SINGLE_0
;
356 config
|= ADS1015_REG_CONFIG_MUX_SINGLE_1
;
359 config
|= ADS1015_REG_CONFIG_MUX_SINGLE_2
;
362 config
|= ADS1015_REG_CONFIG_MUX_SINGLE_3
;
366 // Set the high threshold register
367 error
= ads1015WriteRegister(ADS1015_REG_POINTER_HITHRESH
, threshold
<< 4);
368 if (error
) return error
;
370 // Write config register to the ADC
371 error
= ads1015WriteRegister(ADS1015_REG_POINTER_CONFIG
, config
);
372 if (error
) return error
;
375 /**************************************************************************/
377 @brief In order to clear the comparator, we need to read the
378 conversion results. This function reads the last conversion
379 results without changing the config value.
381 /**************************************************************************/
382 ads1015Error_t
ads1015GetLastConversionResults(int16_t *value
)
384 ads1015Error_t error
= ADS1015_ERROR_OK
;
386 if (!(_ads1015Initialised
))
391 // Wait for the conversion to complete
394 // Read the conversion results
395 error
= ina219Read16(ADS1015_REG_POINTER_CONVERT
, value
);
396 if (error
) return error
;
398 // Shift results 4-bits to the right
399 *value
= *value
>> 4;