In this use case, ADCA is configured for:
- sampling on two channels (0 and 1) with respective inputs:
- internal temperature sensor
- internal bandgap reference
- unsigned conversions
- 12-bit resolution
- VCC/1.6 as voltage reference
- event-triggered conversions
- polled conversion handling
Completed conversions are detected via non-blocking polling of the interrupt flags. The ADC results are stored into local variables as soon as they are available.
A Timer/Counter is used to generate events that trigger the conversions.
- Note
- This use case assumes that the device has multiple ADC channels. Refer to the applicable device datasheet for information about the number of ADC channels.
Setup steps
Prerequisites
This use case requires that the Timer/Counter driver is added to the project.
Example code
Add to application C-file:
#define MY_ADC ADCA
#define MY_TIMER TCC0
static void evsys_init(void)
{
EVSYS.CH3MUX = EVSYS_CHMUX_TCC0_OVF_gc;
}
{
}
static void adc_init(void)
{
}
Add to main()
:
evsys_init();
adc_init();
Workflow
- Add macros for the ADC and the conversion trigger timer to use, so they are easy to change:
#define MY_ADC ADCA
#define MY_TIMER TCC0
- Create a function
evsys_init()
to intialize the event system clocks and to link the conversion timer to the correct event channel:
static void evsys_init(void)
{
}
- Use the sysclk service to enable the clock to the event system:
- Connect the TCC0 overflow event to event channel 3:
EVSYS.CH3MUX = EVSYS_CHMUX_TCC0_OVF_gc;
- Note
- If the ADC trigger timer is changed from TCC0, the
EVSYS_CHMUX_*
mask here will also need to be altered.
- Create a function
tc_init()
to intialize the ADC trigger timer:
- Enable the clock to the ADC trigger timer:
- Configure the ADC trigger timer in normal Waveform Generation mode:
- Configure the ADC trigger timer period to overflow at 200 counts:
- Configure the ADC trigger timer resolution (frequency) for 2KHz:
- Create a function
adc_init()
to intialize the ADC ready for conversions on channels 0 and 1, triggered by the event system:
static void adc_init(void)
{
}
- Allocate configuration structs for ADC and channel, then initialize them:
- Set unsigned, 12-bit conversions with internal VCC/1.6 voltage reference:
- Set event system triggered conversions on the first two ADC channels, with conversions triggered by event system channel 3:
-
- Note
- The event system channel used here must match the channel linked to the conversion trigger timer set up earlier in
tc_init()
.
- Turn on the internal bandgap and temperature sensor ADC inputs:
- Set ADC clock rate to maximum 200 KHz:
- Write the configuration to the ADC:
- Set up single-ended input from the internal temperature sensor, then write the config to the first channel (0):
- Set up single-ended input from the internal bandgap voltage, then write the config to the second channel (1):
- Initialize the clock system, event system, ADC trigger timer, and the ADC:
evsys_init();
adc_init();
Usage steps
Example code
Add to main()
:
do {
uint16_t tmp_result;
uint16_t bg_result;
}
} while (true);
Workflow
- Enable the configured ADC module, so that it will begin conversions when triggered:
- Create an infinite loop so that conversions will be processed forever:
- Within the loop, create local variables to contain the ADC result of each channel (internal temperature sensor and internal bandgap voltage):
int16_t temp_result;
int16_t bg_result;
- Test if both ADC channel 0 and channel 1 have completed a conversion by testing the respective channel conversion complete interrupt flags:
- Store the channel result values into the local variables created earlier:
- Clear both ADC channel conversion complete interrupt flags, so that we can detect future conversions at a later stage: