quick start guide for XMEGA ADC

时间:2022-07-11 12:56:23

This is the quick start guide for the Analog to Digital Converter (ADC), with step-by-step instructions on how to configure and use the driver in a selection of use cases.

The use cases are described with "setup" and "usage" sections, which each have "example code" and "workflow" subsections. This documentation first presents code fragments and function definitions along with instructions on where they can be placed, e.g., into the application C-file or the main() function, then follows up with explanations for all the lines of code.

Use cases

In addition to the basic use case below, refer to the following use cases for demonstrations of the ADC's features:

We recommend reading all the use cases for the sake of all the notes on considerations, limitations and other helpful details.

Basic use case

In this basic use case, ADCA is configured for:

  • sampling on a single channel (0)
    • I/O pin as single-ended input (PA0)
  • unsigned conversions
  • 12-bit resolution
  • internal 1V reference
  • manual conversion triggering
  • polled operation (no interrupts)

Completed conversions are detected by waiting for the relevant interrupt flag to get set. The ADC result is then stored in a local variable.

Setup steps

Example code

Add to application C-file:

#define MY_ADC ADCA
#define MY_ADC_CH ADC_CH0
 
static void adc_init(void)
{
struct adc_config adc_conf;
struct adc_channel_config adcch_conf;
 
adc_read_configuration(&MY_ADC, &adc_conf);
adcch_read_configuration(&MY_ADC, MY_ADC_CH, &adcch_conf);
 
adc_set_clock_rate(&adc_conf, 200000UL);
 
 
adc_write_configuration(&MY_ADC, &adc_conf);
adcch_write_configuration(&MY_ADC, MY_ADC_CH, &adcch_conf);
}

Add to main():

adc_init();

Workflow

  1. Add macros for the ADC and its channel to use, so they are easy to change:
    • #define MY_ADC ADCA
      #define MY_ADC_CH ADC_CH0
  2. Create a function adc_init() to intialize the ADC:
    • static void adc_init(void)
      {
      // ...
      }
  3. Allocate configuration structs for the ADC and its channel:
  4. Initialize the structs:
    • adc_read_configuration(&MY_ADC, &adc_conf);
      adcch_read_configuration(&MY_ADC, MY_ADC_CH, &adcch_conf);
      Attention
      This step must not be skipped because uninitialized structs may contain invalid configurations, thus giving unpredictable behavior.
  5. Set conversion parameters to unsigned, 12-bit and internal 1V reference:
  6. Set conversion trigger to manual triggering:
    • Note
      The number of channels to trigger (1) and base event channel (0) don't affect operation in this trigger mode, but sane values should still be supplied.
  7. Set ADC clock rate to 200 KHz or less:
    • adc_set_clock_rate(&adc_conf, 200000UL);
      Note
      The driver attempts to set the ADC clock rate to the fastest possible without exceeding the specified limit. Refer to the applicable device datasheet and manual for details on maximum ADC clock rate.
  8. Set pin 0 on the associated port as the single-ended input:
  9. Write the configurations to ADC and channel:
  10. Initialize the clock system:
    • Note
      The ADC driver requires the system clock driver to be initialized in order to compute the correct ADC clock rate in step 6.
  11. Call our ADC init function:
    • adc_init();

Usage steps

Example code

Add to, e.g., main-loop in application C-file:

uint16_t result;
 
adc_enable(&MY_ADC);
 
adc_start_conversion(&MY_ADC, MY_ADC_CH);
adc_wait_for_interrupt_flag(&MY_ADC, MY_ADC_CH);
 
result = adc_get_result(&MY_ADC, MY_ADC_CH);

Workflow

  1. Allocate a variable to contain the ADC result:
    • uint16_t result;
  2. Enable the configured ADC:
  3. Trigger a single conversion on the ADC channel:
  4. Wait for the channel's interrupt flag to get set, indicating a completed conversion:
    • adc_wait_for_interrupt_flag(&MY_ADC, MY_ADC_CH);
      Note
      The interrupt flags are set even if the interrupts are disabled. Further, this function will clear the interrupt flag after it has been set, so we do not need to clear it manually.
  5. Read out the result of the ADC channel:
  6. To do more conversions, go back to step 3.