ADC  9.2
Analog to Digital Conversor library for the Teensy LC microprocessor
ADC Class Reference

#include <ADC.h>

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...
 
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...
 

Public Attributes

ADC_Module *const adc0 = &adc0_obj
 Object to control the ADC0.
 
ADC_Module *const adc [ADC_NUM_ADCS] = {adc0}
 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 sc1a2channelADC0 [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...
 

Detailed Description

Class ADC: Controls the Teensy 3.x, 4 ADC

Definition at line 69 of file ADC.h.

Constructor & Destructor Documentation

◆ ADC()

ADC::ADC ( )

Default constructor

Definition at line 257 of file ADC.cpp.

Member Function Documentation

◆ analogRead() [1/2]

int ADC::analogRead ( ADC_INTERNAL_SOURCE  pin,
int8_t  adc_num = -1 
)
inline

Returns the analog value of the special internal source, such as the temperature sensor.

Parameters
pinADC_INTERNAL_SOURCE to read.
adc_numADC_X ADC module
Returns
the value of the pin.

Definition at line 323 of file ADC.h.

◆ analogRead() [2/2]

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.

Parameters
pincan be any of the analog pins
adc_numADC_X ADC module
Returns
the analog value of the pin.

Definition at line 284 of file ADC.cpp.

◆ analogReadContinuous()

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!

Parameters
adc_numADC_X ADC module
Returns
the last converted value.

Set the continuous conversion with with analogStartContinuous(pin) or startContinuousDifferential(pinP, pinN).

Returns
the last converted value. 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!

Definition at line 660 of file ADC.cpp.

◆ analogReadDifferential()

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

Parameters
pinPmust be A10 or A12.
pinNmust be A11 (if pinP=A10) or A13 (if pinP=A12).
adc_numADC_X ADC module
Returns
the differential value of the pins, invalid pins return ADC_ERROR_VALUE. If a comparison has been set up and fails, it will return ADC_ERROR_VALUE.

Definition at line 347 of file ADC.cpp.

◆ disableCompare()

void ADC::disableCompare ( )
inline

Disable the compare function.

Definition at line 266 of file ADC.h.

◆ enableCompare()

void ADC::enableCompare ( int16_t  compValue,
bool  greaterThan 
)
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()

Parameters
compValuevalue to compare
greaterThantrue or false

Definition at line 233 of file ADC.h.

◆ enableCompareRange()

void ADC::enableCompareRange ( int16_t  lowerLimit,
int16_t  upperLimit,
bool  insideRange,
bool  inclusive 
)
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()

Parameters
lowerLimitlower value to compare
upperLimitupper value to compare
insideRangetrue or false
inclusivetrue or false

Definition at line 254 of file ADC.h.

◆ enableDMA()

void ADC::enableDMA ( )
inline

Enable DMA request for all ADCs.

An ADC DMA request will be raised when the conversion is completed (including hardware averages and if the comparison (if any) is true).

Definition at line 205 of file ADC.h.

◆ enableInterrupts()

void ADC::enableInterrupts ( void(*)(void)  isr,
uint8_t  priority = 255 
)
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).

Parameters
isrfunction (returns void and accepts no arguments) that will be executed after an interrupt.
priorityInterrupt priority, highest is 0, lowest is 255.

Definition at line 181 of file ADC.h.

◆ readSingle()

int ADC::readSingle ( int8_t  adc_num = -1)

Reads the analog value of a single conversion.

Set the conversion with with startSingleRead(pin) or startSingleDifferential(pinP, pinN).

Parameters
adc_numADC_X ADC module
Returns
the converted value.

Definition at line 526 of file ADC.cpp.

◆ resetError()

void ADC::resetError ( )
inline

ERRORS ///// Resets all errors from all ADCs, if any.

Definition at line 578 of file ADC.h.

◆ setAveraging()

void ADC::setAveraging ( uint8_t  num)
inline

Set the number of averages for all ADCs.

