Microchip® Advanced Software Framework

adc_example2.c File Reference

AVR XMEGA Analog to Digital Converter Driver Example 2.

Copyright (c) 2010-2018 Microchip Technology Inc. and its subsidiaries.

#include <conf_example.h>
#include <asf.h>

Data Structures

struct  calibration_data
 ADC calibration data. More...
 

Macros

#define IDEAL_GAIN_RESULT   3686
 Ideal ADC result at 0.9 V. More...
 

Functions

static uint16_t get_mean_sample_value (void)
 Get mean sample value. More...
 
 ISR (ADCA_CH0_vect)
 Interrupt Service Routine for channel 0 on ADC A. More...
 
int main (void)
 
static void mdelay (uint16_t ms)
 Delay for ms milliseconds. More...
 
static void wait_for_button (void)
 Wait for button press and release. More...
 

Variables

static struct calibration_data cal_data
 
static volatile uint16_t last_sample
 Offset- and gain-compensated sample value. More...
 

#define IDEAL_GAIN_RESULT   3686

Ideal ADC result at 0.9 V.

This factor is given by the following equation:

\[ R_\textnormal{ideal}(U) = 2^{12} \cdot \frac{U_\textnormal{measured}} {U_\textnormal{reference}} \]

For a 1 V reference and 0.9 V input, the expected result from an unsigned, 12-bit conversion is 3686.

Note
To make the example compute the voltage in milliVolts, change this factor to 900, corresponding to the 900 mV that the gain error measurements are done at.

Referenced by ISR().

static uint16_t get_mean_sample_value ( void  )
static

Get mean sample value.

Performs 2 to the power of OVERSAMPLING_FACTOR successive conversions, and computes the mean value of the resulting sample values.

Returns
Mean sample value.

References ADC_CH0, adc_get_result, adc_start_conversion(), and adc_wait_for_interrupt_flag().

Referenced by main().

ISR ( ADCA_CH0_vect  )

Interrupt Service Routine for channel 0 on ADC A.

Upon completed AD conversion, this ISR computes a calibrated sample from the ADC result, then stores it in a global variable before triggering a new conversion.

References ADC_CH0, adc_get_result, adc_start_conversion(), cal_data, calibration_data::gain, IDEAL_GAIN_RESULT, last_sample, and calibration_data::offset.

static void mdelay ( uint16_t  ms)
static

Delay for ms milliseconds.

Parameters
msnumber of milliseconds to busy wait.

References sysclk_get_cpu_hz().

Referenced by wait_for_button().

static void wait_for_button ( void  )
static

Wait for button press and release.

Waits for the button connected to BUTTON_PIN to be pressed and released. Debouncing is done with an approximate 20 ms delay after both the press and the release.

References ioport_pin_is_high(), ioport_pin_is_low(), and mdelay().

Referenced by main().

struct calibration_data cal_data
static

Referenced by ISR(), and main().

volatile uint16_t last_sample
static

Offset- and gain-compensated sample value.

Referenced by ISR().