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.
Referenced by ISR().
|
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.
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.
int main | ( | void | ) |
References ADC_CH0, adc_enable(), adc_read_configuration(), ADC_REF_BANDGAP, ADC_RES_12, adc_set_clock_rate(), adc_set_conversion_parameters(), adc_set_conversion_trigger(), ADC_SIGN_OFF, adc_start_conversion(), ADC_TRIG_MANUAL, adc_wait_for_interrupt_flag(), adc_write_configuration(), adcch_disable_interrupt(), adcch_enable_interrupt(), ADCCH_MODE_COMPLETE, ADCCH_NEG_NONE, adcch_read_configuration(), adcch_set_input(), adcch_set_interrupt_mode(), adcch_write_configuration(), board_init(), cal_data, cpu_irq_enable, calibration_data::gain, get_mean_sample_value(), ioport_set_pin_high(), ioport_set_pin_low(), calibration_data::offset, sleepmgr_enter_sleep(), sleepmgr_init(), sysclk_init(), and wait_for_button().
|
static |
Delay for ms milliseconds.
ms | number of milliseconds to busy wait. |
References sysclk_get_cpu_hz().
Referenced by wait_for_button().
|
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().
|
static |
|
static |
Offset- and gain-compensated sample value.
Referenced by ISR().