Xmega Application Note


adc_example_interrupt.c File Reference


Detailed Description

XMEGA ADC driver interrupt example source.

This file contains an example application that demonstrates the ADC driver using interrupts. It shows how to set up the ADC to measure the voltage on four analog inputs. It uses the Internal reference of VCC/1.6 V as conversion reference. A number of samples are stored in an SRAM table for later examination.

Application note:
AVR1300: Using the XMEGA ADC
Documentation
For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html
Author:
Atmel Corporation: http://www.atmel.com
Support email: avr@atmel.com
Revision
Date

Copyright (c) 2008, Atmel Corporation All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. The name of ATMEL may not be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file adc_example_interrupt.c.

#include "avr_compiler.h"
#include "adc_driver.h"

Include dependency graph for adc_example_interrupt.c:

Go to the source code of this file.

Defines

#define SAMPLE_COUNT   100

Functions

 ISR (ADCA_CH3_vect)
int main (void)

Variables

int16_t adcSamples [4][SAMPLE_COUNT]
uint16_t interrupt_count = 0
int8_t offset


Define Documentation

#define SAMPLE_COUNT   100

How many samples for each ADC channel.

Definition at line 56 of file adc_example_interrupt.c.

Referenced by ISR(), and main().


Function Documentation

ISR ( ADCA_CH3_vect   ) 

Interrupt routine that reads out the result form the conversion on all channels to a global array. If the number of conversions carried out is less than the number given in the define SAMPLE_COUNT a new conversion on all the channels is started. If not the interrupt on ADC A channel 3 is disabled.

Definition at line 154 of file adc_example_interrupt.c.

References ADC_Ch_Interrupts_Config, ADC_Disable, ADC_FreeRunning_Disable, ADC_ResultCh_GetWord_Signed(), adcSamples, interrupt_count, offset, and SAMPLE_COUNT.

00155 {
00156         /*  Read samples and clear interrupt flags. */
00157         adcSamples[0][interrupt_count] = ADC_ResultCh_GetWord_Signed(&ADCA.CH0, offset);
00158         adcSamples[1][interrupt_count] = ADC_ResultCh_GetWord_Signed(&ADCA.CH1, offset);
00159         adcSamples[2][interrupt_count] = ADC_ResultCh_GetWord_Signed(&ADCA.CH2, offset);
00160         adcSamples[3][interrupt_count] = ADC_ResultCh_GetWord_Signed(&ADCA.CH3, offset);
00161 
00162 
00163         if(interrupt_count == SAMPLE_COUNT-1){
00164                 ADC_Ch_Interrupts_Config(&ADCA.CH3, ADC_CH_INTMODE_COMPLETE_gc, ADC_CH_INTLVL_OFF_gc);
00165                 ADC_FreeRunning_Disable(&ADCA);
00166                 ADC_Disable(&ADCA);
00167         }
00168         interrupt_count++;
00169 }

Here is the call graph for this function:

int main ( void   ) 

Definition at line 64 of file adc_example_interrupt.c.

References ADC_CalibrationValues_Load(), ADC_Ch_InputMode_and_Gain_Config, ADC_Ch_InputMux_Config, ADC_Ch_Interrupts_Config, ADC_ConvMode_and_Resolution_Config, ADC_ConvMode_Signed, ADC_Disable, ADC_DRIVER_CH_GAIN_NONE, ADC_Enable, ADC_FreeRunning_Enable, ADC_Offset_Get_Signed(), ADC_Prescaler_Config, ADC_Reference_Config, ADC_SweepChannels_Config, ADC_Wait_8MHz(), and offset.

