ADC 7.0

- Main Page
- Related Pages
- Classes
- Files

- Class List
- Class Index
- Class Members

All Classes Functions Variables Pages
Classes | Public Member Functions | Public Attributes | Static Public Attributes | List of all members
ADC Class Reference

`#include <ADC.h>`

## Classes
struct | Sync_result
| Struct for synchronous measurements. More...

## Public Member Functions
| ADC ()
void | setReference (uint8_t type, int8_t adc_num=-1)
| Set the voltage reference you prefer, default is 3.3 V (VCC) More...
void | setResolution (uint8_t bits, int8_t adc_num=-1)
| Change the resolution of the measurement. More...
uint8_t | getResolution (int8_t adc_num=-1)
| Returns the resolution of the ADC_Module.
uint32_t | getMaxValue (int8_t adc_num=-1)
| Returns the maximum value for a measurement: 2^res-1. More...
void | setConversionSpeed (uint8_t speed, int8_t adc_num=-1)
| Sets the conversion speed (changes the ADC clock, ADCK) More...
void | setSamplingSpeed (uint8_t speed, int8_t adc_num=-1)
| Sets the sampling speed. More...
void | setAveraging (uint8_t num, int8_t adc_num=-1)
| Set the number of averages. More...
void | enableInterrupts (int8_t adc_num=-1)
| Enable interrupts. More...
void | disableInterrupts (int8_t adc_num=-1)
| Disable interrupts.
void | enableDMA (int8_t adc_num=-1)
| Enable DMA request. More...
void | disableDMA (int8_t adc_num=-1)
| Disable ADC DMA request.
void | enableCompare (int16_t compValue, bool greaterThan, int8_t adc_num=-1)
| Enable the compare function to a single value. More...
void | enableCompareRange (int16_t lowerLimit, int16_t upperLimit, bool insideRange, bool inclusive, int8_t adc_num=-1)
| Enable the compare function to a range. More...
void | disableCompare (int8_t adc_num=-1)
| Disable the compare function.
void | enablePGA (uint8_t gain, int8_t adc_num=-1)
| Enable and set PGA. More...
uint8_t | getPGA (int8_t adc_num=-1)
| Returns the PGA level. More...
void | disablePGA (int8_t adc_num=-1)
| Disable PGA.
bool | isConverting (int8_t adc_num=-1)
| Is the ADC converting at the moment?
bool | isComplete (int8_t adc_num=-1)
| Is an ADC conversion ready? More...
bool | isDifferential (int8_t adc_num=-1)
| Is the ADC in differential mode?
bool | isContinuous (int8_t adc_num=-1)
| Is the ADC in continuous mode?
int | analogRead (uint8_t pin, int8_t adc_num=-1)
| Returns the analog value of the pin. More...
int | analogReadDifferential (uint8_t pinP, uint8_t pinN, int8_t adc_num=-1)
| Reads the differential analog value of two pins (pinP - pinN). More...
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)
| Start a differential conversion between two pins (pinP - pinN) and enables interrupts. More...
int | readSingle (int8_t adc_num=-1)
| Reads the analog value of a single conversion. More...
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.
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. More...
Sync_result | analogSyncRead (uint8_t pin0, uint8_t pin1) __attribute__((always_inline))
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 modules. More...
Sync_result | analogSyncReadDifferential (uint8_t pin0P, uint8_t pin0N, uint8_t pin1P, uint8_t pin1N) __attribute__((always_inline))
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)
| Start a differential conversion between two pins (pin0P - pin0N) and (pin1P - pin1N) More...
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.
void | stopSynchronizedContinuous ()
| Stops synchronous continuous conversion.

## Public Attributes
ADC_Module * | adc0
| Object to control the ADC0.
ADC_Module * | adc1
| Object to control the ADC1.

## Static Public Attributes
static const uint8_t | channel2sc1aADC0 [44]
| Translate pin number to SC1A nomenclature. More...
static const uint8_t | channel2sc1aADC1 [44]
| Translate pin number to SC1A nomenclature. More...
static const uint8_t | channel2sc1a_diff_ADC0 [4]
| Translate pin number to SC1A nomenclature for differential pins. More...
static const uint8_t | channel2sc1a_diff_ADC1 [4]
static const uint8_t | sc1a2channelADC0 [44]
| Translate SC1A to pin number. More...
static const uint8_t | sc1a2channelADC1 [44]

## Detailed Description

Class ADC: Controls the Teensy 3.x ADC

## Constructor & Destructor Documentation

ADC::ADC | ( |  | ) |

Default constructor

## Member Function Documentation

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 Teensy 3.0 it will return ADC_ERROR_VALUE.

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

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!

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. If a comparison has been set up and fails, it will return ADC_ERROR_VALUE.

Parameters

  pinP | must be A10 or A12.
  pinN | must be A11 (if pinP=A10) or A13 (if pinP=A12). Other pins 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

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

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

