ADC 7.0

- Main Page
- Related Pages
- Classes
- Files

- File List

All Classes Functions Variables Pages
ADC.h
1 /* Teensy 3.x, LC ADC library
2 * https://github.com/pedvide/ADC
3 * Copyright (c) 2015 Pedro Villanueva
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 */
25
26 /* ADC.h: Control for one (Teensy 3.0, LC) or two ADC modules (Teensy 3.1).
27 *
28 */
29
30 /* TODO
31 * - Function to measure more that 1 pin consecutively (stream?)
32 *
33 * bugs:
34 * - comparison values in 16 bit differential mode are twice what they should be
35 */
36
37 #ifndef ADC_H
38 #define ADC_H
39
40 #define ADC_0 0
41 #define ADC_1 1
42
43 // include ADC module class
44 #include "ADC_Module.h"
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
53 class ADC
54 {
55 public:
56
58 ADC();
59
60
61 // create both adc objects
62
64 ADC_Module *adc0; // adc object
65 #if ADC_NUM_ADCS>1
66 ADC_Module *adc1; // adc object
68 #endif
69
70
72
74
79 void setReference(uint8_t type, int8_t adc_num = -1);
80
81
83
92 void setResolution(uint8_t bits, int8_t adc_num = -1);
93
95 uint8_t getResolution(int8_t adc_num = -1);
96
98 uint32_t getMaxValue(int8_t adc_num = -1);
99
100
102
119 void setConversionSpeed(uint8_t speed, int8_t adc_num = -1);
120
121
123
132 void setSamplingSpeed(uint8_t speed, int8_t adc_num = -1);
133
134
136
139 void setAveraging(uint8_t num, int8_t adc_num = -1);
140
141
143
146 void enableInterrupts(int8_t adc_num = -1);
147
149 void disableInterrupts(int8_t adc_num = -1);
150
151
153
156 void enableDMA(int8_t adc_num = -1);
157
159 void disableDMA(int8_t adc_num = -1);
160
161
163
168 void enableCompare(int16_t compValue, bool greaterThan, int8_t adc_num = -1);
169
171
177 void enableCompareRange(int16_t lowerLimit, int16_t upperLimit, bool insideRange, bool inclusive, int8_t adc_num = -1);
178
180 void disableCompare(int8_t adc_num = -1);
181
182
184
189 void enablePGA(uint8_t gain, int8_t adc_num = -1);
190
192
194 uint8_t getPGA(int8_t adc_num = -1);
195
197 void disablePGA(int8_t adc_num = -1);
198
199
200
202
204 bool isConverting(int8_t adc_num = -1);
205
207
212 bool isComplete(int8_t adc_num = -1);
213
215 bool isDifferential(int8_t adc_num = -1);
216
218 bool isContinuous(int8_t adc_num = -1);
219
220
221
223
225
231 int analogRead(uint8_t pin, int8_t adc_num = -1);
232
234
244 int analogReadDifferential(uint8_t pinP, uint8_t pinN, int8_t adc_num = -1);
245
246
248
250
254 bool startSingleRead(uint8_t pin, int8_t adc_num = -1);
255
257
264 bool startSingleDifferential(uint8_t pinP, uint8_t pinN, int8_t adc_num = -1);
265
267
270 int readSingle(int8_t adc_num = -1);
271
272
273
275
277
279 bool startContinuous(uint8_t pin, int8_t adc_num = -1);
280
282
287 bool startContinuousDifferential(uint8_t pinP, uint8_t pinN, int8_t adc_num = -1);
288
290
295 int analogReadContinuous(int8_t adc_num = -1);
296
298 void stopContinuous(int8_t adc_num = -1);
299
300
301
304
306
308 struct Sync_result{
309 int32_t result_adc0, result_adc1;
310 };
311
313
315
320 Sync_result analogSynchronizedRead(uint8_t pin0, uint8_t pin1);
321
322 Sync_result analogSyncRead(uint8_t pin0, uint8_t pin1) __attribute__((always_inline)) {return analogSynchronizedRead(pin0, pin1);}
323
325
330 Sync_result analogSynchronizedReadDifferential(uint8_t pin0P, uint8_t pin0N, uint8_t pin1P, uint8_t pin1N);
331
332 Sync_result analogSyncReadDifferential(uint8_t pin0P, uint8_t pin0N, uint8_t pin1P, uint8_t pin1N) __attribute__((always_inline)) {
333 return analogSynchronizedReadDifferential(pin0P, pin0N, pin1P, pin1N);
334 }
335
337
339
343 bool startSynchronizedSingleRead(uint8_t pin0, uint8_t pin1);
344
346
352 bool startSynchronizedSingleDifferential(uint8_t pin0P, uint8_t pin0N, uint8_t pin1P, uint8_t pin1N);
353
355
358 Sync_result readSynchronizedSingle();
359
360
362
364
367 bool startSynchronizedContinuous(uint8_t pin0, uint8_t pin1);
368
370
373 bool startSynchronizedContinuousDifferential(uint8_t pin0P, uint8_t pin0N, uint8_t pin1P, uint8_t pin1N);
374
376 Sync_result readSynchronizedContinuous();
377
379 void stopSynchronizedContinuous();
380
381
383 // should this be a constexpr?
384 static const uint8_t channel2sc1aADC0[44];
385 #if ADC_NUM_ADCS>1
386 static const uint8_t channel2sc1aADC1[44];
388 #endif
389
391
393 static const uint8_t channel2sc1a_diff_ADC0[4];
394 #if ADC_NUM_ADCS>1
395 static const uint8_t channel2sc1a_diff_ADC1[4];
396 #endif
397
399 static const uint8_t sc1a2channelADC0[44];
400 #if ADC_NUM_ADCS>1
401 static const uint8_t sc1a2channelADC1[44];
402 #endif
403
404
405 protected:
406 private:
407
408 const uint8_t num_ADCs = ADC_NUM_ADCS;
409
410 };
411
412
413 #ifdef __cplusplus
414 }
415 #endif
416
417
418 #endif // ADC_H
ADC::disableCompare
void disableCompare(int8_t adc_num=-1)
Disable the compare function.
Definition: ADC.cpp:367
ADC::disableDMA
void disableDMA(int8_t adc_num=-1)
Disable ADC DMA request.
Definition: ADC.cpp:313
ADC::readSynchronizedContinuous
Sync_result readSynchronizedContinuous()
Returns the values of both ADCs.
Definition: ADC.cpp:1201
ADC::isDifferential
bool isDifferential(int8_t adc_num=-1)
Is the ADC in differential mode?
Definition: ADC.cpp:461
ADC::getResolution
uint8_t getResolution(int8_t adc_num=-1)
Returns the resolution of the ADC_Module.
Definition: ADC.cpp:176
ADC::stopSynchronizedContinuous
void stopSynchronizedContinuous()
Stops synchronous continuous conversion.
Definition: ADC.cpp:1211
ADC::analogReadDifferential
int analogReadDifferential(uint8_t pinP, uint8_t pinN, int8_t adc_num=-1)
Reads the differential analog value of two pins (pinP - pinN).
Definition: ADC.cpp:548
ADC::ADC
ADC()
Definition: ADC.cpp:116
ADC::isConverting
bool isConverting(int8_t adc_num=-1)
Is the ADC converting at the moment?
Definition: ADC.cpp:430
ADC::Sync_result
Struct for synchronous measurements.
Definition: ADC.h:308
ADC::stopContinuous
void stopContinuous(int8_t adc_num=-1)
Stops continuous conversion.
Definition: ADC.cpp:830
ADC::startContinuous
bool startContinuous(uint8_t pin, int8_t adc_num=-1)
Starts continuous conversion on the pin.
Definition: ADC.cpp:716
ADC::readSingle
int readSingle(int8_t adc_num=-1)
Reads the analog value of a single conversion.
Definition: ADC.cpp:700
ADC::adc0
ADC_Module * adc0
Object to control the ADC0.
Definition: ADC.h:64
ADC::startSingleDifferential
bool startSingleDifferential(uint8_t pinP, uint8_t pinN, int8_t adc_num=-1)
Start a differential conversion between two pins (pinP - pinN) and enables interrupts.
Definition: ADC.cpp:652
ADC::isComplete
bool isComplete(int8_t adc_num=-1)
Is an ADC conversion ready?
Definition: ADC.cpp:448
ADC::startContinuousDifferential
bool startContinuousDifferential(uint8_t pinP, uint8_t pinN, int8_t adc_num=-1)
Starts continuous conversion between the pins (pinP-pinN).
Definition: ADC.cpp:767
ADC::analogRead
int analogRead(uint8_t pin, int8_t adc_num=-1)
Returns the analog value of the pin.
Definition: ADC.cpp:494
ADC_Module
Definition: ADC_Module.h:359
ADC::startSynchronizedSingleDifferential
bool startSynchronizedSingleDifferential(uint8_t pin0P, uint8_t pin0N, uint8_t pin1P, uint8_t pin1N)
Start a differential conversion between two pins (pin0P - pin0N) and (pin1P - pin1N) ...
Definition: ADC.cpp:1076
ADC::sc1a2channelADC0
static const uint8_t sc1a2channelADC0[44]
Translate SC1A to pin number.
Definition: ADC.h:399
ADC
Definition: ADC.h:53
ADC::enableInterrupts
void enableInterrupts(int8_t adc_num=-1)
Enable interrupts.
Definition: ADC.cpp:267
ADC::analogSynchronizedReadDifferential
Sync_result analogSynchronizedReadDifferential(uint8_t pin0P, uint8_t pin0N, uint8_t pin1P, uint8_t pin1N)
Returns the differential analog values of both sets of pins, measured at the same time by the two ADC...
Definition: ADC.cpp:941
ADC::startSingleRead
bool startSingleRead(uint8_t pin, int8_t adc_num=-1)
Starts an analog measurement on the pin and enables interrupts.
Definition: ADC.cpp:600
ADC::enableCompareRange
void enableCompareRange(int16_t lowerLimit, int16_t upperLimit, bool insideRange, bool inclusive, int8_t adc_num=-1)
Enable the compare function to a range.
Definition: ADC.cpp:353
ADC::startSynchronizedContinuousDifferential
bool startSynchronizedContinuousDifferential(uint8_t pin0P, uint8_t pin0N, uint8_t pin1P, uint8_t pin1N)
Starts a continuous differential conversion in both ADCs simultaneously.
Definition: ADC.cpp:1171
ADC::setResolution
void setResolution(uint8_t bits, int8_t adc_num=-1)
Change the resolution of the measurement.
Definition: ADC.cpp:162
ADC::readSynchronizedSingle
Sync_result readSynchronizedSingle()
Reads the analog value of a single conversion.
Definition: ADC.cpp:1123
ADC::channel2sc1aADC1
static const uint8_t channel2sc1aADC1[44]
Translate pin number to SC1A nomenclature.
Definition: ADC.h:387
ADC::enablePGA
void enablePGA(uint8_t gain, int8_t adc_num=-1)
Enable and set PGA.
Definition: ADC.cpp:387
ADC::channel2sc1aADC0
static const uint8_t channel2sc1aADC0[44]
Translate pin number to SC1A nomenclature.
Definition: ADC.h:384
ADC::startSynchronizedSingleRead
bool startSynchronizedSingleRead(uint8_t pin0, uint8_t pin1)
Starts an analog measurement at the same time on the two ADC modules.
Definition: ADC.cpp:1025
ADC::disablePGA
void disablePGA(int8_t adc_num=-1)
Disable PGA.
Definition: ADC.cpp:416
ADC::startSynchronizedContinuous
bool startSynchronizedContinuous(uint8_t pin0, uint8_t pin1)
Starts a continuous conversion in both ADCs simultaneously.
Definition: ADC.cpp:1139
ADC::setAveraging
void setAveraging(uint8_t num, int8_t adc_num=-1)
Set the number of averages.
Definition: ADC.cpp:249
ADC::getPGA
uint8_t getPGA(int8_t adc_num=-1)
Returns the PGA level.
Definition: ADC.cpp:403
ADC::getMaxValue
uint32_t getMaxValue(int8_t adc_num=-1)
Returns the maximum value for a measurement: 2^res-1.
Definition: ADC.cpp:190
ADC::disableInterrupts
void disableInterrupts(int8_t adc_num=-1)
Disable interrupts.
Definition: ADC.cpp:281
ADC::analogReadContinuous
int analogReadContinuous(int8_t adc_num=-1)
Reads the analog value of a continuous conversion.
Definition: ADC.cpp:817
ADC::enableCompare
void enableCompare(int16_t compValue, bool greaterThan, int8_t adc_num=-1)
Enable the compare function to a single value.
Definition: ADC.cpp:333
ADC::analogSynchronizedRead
Sync_result analogSynchronizedRead(uint8_t pin0, uint8_t pin1)
Returns the analog values of both pins, measured at the same time by the two ADC modules.
Definition: ADC.cpp:856
ADC::setSamplingSpeed
void setSamplingSpeed(uint8_t speed, int8_t adc_num=-1)
Sets the sampling speed.
Definition: ADC.cpp:230
ADC::setConversionSpeed
void setConversionSpeed(uint8_t speed, int8_t adc_num=-1)
Sets the conversion speed (changes the ADC clock, ADCK)
Definition: ADC.cpp:209
ADC::adc1
ADC_Module * adc1
Object to control the ADC1.
Definition: ADC.h:67
ADC::enableDMA
void enableDMA(int8_t adc_num=-1)
Enable DMA request.
Definition: ADC.cpp:299
ADC::isContinuous
bool isContinuous(int8_t adc_num=-1)
Is the ADC in continuous mode?
Definition: ADC.cpp:474
ADC::channel2sc1a_diff_ADC0
static const uint8_t channel2sc1a_diff_ADC0[4]
Translate pin number to SC1A nomenclature for differential pins.
Definition: ADC.h:393
ADC::setReference
void setReference(uint8_t type, int8_t adc_num=-1)
Set the voltage reference you prefer, default is 3.3 V (VCC)
Definition: ADC.cpp:137