ADC
9.2
Analog to Digital Conversor library for the Teensy 3.1/3.2 microprocessor
|
#include <ADC.h>
Classes | |
struct | Sync_result |
Struct for synchronous measurements. More... | |
Public Member Functions | |
ADC () | |
void | resetError () |
ADC settings | |
void | setReference (ADC_REFERENCE type) |
Set the voltage reference for all ADCs, default is vcc. More... | |
void | setResolution (uint8_t bits) |
Change the resolution for all ADCs. More... | |
void | setConversionSpeed (ADC_CONVERSION_SPEED speed) |
Sets the conversion speed for all ADCs. More... | |
void | setSamplingSpeed (ADC_SAMPLING_SPEED speed) |
Sets the sampling speed for all ADCs. More... | |
void | setAveraging (uint8_t num) |
Set the number of averages for all ADCs. More... | |
void | enableInterrupts (void(*isr)(void), uint8_t priority=255) |
Enable interrupts for all ADCs. More... | |
void | disableInterrupts () |
Disable interrupts for all ADCs. | |
void | enableDMA () |
Enable DMA request for all ADCs. More... | |
void | disableDMA () |
Disable DMA request for all ADCs. | |
void | enableCompare (int16_t compValue, bool greaterThan) |
Enable the compare function to a single value. More... | |
void | enableCompareRange (int16_t lowerLimit, int16_t upperLimit, bool insideRange, bool inclusive) |
Enable the compare function to a range. More... | |
void | disableCompare () |
Disable the compare function. More... | |
void | enablePGA (uint8_t gain) |
Enable and set PGA. More... | |
void | disablePGA () |
Disable PGA. | |
Blocking conversion methods | |
int | analogRead (uint8_t pin, int8_t adc_num=-1) |
Returns the analog value of the pin. More... | |
int | analogRead (ADC_INTERNAL_SOURCE pin, int8_t adc_num=-1) |
int | analogReadDifferential (uint8_t pinP, uint8_t pinN, int8_t adc_num=-1) |
Reads the differential analog value of two pins (pinP - pinN). More... | |
Non-blocking conversion methods | |
bool | startSingleRead (uint8_t pin, int8_t adc_num=-1) |
Starts an analog measurement on the pin and enables interrupts. More... | |
bool | startSingleDifferential (uint8_t pinP, uint8_t pinN, int8_t adc_num=-1) |
int | readSingle (int8_t adc_num=-1) |
Reads the analog value of a single conversion. More... | |
Continuous conversion methods | |
bool | startContinuous (uint8_t pin, int8_t adc_num=-1) |
Starts continuous conversion on the pin. More... | |
bool | startContinuousDifferential (uint8_t pinP, uint8_t pinN, int8_t adc_num=-1) |
Starts continuous conversion between the pins (pinP-pinN). More... | |
int | analogReadContinuous (int8_t adc_num=-1) |
Reads the analog value of a continuous conversion. More... | |
void | stopContinuous (int8_t adc_num=-1) |
Stops continuous conversion. More... | |
Synchronized conversion methods | |
ADC0 and ADC1 will measure simultaneously | |
Sync_result | analogSynchronizedRead (uint8_t pin0, uint8_t pin1) |
Sync_result | analogSyncRead (uint8_t pin0, uint8_t pin1) |
Same as analogSynchronizedRead. More... | |
Sync_result | analogSynchronizedReadDifferential (uint8_t pin0P, uint8_t pin0N, uint8_t pin1P, uint8_t pin1N) |
Sync_result | analogSyncReadDifferential (uint8_t pin0P, uint8_t pin0N, uint8_t pin1P, uint8_t pin1N) |
Same as analogSynchronizedReadDifferential. More... | |
bool | startSynchronizedSingleRead (uint8_t pin0, uint8_t pin1) |
Starts an analog measurement at the same time on the two ADC modules. More... | |
bool | startSynchronizedSingleDifferential (uint8_t pin0P, uint8_t pin0N, uint8_t pin1P, uint8_t pin1N) |
Sync_result | readSynchronizedSingle () |
Reads the analog value of a single conversion. More... | |
bool | startSynchronizedContinuous (uint8_t pin0, uint8_t pin1) |
Starts a continuous conversion in both ADCs simultaneously. More... | |
bool | startSynchronizedContinuousDifferential (uint8_t pin0P, uint8_t pin0N, uint8_t pin1P, uint8_t pin1N) |
Starts a continuous differential conversion in both ADCs simultaneously. More... | |
Sync_result | readSynchronizedContinuous () |
Returns the values of both ADCs. More... | |
void | stopSynchronizedContinuous () |
Stops synchronous continuous conversion. | |
Public Attributes | |
ADC_Module *const | adc0 = &adc0_obj |
Object to control the ADC0. | |
ADC_Module *const | adc1 = &adc1_obj |
Object to control the ADC1. | |
ADC_Module *const | adc [ADC_NUM_ADCS] = {adc0, adc1} |
Array with the ADC Modules. | |
Static Public Attributes | |
static const uint8_t | channel2sc1aADC0 [ADC_MAX_PIN+1] |
Translate pin number to SC1A nomenclature. More... | |
static const uint8_t | channel2sc1aADC1 [ADC_MAX_PIN+1] |
Translate pin number to SC1A nomenclature. More... | |
static const uint8_t | sc1a2channelADC0 [ADC_MAX_PIN+1] |
Translate pin number to SC1A nomenclature for differential pins. More... | |
static const uint8_t | sc1a2channelADC1 [ADC_MAX_PIN+1] |
Translate pin number to SC1A nomenclature for differential pins. More... | |
static const ADC_Module::ADC_NLIST | diff_table_ADC0 [ADC_DIFF_PAIRS] |
Translate differential pin number to SC1A nomenclature. More... | |
static const ADC_Module::ADC_NLIST | diff_table_ADC1 [ADC_DIFF_PAIRS] |
Translate differential pin number to SC1A nomenclature. More... | |
|
inline |
int ADC::analogRead | ( | uint8_t | pin, |
int8_t | adc_num = -1 |
||
) |
Returns the analog value of the pin.
It waits until the value is read and then returns the result. If a comparison has been set up and fails, it will return ADC_ERROR_VALUE. This function is interrupt safe, so it will restore the adc to the state it was before being called If more than one ADC exists, it will select the module with less workload, you can force a selection using adc_num. If you select ADC1 in a board with just one ADC, it will return ADC_ERROR_VALUE.
pin | can be any of the analog pins |
adc_num | ADC_X ADC module |
int ADC::analogReadContinuous | ( | int8_t | adc_num = -1 | ) |
Reads the analog value of a continuous conversion.
Set the continuous conversion with with analogStartContinuous(pin) or startContinuousDifferential(pinP, pinN). If single-ended and 16 bits it's necessary to typecast it to an unsigned type (like uint16_t), otherwise values larger than 3.3/2 V are interpreted as negative!
adc_num | ADC_X ADC module |
Set the continuous conversion with with analogStartContinuous(pin) or startContinuousDifferential(pinP, pinN).
int ADC::analogReadDifferential | ( | uint8_t | pinP, |
uint8_t | pinN, | ||
int8_t | adc_num = -1 |
||
) |
Reads the differential analog value of two pins (pinP - pinN).
It waits until the value is read and then returns the result. This function is interrupt safe, so it will restore the adc to the state it was before being called If more than one ADC exists, it will select the module with less workload, you can force a selection using adc_num
pinP | must be A10 or A12. |
pinN | must be A11 (if pinP=A10) or A13 (if pinP=A12). |
adc_num | ADC_X ADC module |
ADC::Sync_result ADC::analogSynchronizedRead | ( | uint8_t | pin0, |
uint8_t | pin1 | ||
) |
Returns the analog values of both pins, measured at the same time by the two ADC modules. It waits until the values are read and then returns the result as a struct Sync_result, use Sync_result.result_adc0 and Sync_result.result_adc1. If a comparison has been set up and fails, it will return ADC_ERROR_VALUE in both fields of the struct. This function is interrupt safe, so it will restore the adc to the state it was before being called
pin0 | pin in ADC0 |
pin1 | pin in ADC1 |
ADC::Sync_result ADC::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 modules. It waits until the values are read and then returns the result as a struct Sync_result, use Sync_result.result_adc0 and Sync_result.result_adc1. If a comparison has been set up and fails, it will return ADC_ERROR_VALUE in both fields of the struct. This function is interrupt safe, so it will restore the adc to the state it was before being called
pin0P | positive pin in ADC0 |
pin0N | negative pin in ADC0 |
pin1P | positive pin in ADC1 |
pin1N | negative pin in ADC1 |
|
inline |
Same as analogSynchronizedRead.
pin0 | pin in ADC0 |
pin1 | pin in ADC1 |
|
inline |
Same as analogSynchronizedReadDifferential.
pin0P | positive pin in ADC0 |
pin0N | negative pin in ADC0 |
pin1P | positive pin in ADC1 |
pin1N | negative pin in ADC1 |
|
inline |
|
inline |
Enable the compare function to a single value.
A conversion will be completed only when the ADC value is >= compValue (greaterThan=1) or < compValue (greaterThan=0) Call it after changing the resolution Use with interrupts or poll conversion completion with isComplete()
compValue | value to compare |
greaterThan | true or false |
|
inline |
Enable the compare function to a range.
A conversion will be completed only when the ADC value is inside (insideRange=1) or outside (=0) the range given by (lowerLimit, upperLimit),including (inclusive=1) the limits or not (inclusive=0). See Table 31-78, p. 617 of the freescale manual. Call it after changing the resolution Use with interrupts or poll conversion completion with isComplete()
lowerLimit | lower value to compare |
upperLimit | upper value to compare |
insideRange | true or false |
inclusive | true or false |
|
inline |
|
inline |
Enable interrupts for all ADCs.
An IRQ_ADCx Interrupt will be raised when the conversion is completed (including hardware averages and if the comparison (if any) is true).
isr | function (returns void and accepts no arguments) that will be executed after an interrupt. |
priority | Interrupt priority, highest is 0, lowest is 255. |
|
inline |
int ADC::readSingle | ( | int8_t | adc_num = -1 | ) |
ADC::Sync_result ADC::readSynchronizedContinuous | ( | ) |
ADC::Sync_result ADC::readSynchronizedSingle | ( | ) |
|
inline |
|
inline |
|
inline |
Sets the conversion speed for all ADCs.
speed | can be any from the ADC_CONVERSION_SPEED enum. |
|
inline |
Set the voltage reference for all ADCs, default is vcc.
type | Any of ADC_REFERENCE |
|
inline |
Change the resolution for all ADCs.
bits | is the number of bits of resolution. For single-ended measurements: 8, 10, 12 or 16 bits. For differential measurements: 9, 11, 13 or 16 bits. If you want something in between (11 bits single-ended for example) select the immediate higher and shift the result one to the right. Whenever you change the resolution, change also the comparison values (if you use them). |
|
inline |
Sets the sampling speed for all ADCs.
Increase the sampling speed for low impedance sources, decrease it for higher impedance ones.
speed | can be any of the ADC_SAMPLING_SPEED. |
bool ADC::startContinuous | ( | uint8_t | pin, |
int8_t | adc_num = -1 |
||
) |
Starts continuous conversion on the pin.
It returns as soon as the ADC is set, use analogReadContinuous() to read the value.
pin | can be any of the analog pins |
adc_num | ADC_X ADC module |
bool ADC::startContinuousDifferential | ( | uint8_t | pinP, |
uint8_t | pinN, | ||
int8_t | adc_num = -1 |
||
) |
Starts continuous conversion between the pins (pinP-pinN).
It returns as soon as the ADC is set, use analogReadContinuous() to read the value.
pinP | must be A10 or A12. |
pinN | must be A11 (if pinP=A10) or A13 (if pinP=A12). |
adc_num | ADC_X ADC module |
bool ADC::startSingleDifferential | ( | uint8_t | pinP, |
uint8_t | pinN, | ||
int8_t | adc_num = -1 |
||
) |
Start a differential conversion between two pins (pinP - pinN) and enables interrupts. It returns immediately, get value with readSingle(). If this function interrupts a measurement, it stores the settings in adc_config
pinP | must be A10 or A12. |
pinN | must be A11 (if pinP=A10) or A13 (if pinP=A12). |
adc_num | ADC_X ADC module |
bool ADC::startSingleRead | ( | uint8_t | pin, |
int8_t | adc_num = -1 |
||
) |
Starts an analog measurement on the pin and enables interrupts.
It returns immediately, get value with readSingle(). If this function interrupts a measurement, it stores the settings in adc_config
pin | can be any of the analog pins |
adc_num | ADC_X ADC module |
bool ADC::startSynchronizedContinuous | ( | uint8_t | pin0, |
uint8_t | pin1 | ||
) |
bool ADC::startSynchronizedContinuousDifferential | ( | uint8_t | pin0P, |
uint8_t | pin0N, | ||
uint8_t | pin1P, | ||
uint8_t | pin1N | ||
) |
Starts a continuous differential conversion in both ADCs simultaneously.
Use readSynchronizedContinuous to get the values
pin0P | positive pin in ADC0 |
pin0N | negative pin in ADC0 |
pin1P | positive pin in ADC1 |
pin1N | negative pin in ADC1 |
Use readSynchronizedContinuous to get the values
bool ADC::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) It returns immediately, get value with readSynchronizedSingle(). If this function interrupts a measurement, it stores the settings in adc_config
pin0P | positive pin in ADC0 |
pin0N | negative pin in ADC0 |
pin1P | positive pin in ADC1 |
pin1N | negative pin in ADC1 |
bool ADC::startSynchronizedSingleRead | ( | uint8_t | pin0, |
uint8_t | pin1 | ||
) |
Starts an analog measurement at the same time on the two ADC modules.
It returns immediately, get value with readSynchronizedSingle(). If this function interrupts a measurement, it stores the settings in adc_config
pin0 | pin in ADC0 |
pin1 | pin in ADC1 |
void ADC::stopContinuous | ( | int8_t | adc_num = -1 | ) |
|
static |
Translate pin number to SC1A nomenclature.
|
static |
Translate pin number to SC1A nomenclature.
|
static |
|
static |
|
static |
|
static |