Parameters
numcan be 0, 4, 8, 16 or 32.

Definition at line 165 of file ADC.h.

◆ setConversionSpeed()

void ADC::setConversionSpeed ( ADC_CONVERSION_SPEED  speed)
inline

Sets the conversion speed for all ADCs.

Parameters
speedcan be any from the ADC_CONVERSION_SPEED enum.

Definition at line 139 of file ADC.h.

◆ setReference()

void ADC::setReference ( ADC_REFERENCE  type)
inline

Set the voltage reference for all ADCs, default is vcc.

Parameters
typeAny of ADC_REFERENCE

Definition at line 111 of file ADC.h.

◆ setResolution()

void ADC::setResolution ( uint8_t  bits)
inline

Change the resolution for all ADCs.

Parameters
bitsis 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).

Definition at line 127 of file ADC.h.

◆ setSamplingSpeed()

void ADC::setSamplingSpeed ( ADC_SAMPLING_SPEED  speed)
inline

Sets the sampling speed for all ADCs.

Increase the sampling speed for low impedance sources, decrease it for higher impedance ones.

Parameters
speedcan be any of the ADC_SAMPLING_SPEED.

Definition at line 154 of file ADC.h.

◆ startContinuous()

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.

Parameters
pincan be any of the analog pins
adc_numADC_X ADC module
Returns
true if the pin is valid, false otherwise.

Definition at line 542 of file ADC.cpp.

◆ startContinuousDifferential()

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.

Parameters
pinPmust be A10 or A12.
pinNmust be A11 (if pinP=A10) or A13 (if pinP=A12).
adc_numADC_X ADC module
Returns
true if the pins are valid, false otherwise.

Definition at line 601 of file ADC.cpp.

◆ startSingleDifferential()

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

Parameters
pinPmust be A10 or A12.
pinNmust be A11 (if pinP=A10) or A13 (if pinP=A12).
adc_numADC_X ADC module
Returns
true if the pins are valid, false otherwise.

Definition at line 469 of file ADC.cpp.

◆ startSingleRead()

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

Parameters
pincan be any of the analog pins
adc_numADC_X ADC module
Returns
true if the pin is valid, false otherwise.

Definition at line 407 of file ADC.cpp.

◆ stopContinuous()

void ADC::stopContinuous ( int8_t  adc_num = -1)

Stops continuous conversion.

Parameters
adc_numADC_X ADC module

Definition at line 674 of file ADC.cpp.

Member Data Documentation

◆ channel2sc1aADC0

const uint8_t ADC::channel2sc1aADC0
static
Initial value:
= {
5, 14, 8, 9, 13, 12, 6, 7, 15, 11, 0, 4 + ADC_SC1A_PIN_MUX, 23, 31,
5, 14, 8, 9, 13, 12, 6, 7, 15, 11,
0 + ADC_SC1A_PIN_DIFF, 4 + ADC_SC1A_PIN_MUX + ADC_SC1A_PIN_DIFF, 23, 31, 31, 31, 31, 31, 31, 31,
31, 31, 31, 31,
26, 27, 31, 27, 29, 30
}

Translate pin number to SC1A nomenclature.

Definition at line 586 of file ADC.h.

◆ diff_table_ADC0

const ADC_Module::ADC_NLIST ADC::diff_table_ADC0
static
Initial value:
= {
{A10, 0}}

Translate differential pin number to SC1A nomenclature.

Definition at line 601 of file ADC.h.

◆ sc1a2channelADC0

const uint8_t ADC::sc1a2channelADC0
static
Initial value:
= {
24, 0, 0, 0, 25, 14, 20, 21, 16, 17, 0, 23, 19, 18,
15, 22, 23, 0, 0, 0, 0, 0,
26, 0, 0, 0, 38, 41, 0, 42, 43,
0
}

Translate pin number to SC1A nomenclature for differential pins.

Definition at line 593 of file ADC.h.


The documentation for this class was generated from the following files: