In this use case, the SDADC 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 compete.
The SDADC will be set up as follows:
- GCLK generator 0 (GCLK main) clock source
- Internal bandgap reference 1V
- Div 2 clock prescaler
- Over Sampling Ratio is 1024
- Skip 2 samples
- MUX input on SDADC AIN2
- All events (input and generation) disabled
- Free running disabled
- Run in standby disabled
- On command disabled
- Disable all positive input in sequence
- Window monitor disabled
- No gain/offset/shift correction
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 SDADC_SAMPLES 128
int32_t sdadc_result_buffer[SDADC_SAMPLES];
Callback function:
volatile bool sdadc_read_done = false;
void sdadc_complete_callback(
{
sdadc_read_done = true;
}
Copy-paste the following setup code to your user application:
void configure_sdadc(void)
{
config_sdadc.clock_prescaler = 2;
sdadc_init(&sdadc_instance, SDADC, &config_sdadc);
}
void configure_sdadc_callbacks(void)
{
}
Add to user application initialization (typically the start of main()
):
configure_sdadc();
configure_sdadc_callbacks();
Workflow
- Create a module software instance structure for the SDADC module to store the SDADC driver state while it is 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 SDADC samples to be stored in by the driver asynchronously.
#define SDADC_SAMPLES 128
int32_t sdadc_result_buffer[SDADC_SAMPLES];
- Create a callback function that will be called each time the SDADC completes an asynchronous read job.
volatile bool sdadc_read_done = false;
void sdadc_complete_callback(
{
sdadc_read_done = true;
}
- Configure the SDADC module.
- Create a SDADC module configuration struct, which can be filled out to adjust the configuration of a physical SDADC peripheral.
- Initialize the SDADC 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 SDADC module configuration to suit the application.
config_sdadc.clock_prescaler = 2;
- Set SDADC configurations.
sdadc_init(&sdadc_instance, SDADC, &config_sdadc);
- Enable the SDADC module so that conversions can be made.
- Register and enable the SDADC 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 (sdadc_read_done == false) {
}
while (1) {
}
Workflow
- Enable global interrupts, so that callbacks can be generated by the driver.
- Start an asynchronous SDADC conversion, to store SDADC samples into the global buffer and generate a callback when complete.
- Wait until the asynchronous conversion is complete.
while (sdadc_read_done == false) {
}
- Enter an infinite loop once the conversion is complete.