ADC
7.0
|
#include <ADC_Module.h>
Classes | |
struct | ADC_Config |
Store the config of the adc. More... | |
Public Member Functions | |
ADC_Module (uint8_t ADC_number, const uint8_t *const a_channel2sc1a, const uint8_t *const a_channel2sc1a_diff) | |
Constructor. More... | |
void | recalibrate () |
Starts the calibration sequence, waits until it's done and writes the results. More... | |
void | calibrate () |
Starts the calibration sequence. | |
void | wait_for_cal () |
Waits until calibration is finished and writes the corresponding registers. | |
void | setReference (uint8_t type) |
Set the voltage reference you prefer, default is vcc. More... | |
void | setResolution (uint8_t bits) |
Change the resolution of the measurement. More... | |
uint8_t | getResolution () |
Returns the resolution of the ADC_Module. | |
uint32_t | getMaxValue () |
Returns the maximum value for a measurement: 2^res-1. | |
void | setConversionSpeed (uint8_t speed) |
Sets the conversion speed. More... | |
void | setSamplingSpeed (uint8_t speed) |
Sets the sampling speed. More... | |
void | setAveraging (uint8_t num) |
Set the number of averages. More... | |
void | enableInterrupts () |
Enable interrupts. More... | |
void | disableInterrupts () |
Disable interrupts. | |
void | enableDMA () |
Enable DMA request. More... | |
void | disableDMA () |
Disable ADC DMA request. | |
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. | |
void | enablePGA (uint8_t gain) |
Enable and set PGA. More... | |
uint8_t | getPGA () |
Returns the PGA level. More... | |
void | disablePGA () |
Disable PGA. | |
void | continuousMode () __attribute__((always_inline)) |
Set continuous conversion mode. | |
void | singleMode () __attribute__((always_inline)) |
Set single-shot conversion mode. | |
void | setSoftwareTrigger () __attribute__((always_inline)) |
Use software to trigger the ADC, this is the most common setting. | |
void | setHardwareTrigger () __attribute__((always_inline)) |
Use hardware to trigger the ADC. | |
bool | isConverting () __attribute__((always_inline)) |
Is the ADC converting at the moment? | |
bool | isComplete () __attribute__((always_inline)) |
Is an ADC conversion ready? More... | |
bool | isDifferential () __attribute__((always_inline)) |
Is the ADC in differential mode? | |
bool | isContinuous () __attribute__((always_inline)) |
Is the ADC in continuous mode? | |
bool | isPGAEnabled () __attribute__((always_inline)) |
Is the PGA function enabled? | |
bool | checkPin (uint8_t pin) |
Check whether the pin is a valid analog pin. | |
bool | checkDifferentialPins (uint8_t pinP, uint8_t pinN) |
Check whether the pins are a valid analog differential pair of pins. More... | |
void | startReadFast (uint8_t pin) |
Starts a single-ended conversion on the pin. More... | |
void | startDifferentialFast (uint8_t pinP, uint8_t pinN) |
Starts a differential conversion on the pair of pins. More... | |
int | analogRead (uint8_t pin) |
Returns the analog value of the pin. More... | |
int | analogReadDifferential (uint8_t pinP, uint8_t pinN) |
Reads the differential analog value of two pins (pinP - pinN). More... | |
bool | startSingleRead (uint8_t pin) |
Starts an analog measurement on the pin and enables interrupts. More... | |
bool | startSingleDifferential (uint8_t pinP, uint8_t pinN) |
Start a differential conversion between two pins (pinP - pinN) and enables interrupts. More... | |
int | readSingle () __attribute__((always_inline)) |
Reads the analog value of a single conversion. More... | |
bool | startContinuous (uint8_t pin) |
Starts continuous conversion on the pin. More... | |
bool | startContinuousDifferential (uint8_t pinP, uint8_t pinN) |
Starts continuous conversion between the pins (pinP-pinN). More... | |
int | analogReadContinuous () __attribute__((always_inline)) |
Reads the analog value of a continuous conversion. More... | |
void | stopContinuous () |
Stops continuous conversion. | |
void | startPDB (uint32_t freq) |
Start PDB triggering the ADC at the frequency. More... | |
void | stopPDB () |
Stop the PDB. | |
void | saveConfig (ADC_Config *config) |
Save config of the ADC to the ADC_Config struct. | |
void | loadConfig (ADC_Config *config) |
Load config to the ADC. | |
Public Attributes | |
struct ADC_Module::ADC_Config | adc_config |
uint8_t | adcWasInUse |
Was the adc in use before a call? | |
uint8_t | num_measurements |
Number of measurements that the ADC is performing. | |
volatile uint16_t | fail_flag |
This flag indicates that some kind of error took place. More... | |
uint8_t | ADC_num |
Which adc is this? | |
Class ADC_Module: Implements all functions of the Teensy 3.x analog to digital converter
ADC_Module::ADC_Module | ( | uint8_t | ADC_number, |
const uint8_t *const | a_channel2sc1a, | ||
const uint8_t *const | a_channel2sc1a_diff | ||
) |
Constructor.
Pass the ADC number and the Channel number to SC1A number arrays.
a_channel2sc1a | contains an index that pairs each pin to its SC1A number (used to start a conversion on that pin) |
a_channel2sc1a_diff | is similar to a_channel2sc1a, but for differential pins. It uses base A10: a_channel2sc1a_diff[0] corresponds to A10. |
int ADC_Module::analogRead | ( | uint8_t | pin | ) |
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
|
inline |
Reads the analog value of a continuous conversion.
Set the continuous conversion with with analogStartContinuous(pin) or startContinuousDifferential(pinP, pinN).
int ADC_Module::analogReadDifferential | ( | uint8_t | pinP, |
uint8_t | pinN | ||
) |
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_DIFF_VALUE.
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. This function is interrupt safe, so it will restore the adc to the state it was before being called |
bool ADC_Module::checkDifferentialPins | ( | uint8_t | pinP, |
uint8_t | pinN | ||
) |
Check whether the pins are a valid analog differential pair of pins.
If PGA is enabled it also checks that this ADCx can use PGA on this pins
void ADC_Module::enableCompare | ( | int16_t | compValue, |
bool | greaterThan | ||
) |
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_Module::enableCompareRange | ( | int16_t | lowerLimit, |
int16_t | upperLimit, | ||
bool | insideRange, | ||
bool | inclusive | ||
) |
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_Module::enableDMA | ( | ) |
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_Module::enableInterrupts | ( | ) |
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_Module::enablePGA | ( | uint8_t | gain | ) |
Enable and set PGA.
Enables the PGA and sets the gain Use only for signals lower than 1.2 V
gain | can be 1, 2, 4, 8, 16, 32 or 64 |
uint8_t ADC_Module::getPGA | ( | ) |
Returns the PGA level.
PGA from 1 to 64
|
inline |
Is an ADC conversion ready?
|
inline |
Reads the analog value of a single conversion.
Set the conversion with with startSingleRead(pin) or startSingleDifferential(pinP, pinN).
void ADC_Module::recalibrate | ( | ) |
Starts the calibration sequence, waits until it's done and writes the results.
Usually it's not necessary to call this function directly, but do it if the "enviroment" changed significantly since the program was started.
void ADC_Module::setAveraging | ( | uint8_t | num | ) |
Set the number of averages.
num | can be 0, 4, 8, 16 or 32. |
It doesn't recalibrate at the end.
void ADC_Module::setConversionSpeed | ( | uint8_t | speed | ) |
Sets the conversion speed.
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_Module::setReference | ( | uint8_t | type | ) |
Set the voltage reference you prefer, default is vcc.
type | can be ADC_REF_3V3, ADC_REF_1V2 (not for Teensy LC) or ADC_REF_EXT |
It recalibrates at the end.
void ADC_Module::setResolution | ( | uint8_t | bits | ) |
Change the resolution of the measurement.
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_Module::setSamplingSpeed | ( | uint8_t | speed | ) |
Sets the sampling speed.
Increase the sampling speed for low impedance sources, decrease it for higher impedance ones.
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_Module::startContinuous | ( | uint8_t | pin | ) |
Starts continuous conversion on the pin.
It returns as soon as the ADC is set, use analogReadContinuous() to read the value.
bool ADC_Module::startContinuousDifferential | ( | uint8_t | pinP, |
uint8_t | pinN | ||
) |
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). Other pins will return ADC_ERROR_DIFF_VALUE. |
void ADC_Module::startDifferentialFast | ( | uint8_t | pinP, |
uint8_t | pinN | ||
) |
Starts a differential conversion on the pair of pins.
It sets the mux correctly, doesn't do any of the checks on the pin and doesn't change the continuous conversion bit.
void ADC_Module::startPDB | ( | uint32_t | freq | ) |
void ADC_Module::startReadFast | ( | uint8_t | pin | ) |
Starts a single-ended conversion on the pin.
It sets the mux correctly, doesn't do any of the checks on the pin and doesn't change the continuous conversion bit.
bool ADC_Module::startSingleDifferential | ( | uint8_t | pinP, |
uint8_t | pinN | ||
) |
Start a differential conversion between two pins (pinP - pinN) and enables interrupts.
It returns inmediately, get value with readSingle().
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_Module::startSingleRead | ( | uint8_t | pin | ) |
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
volatile uint16_t ADC_Module::fail_flag |
This flag indicates that some kind of error took place.
Use the defines at the beggining of this file to find out what caused the fail.