Microchip® Advanced Software Framework

Quick Start Guide for ADC - Basic

In this use case, the ADC will be configured with the following settings:

  • 1V from internal bandgap reference
  • Div 4 clock prescaler
  • 12-bit resolution
  • Window monitor disabled
  • No gain
  • Positive input on ADC PIN x (depend on default configuration)
  • 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:

struct adc_module adc_instance;

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

void configure_adc(void)
{
struct adc_config config_adc;
#if (SAMC21)
adc_init(&adc_instance, ADC1, &config_adc);
#else
adc_init(&adc_instance, ADC, &config_adc);
#endif
adc_enable(&adc_instance);
}

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

configure_adc();

Workflow

  1. Create a module software instance structure for the ADC module to store the ADC driver state while in use.
    struct adc_module adc_instance;
    Note
    This should never go out of scope as long as the module is in use. In most cases, this should be global.
  2. Configure the ADC module.
    1. Create an ADC module configuration struct, which can be filled out to adjust the configuration of a physical ADC peripheral.
      struct adc_config config_adc;
    2. 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.
    3. Set ADC configurations.
      #if (SAMC21)
      adc_init(&adc_instance, ADC1, &config_adc);
      #else
      adc_init(&adc_instance, ADC, &config_adc);
      #endif
    4. Enable the ADC module so that conversions can be made.
      adc_enable(&adc_instance);

Use Case

Code

Copy-paste the following code to your user application:

adc_start_conversion(&adc_instance);
uint16_t result;
do {
/* Wait for conversion to be done and read out result */
} while (adc_read(&adc_instance, &result) == STATUS_BUSY);
while (1) {
/* Infinite loop */
}

Workflow

  1. Start conversion.
    adc_start_conversion(&adc_instance);
  2. Wait until conversion is done and read result.
    uint16_t result;
    do {
    /* Wait for conversion to be done and read out result */
    } while (adc_read(&adc_instance, &result) == STATUS_BUSY);
  3. Enter an infinite loop once the conversion is complete.
    while (1) {
    /* Infinite loop */
    }