In this use case, the ADC will convert 128 samples using interrupt driven conversion.
When all samples have been sampled, a callback will be called that signals the main application that conversion is complete.
The ADC will be set up as follows:
- VCC /2 as reference
- Div 8 clock prescaler
- 12-bit resolution
- Window monitor disabled
- 1/2 gain
- Positive input on ADC PIN 0
- Negative input to GND (single ended)
- Averaging disabled
- Oversampling disabled
- Right adjust data
- Single-ended mode
- Free running disabled
- All events (input and generation) disabled
- Sleep operation disabled
- No reference compensation
- No gain/offset correction
- No added sampling time
- Pin scan mode disabled
Setup
Prerequisites
There are no special setup requirements for this use-case.
Code
Add to the main application source file, outside of any functions:
#define ADC_SAMPLES 128
uint16_t adc_result_buffer[ADC_SAMPLES];
Callback function:
volatile bool adc_read_done = false;
void adc_complete_callback(
{
adc_read_done = true;
}
Copy-paste the following setup code to your user application:
void configure_adc(void)
{
#if (!SAML21) && (!SAML22) && (!SAMC21) && (!SAMR30)
config_adc.gain_factor = ADC_GAIN_FACTOR_DIV2;
#endif
#if (SAMC21)
#else
#endif
#if (SAMC21)
adc_init(&adc_instance, ADC1, &config_adc);
#else
adc_init(&adc_instance, ADC, &config_adc);
#endif
}
void configure_adc_callbacks(void)
{
}
Add to user application initialization (typically the start of main()
):
configure_adc();
configure_adc_callbacks();
Workflow
- Create a module software instance structure for the ADC module to store the ADC driver state while in use.
- Note
- This should never go out of scope as long as the module is in use. In most cases, this should be global.
- Create a buffer for the ADC samples to be stored in by the driver asynchronously.
#define ADC_SAMPLES 128
uint16_t adc_result_buffer[ADC_SAMPLES];
- Create a callback function that will be called each time the ADC completes an asynchronous read job.
volatile bool adc_read_done = false;
void adc_complete_callback(
{
adc_read_done = true;
}
- Configure the ADC module.
- Create an ADC module configuration struct, which can be filled out to adjust the configuration of a physical ADC peripheral.
- Initialize the ADC configuration struct with the module's default values.
- Note
- This should always be performed before using the configuration struct to ensure that all values are initialized to known default settings.
- Change the ADC module configuration to suit the application.
#if (!SAML21) && (!SAML22) && (!SAMC21) && (!SAMR30)
config_adc.gain_factor = ADC_GAIN_FACTOR_DIV2;
#endif
#if (SAMC21)
#else
#endif
- Set ADC configurations.
#if (SAMC21)
adc_init(&adc_instance, ADC1, &config_adc);
#else
adc_init(&adc_instance, ADC, &config_adc);
#endif
- Enable the ADC module so that conversions can be made.
- Register and enable the ADC Read Buffer Complete callback handler.
- Register the user-provided Read Buffer Complete callback function with the driver, so that it will be run when an asynchronous buffer read job completes.
- Enable the Read Buffer Complete callback so that it will generate callbacks.
Use Case
Code
Copy-paste the following code to your user application:
while (adc_read_done == false) {
}
while (1) {
}
Workflow
- Enable global interrupts, so that callbacks can be generated by the driver.
- Start an asynchronous ADC conversion, to store ADC samples into the global buffer and generate a callback when complete.
- Wait until the asynchronous conversion is complete.
while (adc_read_done == false) {
}
- Enter an infinite loop once the conversion is complete.