void ADC::enableCompare | ( | int16_t | compValue,
|  | bool | greaterThan,
|  | int8_t | adc_num = `-1`
| ) |  |

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()

void ADC::enableCompareRange | ( | int16_t | lowerLimit,
|  | int16_t | upperLimit,
|  | bool | insideRange,
|  | bool | inclusive,
|  | int8_t | adc_num = `-1`
| ) |  |

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()

void ADC::enableDMA | ( | int8_t | adc_num = `-1` | ) |

Enable DMA request.

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

void ADC::enableInterrupts | ( | int8_t | adc_num = `-1` | ) |

Enable interrupts.

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

void ADC::enablePGA | ( | uint8_t | gain,
|  | int8_t | adc_num = `-1`
| ) |  |

Enable and set PGA.

Enables the PGA and sets the gain Use only for signals lower than 1.2 V

Parameters

  gain | can be 1, 2, 4, 8, 16, 32 or 64

uint32_t ADC::getMaxValue | ( | int8_t | adc_num = `-1` | ) |

Returns the maximum value for a measurement: 2^res-1.

Returns the maximum value for a measurement.

uint8_t ADC::getPGA | ( | int8_t | adc_num = `-1` | ) |

Returns the PGA level.

PGA level = from 1 to 64

PGA level = 2^gain, from 0 to 64

bool ADC::isComplete | ( | int8_t | adc_num = `-1` | ) |

Is an ADC conversion ready?

Returns
  1 if yes, 0 if not. When a value is read this function returns 0 until a new value exists So it only makes sense to call it with continuous or non-blocking methods

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

Returns
  the converted value.

ADC::Sync_result ADC::readSynchronizedSingle | ( |  | ) |

Reads the analog value of a single conversion.

Returns
  the converted value.

void ADC::setAveraging | ( | uint8_t | num,
|  | int8_t | adc_num = `-1`
| ) |  |

Set the number of averages.

Parameters

  num | can be 0, 4, 8, 16 or 32.

void ADC::setConversionSpeed | ( | uint8_t | speed,
|  | int8_t | adc_num = `-1`
| ) |  |

Sets the conversion speed (changes the ADC clock, ADCK)

Parameters

  speed | can be ADC_VERY_LOW_SPEED, ADC_LOW_SPEED, ADC_MED_SPEED, ADC_HIGH_SPEED_16BITS, ADC_HIGH_SPEED or ADC_VERY_HIGH_SPEED.

ADC_VERY_LOW_SPEED is guaranteed to be the lowest possible speed within specs for resolutions less than 16 bits (higher than 1 MHz), it's different from ADC_LOW_SPEED only for 24, 4 or 2 MHz bus frequency. ADC_LOW_SPEED is guaranteed to be the lowest possible speed within specs for all resolutions (higher than 2 MHz). ADC_MED_SPEED is always >= ADC_LOW_SPEED and <= ADC_HIGH_SPEED. ADC_HIGH_SPEED_16BITS is guaranteed to be the highest possible speed within specs for all resolutions (lower or eq than 12 MHz). ADC_HIGH_SPEED is guaranteed to be the highest possible speed within specs for resolutions less than 16 bits (lower or eq than 18 MHz). ADC_VERY_HIGH_SPEED may be out of specs, it's different from ADC_HIGH_SPEED only for 48, 40 or 24 MHz bus frequency.

Additionally the conversion speed can also be ADC_ADACK_2_4, ADC_ADACK_4_0, ADC_ADACK_5_2 and ADC_ADACK_6_2, where the numbers are the frequency of the ADC clock (ADCK) in MHz and are independent on the bus speed. This is useful if you are using the Teensy at a very low clock frequency but want faster conversions, but if F_BUS<F_ADCK, you can't use ADC_VERY_HIGH_SPEED for sampling speed.

void ADC::setReference | ( | uint8_t | type,
|  | int8_t | adc_num = `-1`
| ) |  |

Set the voltage reference you prefer, default is 3.3 V (VCC)

Parameters

  type | can be ADC_REF_3V3, ADC_REF_1V2 (not for Teensy LC) or ADC_REF_EXT.

It recalibrates at the end.

void ADC::setResolution | ( | uint8_t | bits,
|  | int8_t | adc_num = `-1`
| ) |  |

Change the resolution of the measurement.

Parameters

  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 inmediate higher and shift the result one to the right.

Whenever you change the resolution, change also the comparison values (if you use them).

void ADC::setSamplingSpeed | ( | uint8_t | speed,
|  | int8_t | adc_num = `-1`
| ) |  |

Sets the sampling speed.

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

Parameters

  speed | can be ADC_VERY_LOW_SPEED, ADC_LOW_SPEED, ADC_MED_SPEED, ADC_HIGH_SPEED or ADC_VERY_HIGH_SPEED.

