ADC: ADC.h Source File

ADC 8.0 Analog to Digital Conversor library for the Teensy 3.1/3.2 microprocessor

ADC.h
1 /* Teensy 4, 3.x, LC ADC library
2 * https://github.com/pedvide/ADC
3 * Copyright (c) 2019 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 /* TODO
27 * - Function to measure more that 1 pin consecutively (stream?)
28 *
29 * bugs:
30 * - comparison values in 16 bit differential mode are twice what they should be
31 */
32
51 #ifndef ADC_H
52 #define ADC_H
53
54 #define ADC_0 0
55 #define ADC_1 1
56 //enum class ADC_NUM {ADC_0, ADC_1}; // too verbose, but it'd avoid some mistakes
57
58 // include ADC module class
59 #include "ADC_Module.h"
60
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64
68 class ADC
69 {
70 protected:
71 private:
72
73 // ADCs objects
74 ADC_Module adc0_obj;
75 #ifdef ADC_DUAL_ADCS
76 ADC_Module adc1_obj;
77 #endif
78
80 const uint8_t num_ADCs = ADC_NUM_ADCS;
81
82
83 public:
84
86 ADC();
87
88
89 // create both adc objects
90
92 ADC_Module *const adc0 = &adc0_obj; // adc object pointer
93 #ifdef ADC_DUAL_ADCS
94 ADC_Module *const adc1 = &adc1_obj; // adc object pointer
96 #endif
97
98 #ifdef ADC_SINGLE_ADC
99 ADC_Module *const adc[ADC_NUM_ADCS] = {adc0};
101 #else
102 ADC_Module *const adc[ADC_NUM_ADCS] = {adc0, adc1};
104 #endif
105
106
108
110
119 int analogRead(uint8_t pin, int8_t adc_num = -1);
120
122
133 int analogRead(ADC_INTERNAL_SOURCE pin, int8_t adc_num = -1) __attribute__((always_inline)) {
134 return analogRead(static_cast<uint8_t>(pin), adc_num);
135 }
136
137 #if ADC_DIFF_PAIRS > 0
138
149 int analogReadDifferential(uint8_t pinP, uint8_t pinN, int8_t adc_num = -1);
150 #endif
151
152
154
156
162 bool startSingleRead(uint8_t pin, int8_t adc_num = -1);
163
164 #if ADC_DIFF_PAIRS > 0
165
173 bool startSingleDifferential(uint8_t pinP, uint8_t pinN, int8_t adc_num = -1);
174 #endif
175
177
181 int readSingle(int8_t adc_num = -1);
182
183
184
186
188
193 bool startContinuous(uint8_t pin, int8_t adc_num = -1);
194
195 #if ADC_DIFF_PAIRS > 0
196
203 bool startContinuousDifferential(uint8_t pinP, uint8_t pinN, int8_t adc_num = -1);
204 #endif
205
207
213 int analogReadContinuous(int8_t adc_num = -1);
214
216
219 void stopContinuous(int8_t adc_num = -1);
220
221
222
225 #ifdef ADC_DUAL_ADCS
226
228
230 struct Sync_result{
231 int32_t result_adc0, result_adc1;
232 };
233
235
237
245 Sync_result analogSynchronizedRead(uint8_t pin0, uint8_t pin1);
246
248
253 Sync_result analogSyncRead(uint8_t pin0, uint8_t pin1) __attribute__((always_inline)) {return analogSynchronizedRead(pin0, pin1);}
254
255 #if ADC_DIFF_PAIRS > 0
256
267 Sync_result analogSynchronizedReadDifferential(uint8_t pin0P, uint8_t pin0N, uint8_t pin1P, uint8_t pin1N);
268
270
277 Sync_result analogSyncReadDifferential(uint8_t pin0P, uint8_t pin0N, uint8_t pin1P, uint8_t pin1N) __attribute__((always_inline)) {
278 return analogSynchronizedReadDifferential(pin0P, pin0N, pin1P, pin1N);
279 }
280 #endif
281
283
285
291 bool startSynchronizedSingleRead(uint8_t pin0, uint8_t pin1);
292
293 #if ADC_DIFF_PAIRS > 0
294
303 bool startSynchronizedSingleDifferential(uint8_t pin0P, uint8_t pin0N, uint8_t pin1P, uint8_t pin1N);
304 #endif
305
307
310 Sync_result readSynchronizedSingle();
311
312
314
316
321 bool startSynchronizedContinuous(uint8_t pin0, uint8_t pin1);
322
323 #if ADC_DIFF_PAIRS > 0
324
332 bool startSynchronizedContinuousDifferential(uint8_t pin0P, uint8_t pin0N, uint8_t pin1P, uint8_t pin1N);
333 #endif
334
336
339 Sync_result readSynchronizedContinuous();
340
342 void stopSynchronizedContinuous();
343
344 #endif
345
346
349 void resetError() {
350 for(int i=0; i< ADC_NUM_ADCS; i++) {
351 adc[i]->resetError();
352 }
353 }
354
355
357 // should this be a constexpr?
358 static const uint8_t channel2sc1aADC0[ADC_MAX_PIN+1];
359 #ifdef ADC_DUAL_ADCS
360 static const uint8_t channel2sc1aADC1[ADC_MAX_PIN+1];
362 #endif
363
365 static const uint8_t sc1a2channelADC0[ADC_MAX_PIN+1];
366 #ifdef ADC_DUAL_ADCS
367 static const uint8_t sc1a2channelADC1[ADC_MAX_PIN+1];
369 #endif
370
371
372 #if ADC_DIFF_PAIRS > 0
373 static const ADC_Module::ADC_NLIST diff_table_ADC0[ADC_DIFF_PAIRS];
375 #ifdef ADC_DUAL_ADCS
376 static const ADC_Module::ADC_NLIST diff_table_ADC1[ADC_DIFF_PAIRS];
378 #endif
379 #endif
380
381
382 };
383
384
385
386
387 #ifdef __cplusplus
388 }
389 #endif
390
391
392 #endif // ADC_H
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.
ADC::adc0
ADC_Module *const adc0
Object to control the ADC0.
Definition: ADC.h:92
ADC::readSynchronizedContinuous
Sync_result readSynchronizedContinuous()
Returns the values of both ADCs.
ADC::adc
ADC_Module *const adc[ADC_NUM_ADCS]
Array with the ADC Modules.
Definition: ADC.h:103
ADC::startSynchronizedContinuous
bool startSynchronizedContinuous(uint8_t pin0, uint8_t pin1)
Starts a continuous conversion in both ADCs simultaneously.
ADC::diff_table_ADC1
static const ADC_Module::ADC_NLIST diff_table_ADC1[ADC_DIFF_PAIRS]
Translate differential pin number to SC1A nomenclature.
Definition: ADC.h:377
ADC::startContinuousDifferential
bool startContinuousDifferential(uint8_t pinP, uint8_t pinN, int8_t adc_num=-1)
Starts continuous conversion between the pins (pinP-pinN).
ADC::analogReadContinuous
int analogReadContinuous(int8_t adc_num=-1)
Reads the analog value of a continuous conversion.
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...
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.
ADC::startContinuous
bool startContinuous(uint8_t pin, int8_t adc_num=-1)
Starts continuous conversion on the pin.
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.
ADC_Module::resetError
void resetError()
Resets all errors from the ADC, if any.
Definition: ADC_Module.h:659
ADC::readSingle
int readSingle(int8_t adc_num=-1)
Reads the analog value of a single conversion.
ADC::channel2sc1aADC0
static const uint8_t channel2sc1aADC0[ADC_MAX_PIN+1]
Translate pin number to SC1A nomenclature.
Definition: ADC.h:358
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)
ADC::diff_table_ADC0
static const ADC_Module::ADC_NLIST diff_table_ADC0[ADC_DIFF_PAIRS]
Translate differential pin number to SC1A nomenclature.
Definition: ADC.h:374
ADC::readSynchronizedSingle
Sync_result readSynchronizedSingle()
Reads the analog value of a single conversion.
ADC::analogRead
int analogRead(ADC_INTERNAL_SOURCE pin, int8_t adc_num=-1)
Returns the analog value of the special internal source, such as the temperature sensor.
Definition: ADC.h:133
ADC_Module
Definition: ADC_Module.h:54
ADC::sc1a2channelADC1
static const uint8_t sc1a2channelADC1[ADC_MAX_PIN+1]
Translate pin number to SC1A nomenclature for differential pins.
Definition: ADC.h:368
ADC::analogSyncRead
Sync_result analogSyncRead(uint8_t pin0, uint8_t pin1)
Same as analogSynchronizedRead.
Definition: ADC.h:253
ADC::startSynchronizedSingleRead
bool startSynchronizedSingleRead(uint8_t pin0, uint8_t pin1)
Starts an analog measurement at the same time on the two ADC modules.
ADC
Definition: ADC.h:68
ADC::adc1
ADC_Module *const adc1
Object to control the ADC1.
Definition: ADC.h:95
ADC::channel2sc1aADC1
static const uint8_t channel2sc1aADC1[ADC_MAX_PIN+1]
Translate pin number to SC1A nomenclature.
Definition: ADC.h:361
ADC::stopSynchronizedContinuous
void stopSynchronizedContinuous()
Stops synchronous continuous conversion.
ADC::ADC
ADC()
ADC_settings::ADC_INTERNAL_SOURCE
ADC_INTERNAL_SOURCE
Definition: settings_defines.h:221
ADC::stopContinuous
void stopContinuous(int8_t adc_num=-1)
Stops continuous conversion.
ADC::resetError
void resetError()
Definition: ADC.h:349
ADC::analogRead
int analogRead(uint8_t pin, int8_t adc_num=-1)
Returns the analog value of the pin.
ADC::Sync_result
Struct for synchronous measurements.
Definition: ADC.h:230
ADC::sc1a2channelADC0
static const uint8_t sc1a2channelADC0[ADC_MAX_PIN+1]
Translate pin number to SC1A nomenclature for differential pins.
Definition: ADC.h:365
ADC::analogSyncReadDifferential
Sync_result analogSyncReadDifferential(uint8_t pin0P, uint8_t pin0N, uint8_t pin1P, uint8_t pin1N)
Same as analogSynchronizedReadDifferential.
Definition: ADC.h:277
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).
ADC::startSingleRead
bool startSingleRead(uint8_t pin, int8_t adc_num=-1)
Starts an analog measurement on the pin and enables interrupts.