This driver provides an interface for the megaAVR ADC module.
It consists of inline functions that can be used to configure the ADC module and blocking ADC read functions for both 8- and 10-bit results.
Interface
To initialize the ADC you can use the adc_init() function. This sets the ADC prescaler and enables the ADC module. These operations can also be done separately using the adc_set_prescaler() and adc_enable() functions.
To do a blocking ADC read there are two different functions, one for 8-bit and one for 10-bit results:
Usage
First include the header file:
Then initialize and enable the ADC with the correct prescaler:
To do an 8-bit read of ADC channel 0 using 1.1V internal reference:
The syntax to do a 10-bit read is the same, but it returns a 16-bit unsigned integer to fit the whole 10-bit result.
the adc_read_8bit() and adc_read_10bit() functions do not take into account the fact that the voltage reference may be unstable after changing it. It is recommended to discard the first sample after changing voltage reference to avoid incorrect results.
- Note
- None of the functions are interrupt safe to avoid overhead. If you need interrupt protection you can use the following in your application code:
|
enum | adc_adjustment {
ADC_ADJUSTMENT_RIGHT = 0,
ADC_ADJUSTMENT_LEFT = (1 << ADLAR)
} |
|
enum | adc_auto_trigger_source {
ADC_AUTOTRIGGER_SOURCE_FREERUNNING = (0x0 << ADTS0),
ADC_AUTOTRIGGER_SOURCE_ANALOG_COMPARATOR = (0x1 << ADTS0),
ADC_AUTOTRIGGER_SOURCE_EIR0 = (0x2 << ADTS0),
ADC_AUTOTRIGGER_SOURCE_TC0_COMPARE = (0x3 << ADTS0),
ADC_AUTOTRIGGER_SOURCE_TC0_OVERFLOW = (0x4 << ADTS0),
ADC_AUTOTRIGGER_SOURCE_TC1_COMPARE_B = (0x5 << ADTS0),
ADC_AUTOTRIGGER_SOURCE_TC1_OVERFLOW = (0x6 << ADTS0),
ADC_AUTOTRIGGER_SOURCE_TC1_CAPTURE = (0x7 << ADTS0)
} |
| ADC Auto Trigger sources. More...
|
|
enum | adc_mux_setting {
ADC_MUX_ADC0 = 0x0,
ADC_MUX_ADC1 = 0x1,
ADC_MUX_ADC2 = 0x2,
ADC_MUX_ADC3 = 0x3,
ADC_MUX_ADC4 = 0x4,
ADC_MUX_ADC5 = 0x5,
ADC_MUX_ADC6 = 0x6,
ADC_MUX_ADC7 = 0x7,
ADC_MUX_1V1 = 0x1E,
ADC_MUX_GND = 0x1F,
ADC_MUX_ADC0_ADC0_10X = 0x8,
ADC_MUX_ADC1_ADC0_10X = 0x9,
ADC_MUX_ADC0_ADC0_200X = 0xA,
ADC_MUX_ADC1_ADC0_200X = 0xB,
ADC_MUX_ADC2_ADC2_10X = 0xC,
ADC_MUX_ADC3_ADC2_10X = 0xD,
ADC_MUX_ADC2_ADC2_200X = 0xE,
ADC_MUX_ADC3_ADC2_200X = 0xF,
ADC_MUX_ADC0_ADC1 = 0x10,
ADC_MUX_ADC1_ADC1 = 0x11,
ADC_MUX_ADC2_ADC1 = 0x12,
ADC_MUX_ADC3_ADC1 = 0x13,
ADC_MUX_ADC4_ADC1 = 0x14,
ADC_MUX_ADC5_ADC1 = 0x15,
ADC_MUX_ADC6_ADC1 = 0x16,
ADC_MUX_ADC7_ADC1 = 0x17,
ADC_MUX_ADC0_ADC2 = 0x18,
ADC_MUX_ADC1_ADC2 = 0x19,
ADC_MUX_ADC2_ADC2 = 0x1A,
ADC_MUX_ADC3_ADC2 = 0x1B,
ADC_MUX_ADC4_ADC2 = 0x1C,
ADC_MUX_ADC5_ADC2 = 0x1D
} |
| ADC input MUX settings. More...
|
|
enum | adc_prescaler {
ADC_PRESCALER_DIV2 = (0x1 << ADPS0),
ADC_PRESCALER_DIV4 = (0x2 << ADPS0),
ADC_PRESCALER_DIV8 = (0x3 << ADPS0),
ADC_PRESCALER_DIV16 = (0x4 << ADPS0),
ADC_PRESCALER_DIV32 = (0x5 << ADPS0),
ADC_PRESCALER_DIV64 = (0x6 << ADPS0),
ADC_PRESCALER_DIV128 = (0x7 << ADPS0)
} |
| ADC prescaler settings. More...
|
|
enum | adc_voltage_reference {
ADC_VREF_AREF = (0x0 << REFS0),
ADC_VREF_AVCC = (0x1 << REFS0),
ADC_VREF_1V1 = (0x2 << REFS0),
ADC_VREF_2V56 = (0x3 << REFS0)
} |
| ADC voltage references. More...
|
|
|
These functions are for cases where you do not want the overhead of masking out bitfields before updating the register value. These will only do a write to the register instead of a read-modify-write.
|
static void | adc_set_admux (uint8_t regval) |
| Set voltage reference, mux and adjustment bits in admux register. More...
|
|
#define ADC_ADTS_SOURCE_MASK (1 << ADTS2 | 1 << ADTS1 | 1 << ADTS0) |
#define ADC_MUX_MASK (1 << MUX3 | 1 << MUX2 | 1 << MUX1 | 1 << MUX0) |
#define ADC_PRESCALER_MASK (1 << ADPS2 | 1 << ADPS1 | 1 << ADPS0) |
#define ADC_VREF_MASK (1 << REFS1 | 1 << REFS0) |
Enumerator |
---|
ADC_ADJUSTMENT_RIGHT |
|
ADC_ADJUSTMENT_LEFT |
|
ADC Auto Trigger sources.
Available trigger sources for ADC Auto Trigger
Enumerator |
---|
ADC_AUTOTRIGGER_SOURCE_FREERUNNING |
|
ADC_AUTOTRIGGER_SOURCE_ANALOG_COMPARATOR |
|
ADC_AUTOTRIGGER_SOURCE_EIR0 |
|
ADC_AUTOTRIGGER_SOURCE_TC0_COMPARE |
|
ADC_AUTOTRIGGER_SOURCE_TC0_OVERFLOW |
|
ADC_AUTOTRIGGER_SOURCE_TC1_COMPARE_B |
|
ADC_AUTOTRIGGER_SOURCE_TC1_OVERFLOW |
|
ADC_AUTOTRIGGER_SOURCE_TC1_CAPTURE |
|
ADC input MUX settings.
MUX settings for ADC input
- Note
- If using 10X or 200X gain only 2.56V should be used as internal voltage reference.
-
The doxygen documentation contains MUX settings for multiple devices. check the device datasheet or family header files to find what options are available on your device.
Enumerator |
---|
ADC_MUX_ADC0 |
ADC0, single ended.
|
ADC_MUX_ADC1 |
ADC1, single ended.
|
ADC_MUX_ADC2 |
ADC2, single ended.
|
ADC_MUX_ADC3 |
ADC3, single ended.
|
ADC_MUX_ADC4 |
ADC4, single ended.
|
ADC_MUX_ADC5 |
ADC5, single ended.
|
ADC_MUX_ADC6 |
ADC6, single ended.
|
ADC_MUX_ADC7 |
ADC7, single ended.
|
ADC_MUX_1V1 |
1.1V internal bandgap voltage
|
ADC_MUX_GND |
GND, single ended.
|
ADC_MUX_ADC0_ADC0_10X |
Positive: ADC0 Negative: ADC0 Gain: 10x.
|
ADC_MUX_ADC1_ADC0_10X |
Positive: ADC1 Negative: ADC0 Gain: 10x.
|
ADC_MUX_ADC0_ADC0_200X |
Positive: ADC0 Negative: ADC0 Gain: 200x.
|
ADC_MUX_ADC1_ADC0_200X |
Positive: ADC1 Negative: ADC0 Gain: 200x.
|
ADC_MUX_ADC2_ADC2_10X |
Positive: ADC2 Negative: ADC2 Gain: 10x.
|
ADC_MUX_ADC3_ADC2_10X |
Positive: ADC3 Negative: ADC2 Gain: 10x.
|
ADC_MUX_ADC2_ADC2_200X |
Positive: ADC2 Negative: ADC2 Gain: 200x.
|
ADC_MUX_ADC3_ADC2_200X |
Positive: ADC3 Negative: ADC2 Gain: 200x.
|
ADC_MUX_ADC0_ADC1 |
Positive: ADC0 Negative: ADC1.
|
ADC_MUX_ADC1_ADC1 |
Positive: ADC1 Negative: ADC1.
|
ADC_MUX_ADC2_ADC1 |
Positive: ADC2 Negative: ADC1.
|
ADC_MUX_ADC3_ADC1 |
Positive: ADC3 Negative: ADC1.
|
ADC_MUX_ADC4_ADC1 |
Positive: ADC4 Negative: ADC1.
|
ADC_MUX_ADC5_ADC1 |
Positive: ADC5 Negative: ADC1.
|
ADC_MUX_ADC6_ADC1 |
Positive: ADC6 Negative: ADC1.
|
ADC_MUX_ADC7_ADC1 |
Positive: ADC7 Negative: ADC1.
|
ADC_MUX_ADC0_ADC2 |
Positive: ADC0 Negative: ADC2.
|
ADC_MUX_ADC1_ADC2 |
Positive: ADC1 Negative: ADC2.
|
ADC_MUX_ADC2_ADC2 |
Positive: ADC2 Negative: ADC2.
|
ADC_MUX_ADC3_ADC2 |
Positive: ADC3 Negative: ADC2.
|
ADC_MUX_ADC4_ADC2 |
Positive: ADC4 Negative: ADC2.
|
ADC_MUX_ADC5_ADC2 |
Positive: ADC5 Negative: ADC2.
|
ADC prescaler settings.
Available ADC prescaler settings
Enumerator |
---|
ADC_PRESCALER_DIV2 |
|
ADC_PRESCALER_DIV4 |
|
ADC_PRESCALER_DIV8 |
|
ADC_PRESCALER_DIV16 |
|
ADC_PRESCALER_DIV32 |
|
ADC_PRESCALER_DIV64 |
|
ADC_PRESCALER_DIV128 |
|
ADC voltage references.
Available ADC voltage references
Enumerator |
---|
ADC_VREF_AREF |
AREF, internal Vref turned off.
|
ADC_VREF_AVCC |
AVCC.
|
ADC_VREF_1V1 |
Internal 1.1V voltage reference.
|
ADC_VREF_2V56 |
Internal 2.56V voltage reference.
|
static bool adc_conversion_is_complete |
( |
void |
| ) |
|
|
inlinestatic |
Poll ADC conversion complete interrupt flag.
- Returns
- false if conversion not complete
static void adc_disable |
( |
void |
| ) |
|
|
inlinestatic |
static void adc_disable_autotrigger |
( |
void |
| ) |
|
|
inlinestatic |
static void adc_disable_interrupt |
( |
void |
| ) |
|
|
inlinestatic |
static void adc_enable |
( |
void |
| ) |
|
|
inlinestatic |
static void adc_enable_autotrigger |
( |
void |
| ) |
|
|
inlinestatic |
static void adc_enable_interrupt |
( |
void |
| ) |
|
|
inlinestatic |
static uint16_t adc_get_data_10bit |
( |
void |
| ) |
|
|
inlinestatic |
static uint8_t adc_get_data_8bit |
( |
void |
| ) |
|
|
inlinestatic |
ADC initialization.
Set ADC prescaler and enable the ADC
- Parameters
-
prescaler | ADC clock prescaler |
Referenced by main().
Blocking ADC conversion with 10-bit result.
An example demonstrating the minimum initialization needed to perform a conversion:
- Parameters
-
input | ADC input mux selection and voltage reference |
vref | ADC voltage reference |
- Returns
- Conversion result as an unsigned 16-bit integer
- Note
- The first ADC conversion result after switching reference voltage source may be inaccurate, and the user is advised to discard this result. this function is not interrupt safe
References ADC_MUX_MASK.
Blocking ADC conversion with 8-bit result.
An example demonstrating the minimum initialization needed to perform a conversion:
- Parameters
-
input | ADC input mux selection. |
vref | ADC voltage reference |
- Returns
- Conversion result as an unsigned 8-bit integer
- Note
- The first ADC conversion result after switching reference voltage source may be inaccurate, and the user is advised to discard this result. this function is not interrupt safe.
References ADC_ADJUSTMENT_LEFT, and ADC_MUX_MASK.
Referenced by main().
static void adc_set_admux |
( |
uint8_t |
regval | ) |
|
|
inlinestatic |
Set voltage reference, mux and adjustment bits in admux register.
- Note
- This does not handle the MUX5 bit if this is available, only the MUX bits in ADMUX.
Example usage:
- Parameters
-
regval | ADC input mux selection and voltage reference |
Select source from ADC input mux.
A small example demonstrating the usage:
- Parameters
-
References ADC_MUX_MASK.
Set ADC clock prescaler.
- Note
- The ADC clock must not exceed the maximum speed of 1MHz
A small example demonstrating the usage:
- Parameters
-
prescaler | ADC clock prescaler |
References ADC_PRESCALER_MASK.
Set the ADC voltage reference.
An example demonstrating the usage:
- Parameters
-
vref | ADC voltage reference |
- Note
- The first ADC conversion result after switching reference voltage source may be inaccurate, and the user is advised to discard this result.
References ADC_VREF_MASK.
static void adc_start_conversion |
( |
void |
| ) |
|
|
inlinestatic |