ADC_VERY_LOW_SPEED is the lowest possible sampling speed (+24 ADCK). ADC_LOW_SPEED adds +16 ADCK. ADC_MED_SPEED adds +10 ADCK. ADC_HIGH_SPEED (or ADC_HIGH_SPEED_16BITS) adds +6 ADCK. ADC_VERY_HIGH_SPEED is the highest possible sampling speed (0 ADCK added).

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.

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

  pinP | must be A10 or A12.
  pinN | must be A11 (if pinP=A10) or A13 (if pinP=A12). Other pins will return ADC_ERROR_DIFF_VALUE.

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 inmediately, get value with readSingle().

Parameters

  pinP | must be A10 or A12.
  pinN | must be A11 (if pinP=A10) or A13 (if pinP=A12).

Other pins will return ADC_ERROR_DIFF_VALUE. If this function interrupts a measurement, it stores the settings in adc_config

bool ADC::startSingleRead | ( | uint8_t | pin,
|  | int8_t | adc_num = `-1`
| ) |  |

Starts an analog measurement on the pin and enables interrupts.

It returns inmediately, get value with readSingle(). If the pin is incorrect it returns ADC_ERROR_VALUE If this function interrupts a measurement, it stores the settings in adc_config

bool ADC::startSynchronizedContinuous | ( | uint8_t | pin0,
|  | uint8_t | pin1
| ) |  |

Starts a continuous conversion in both ADCs simultaneously.

Use readSynchronizedContinuous to get the values

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

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 inmediately, get value with readSynchronizedSingle().

Parameters

  pin0P,pin1P | must be A10 or A12.
  pin0N,pin1N | must be A11 (if pinP=A10) or A13 (if pinP=A12). Other pins will return false. If this function interrupts a measurement, it stores the settings in adc_config

bool ADC::startSynchronizedSingleRead | ( | uint8_t | pin0,
|  | uint8_t | pin1
| ) |  |

Starts an analog measurement at the same time on the two ADC modules.

It returns inmediately, get value with readSynchronizedSingle(). If the pin is incorrect it returns false If this function interrupts a measurement, it stores the settings in adc_config

## Member Data Documentation

const uint8_t ADC::channel2sc1a_diff_ADC0
| static

Initial value:
= {
0+ADC_SC1A_PIN_PGA, 0+ADC_SC1A_PIN_PGA, 3, 3
}

Translate pin number to SC1A nomenclature for differential pins.

uses base A10, that is, channel2sc1a_diff_ADC0[0] corresponds to pin A10.

const uint8_t ADC::channel2sc1a_diff_ADC1
| static

Initial value:
= {
3, 3, 0+ADC_SC1A_PIN_PGA, 0+ADC_SC1A_PIN_PGA
}

const uint8_t ADC::channel2sc1aADC0
| static

Initial value:
= {
5, 14, 8, 9, 13, 12, 6, 7, 15, 4, 0, 19, 3, 21,
5, 14, 8, 9, 13, 12, 6, 7, 15, 4,
31, 31, 31, 31, 31, 31, 31, 31, 31, 31,
0+ADC_SC1A_PIN_DIFF, 19+ADC_SC1A_PIN_DIFF, 3+ADC_SC1A_PIN_DIFF, 21+ADC_SC1A_PIN_DIFF,
26, 22, 23, 27, 29, 30
}

Translate pin number to SC1A nomenclature.

const uint8_t ADC::channel2sc1aADC1
| static

Initial value:
= {
31, 31, 8, 9, 31, 31, 31, 31, 31, 31, 3, 31, 0, 19,
31, 31, 8, 9, 31, 31, 31, 31, 31, 31,
31, 31,
5+ADC_SC1A_PIN_MUX, 5, 4, 6, 7, 4+ADC_SC1A_PIN_MUX, 31, 31,
3+ADC_SC1A_PIN_DIFF, 31+ADC_SC1A_PIN_DIFF, 0+ADC_SC1A_PIN_DIFF, 19+ADC_SC1A_PIN_DIFF,
26, 18, 31, 27, 29, 30
}

Translate pin number to SC1A nomenclature.

const uint8_t ADC::sc1a2channelADC0
| static

Initial value:
= {
34, 0, 0, 36, 23, 14, 20, 21, 16, 17, 0, 0, 19, 18,
15, 22, 23, 0, 0, 35, 0, 37,
39, 40, 0, 0, 38, 41, 42, 43,
0
}

Translate SC1A to pin number.

const uint8_t ADC::sc1a2channelADC1
| static

Initial value:
= {
36, 0, 0, 34, 28, 26, 29, 30, 16, 17, 0, 0, 0, 0,
0, 0, 0, 0, 39, 37, 0, 0,
0, 0, 0, 0, 38, 41, 0, 42,
43
}

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

- ADC.h
- ADC.cpp