Microchip® Advanced Software Framework

Quick Start Guide for SDADC - Callback

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

Callback function:

volatile bool sdadc_read_done = false;
const struct sdadc_module *const module)
{
sdadc_read_done = true;
}

Copy-paste the following setup code to your user application:

void configure_sdadc(void)
{
struct sdadc_config config_sdadc;
sdadc_get_config_defaults(&config_sdadc);
config_sdadc.clock_prescaler = 2;
config_sdadc.reference.ref_sel = SDADC_REFERENCE_INTREF;
config_sdadc.osr = SDADC_OVER_SAMPLING_RATIO1024;
config_sdadc.mux_input = SDADC_MUX_INPUT_AIN2;
sdadc_init(&sdadc_instance, SDADC, &config_sdadc);
}
{
}

Add to user application initialization (typically the start of main()):

Workflow

  1. 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.
  2. 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];
  3. Create a callback function that will be called each time the SDADC completes an asynchronous read job.
    volatile bool sdadc_read_done = false;
    const struct sdadc_module *const module)
    {
    sdadc_read_done = true;
    }
  4. Configure the SDADC module.
    • Create a SDADC module configuration struct, which can be filled out to adjust the configuration of a physical SDADC peripheral.
      struct sdadc_config config_sdadc;
    • Initialize the SDADC configuration struct with the module's default values.
      sdadc_get_config_defaults(&config_sdadc);
      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;
      config_sdadc.reference.ref_sel = SDADC_REFERENCE_INTREF;
      config_sdadc.osr = SDADC_OVER_SAMPLING_RATIO1024;
      config_sdadc.mux_input = SDADC_MUX_INPUT_AIN2;
    • Set SDADC configurations.
      sdadc_init(&sdadc_instance, SDADC, &config_sdadc);
    • Enable the SDADC module so that conversions can be made.
  5. Register and enable the SDADC read buffer complete callback handler.

Use Case

Code

Copy-paste the following code to your user application:

while (sdadc_read_done == false) {
/* Wait for asynchronous SDADC read to complete */
}
while (1) {
/* Infinite loop */
}

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) {
    /* Wait for asynchronous SDADC read to complete */
    }
  • Enter an infinite loop once the conversion is complete.
    while (1) {
    /* Infinite loop */
    }