00065 {
00066         /* Move stored calibration values to ADC A. */
00067         ADC_CalibrationValues_Load(&ADCA);
00068 
00069         /* Set up ADC A to have signed conversion mode and 12 bit resolution. */
00070         ADC_ConvMode_and_Resolution_Config(&ADCA, ADC_ConvMode_Signed, ADC_RESOLUTION_12BIT_gc);
00071 
00072         /* Set sample rate */
00073         ADC_Prescaler_Config(&ADCA, ADC_PRESCALER_DIV32_gc);
00074 
00075         /* Set referance voltage on ADC A to be VCC/1.6 V.*/
00076         ADC_Reference_Config(&ADCA, ADC_REFSEL_VCC_gc);
00077 
00078         /* Get offset value for ADC A. */
00079         ADC_Ch_InputMode_and_Gain_Config(&ADCA.CH0,
00080                                          ADC_CH_INPUTMODE_DIFF_gc,
00081                                          ADC_DRIVER_CH_GAIN_NONE);
00082 
00083         ADC_Ch_InputMux_Config(&ADCA.CH0, ADC_CH_MUXPOS_PIN1_gc, ADC_CH_MUXNEG_PIN1_gc);
00084 
00085         ADC_Enable(&ADCA);
00086         /* Wait until common mode voltage is stable. Default clk is 2MHz and
00087          * therefore below the maximum frequency to use this function. */
00088         ADC_Wait_8MHz(&ADCA);
00089         offset = ADC_Offset_Get_Signed(&ADCA, &ADCA.CH0, false);
00090     ADC_Disable(&ADCA);
00091     
00092         /* Setup channel 0, 1, 2 and 3 to have single ended input. */
00093         ADC_Ch_InputMode_and_Gain_Config(&ADCA.CH0,
00094                                          ADC_CH_INPUTMODE_SINGLEENDED_gc,
00095                                          ADC_DRIVER_CH_GAIN_NONE);
00096 
00097         ADC_Ch_InputMode_and_Gain_Config(&ADCA.CH1,
00098                                          ADC_CH_INPUTMODE_SINGLEENDED_gc,
00099                                          ADC_DRIVER_CH_GAIN_NONE);
00100 
00101         ADC_Ch_InputMode_and_Gain_Config(&ADCA.CH2,
00102                                          ADC_CH_INPUTMODE_SINGLEENDED_gc,
00103                                          ADC_DRIVER_CH_GAIN_NONE);
00104 
00105         ADC_Ch_InputMode_and_Gain_Config(&ADCA.CH3,
00106                                          ADC_CH_INPUTMODE_SINGLEENDED_gc,
00107                                          ADC_DRIVER_CH_GAIN_NONE);
00108     
00109         ADC_Ch_InputMode_and_Gain_Config(&ADCA.CH0,
00110                                          ADC_CH_INPUTMODE_SINGLEENDED_gc,
00111                                          ADC_DRIVER_CH_GAIN_NONE);
00112     
00113         /* Set input to the channels in ADC A to be PIN 4, 5, 6 and 7. */
00114         ADC_Ch_InputMux_Config(&ADCA.CH0, ADC_CH_MUXPOS_PIN4_gc, 0);
00115         ADC_Ch_InputMux_Config(&ADCA.CH1, ADC_CH_MUXPOS_PIN5_gc, 0);
00116         ADC_Ch_InputMux_Config(&ADCA.CH2, ADC_CH_MUXPOS_PIN6_gc, 0);
00117         ADC_Ch_InputMux_Config(&ADCA.CH3, ADC_CH_MUXPOS_PIN7_gc, 0);
00118 
00119         /* Setup sweep of all four virtual channels.*/
00120         ADC_SweepChannels_Config(&ADCA, ADC_SWEEP_0123_gc);
00121 
00122         /* Enable low level interrupts on ADCA channel 3, on conversion complete. */
00123         ADC_Ch_Interrupts_Config(&ADCA.CH3, ADC_CH_INTMODE_COMPLETE_gc, ADC_CH_INTLVL_LO_gc);
00124 
00125         /* Enable PMIC interrupt level low. */
00126         PMIC.CTRL |= PMIC_LOLVLEX_bm;
00127 
00128         /* Enable global interrupts. */
00129         sei();
00130 
00131         /* Enable ADC A with free running mode, VCC reference and signed conversion.*/
00132         ADC_Enable(&ADCA);
00133 
00134         /* Wait until common mode voltage is stable. Default clock is 2MHz and
00135          * therefore below the maximum frequency to use this function. */
00136         ADC_Wait_8MHz(&ADCA);
00137 
00138         /* Enable free running mode. */
00139         ADC_FreeRunning_Enable(&ADCA);
00140 
00141         /* Eternal loop to wait for the conversions and interrupts to finnish. */
00142         while(true){
00143         }
00144 }

Here is the call graph for this function:


Variable Documentation

int16_t adcSamples[4][SAMPLE_COUNT]

Sample storage (all four channels).

Definition at line 59 of file adc_example_interrupt.c.

Referenced by ISR(), and main().

uint16_t interrupt_count = 0

Definition at line 61 of file adc_example_interrupt.c.

Referenced by ISR().

int8_t offset

Definition at line 62 of file adc_example_interrupt.c.

Referenced by ADC_Offset_Get_Signed(), ADC_Offset_Get_Unsigned(), ISR(), and main().

@DOC_TITLE@
Generated on Tue Aug 4 13:31:15 2009 for AVR1300 Using the XMEGA ADC by doxygen 1.5.3