adc_example.c File Reference


Detailed Description

ADC example driver for AVR UC3.

This file provides an example for the ADC on AVR UC3 UC3 devices.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file adc_example.c.

#include "board.h"
#include "print_funcs.h"
#include "gpio.h"
#include "pm.h"
#include "adc.h"

Go to the source code of this file.

Defines

ADC channels choice
#define EXAMPLE_ADC_LIGHT_CHANNEL   6
#define EXAMPLE_ADC_LIGHT_FUNCTION   AVR32_ADC_AD_6_FUNCTION
#define EXAMPLE_ADC_LIGHT_PIN   AVR32_ADC_AD_6_PIN
#define EXAMPLE_ADC_TEMPERATURE_CHANNEL   7
#define EXAMPLE_ADC_TEMPERATURE_FUNCTION   AVR32_ADC_AD_7_FUNCTION
#define EXAMPLE_ADC_TEMPERATURE_PIN   AVR32_ADC_AD_7_PIN

Functions

int main (void)
 main function : do init and loop to display ADC values

Variables

volatile int true_var = TRUE


Define Documentation

#define EXAMPLE_ADC_LIGHT_CHANNEL   6

Definition at line 124 of file adc_example.c.

Referenced by main().

#define EXAMPLE_ADC_LIGHT_FUNCTION   AVR32_ADC_AD_6_FUNCTION

Definition at line 126 of file adc_example.c.

Referenced by main().

#define EXAMPLE_ADC_LIGHT_PIN   AVR32_ADC_AD_6_PIN

Definition at line 125 of file adc_example.c.

Referenced by main().

#define EXAMPLE_ADC_TEMPERATURE_CHANNEL   7

Definition at line 120 of file adc_example.c.

Referenced by main().

#define EXAMPLE_ADC_TEMPERATURE_FUNCTION   AVR32_ADC_AD_7_FUNCTION

Definition at line 122 of file adc_example.c.

Referenced by main().

#define EXAMPLE_ADC_TEMPERATURE_PIN   AVR32_ADC_AD_7_PIN

Definition at line 121 of file adc_example.c.

Referenced by main().


Function Documentation

int main ( void   ) 

main function : do init and loop to display ADC values

Definition at line 144 of file adc_example.c.

References adc_configure(), adc_disable(), adc_enable(), adc_get_value(), adc_start(), EXAMPLE_ADC_LIGHT_CHANNEL, EXAMPLE_ADC_LIGHT_FUNCTION, EXAMPLE_ADC_LIGHT_PIN, EXAMPLE_ADC_TEMPERATURE_CHANNEL, EXAMPLE_ADC_TEMPERATURE_FUNCTION, EXAMPLE_ADC_TEMPERATURE_PIN, and true_var.

00145 {
00146   // GPIO pin/adc-function map.
00147   static const gpio_map_t ADC_GPIO_MAP =
00148   {
00149 #if BOARD == EVK1100 || BOARD == EVK1101
00150     {EXAMPLE_ADC_TEMPERATURE_PIN, EXAMPLE_ADC_TEMPERATURE_FUNCTION},
00151 #endif
00152     {EXAMPLE_ADC_LIGHT_PIN, EXAMPLE_ADC_LIGHT_FUNCTION},
00153 #if BOARD == EVK1100
00154     {EXAMPLE_ADC_POTENTIOMETER_PIN, EXAMPLE_ADC_POTENTIOMETER_FUNCTION}
00155 #endif
00156   };
00157 
00158   volatile avr32_adc_t *adc = &AVR32_ADC; // ADC IP registers address
00159 
00160 #if BOARD == EVK1100 || BOARD == EVK1101
00161   signed short adc_value_temp = -1;
00162 #endif
00163   signed short adc_value_light = -1;
00164 #if BOARD == EVK1100
00165   signed short adc_value_pot = -1;
00166 #endif
00167 
00168   // Assign the on-board sensors to their ADC channel.
00169 #if BOARD == EVK1100 || BOARD == EVK1101
00170   unsigned short adc_channel_temp = EXAMPLE_ADC_TEMPERATURE_CHANNEL;
00171 #endif
00172   unsigned short adc_channel_light = EXAMPLE_ADC_LIGHT_CHANNEL;
00173 #if BOARD == EVK1100
00174   unsigned short adc_channel_pot = EXAMPLE_ADC_POTENTIOMETER_CHANNEL;
00175 #endif
00176 
00177   int i;
00178 
00179 
00180   // switch to oscillator 0
00181   pm_switch_to_osc0(&AVR32_PM, FOSC0, OSC0_STARTUP);
00182 
00183   // init debug serial line
00184   init_dbg_rs232(FOSC0);
00185 
00186   // Assign and enable GPIO pins to the ADC function.
00187   gpio_enable_module(ADC_GPIO_MAP, sizeof(ADC_GPIO_MAP) / sizeof(ADC_GPIO_MAP[0]));
00188 
00189   // configure ADC
00190   // Lower the ADC clock to match the ADC characteristics (because we configured
00191   // the CPU clock to 12MHz, and the ADC clock characteristics are usually lower;
00192   // cf. the ADC Characteristic section in the datasheet).
00193   AVR32_ADC.mr |= 0x1 << AVR32_ADC_MR_PRESCAL_OFFSET;
00194   adc_configure(adc);
00195 
00196   // Enable the ADC channels.
00197 #if BOARD == EVK1100 || BOARD == EVK1101
00198   adc_enable(adc,adc_channel_temp);
00199 #endif
00200   adc_enable(adc,adc_channel_light);
00201 #if BOARD == EVK1100
00202   adc_enable(adc,adc_channel_pot);
00203 #endif
00204 
00205 
00206   // do an infinite loop
00207   while (true_var)    // use a volatile true variable to avoid warning on unreachable code
00208   {
00209     // slow down operations
00210     for ( i=0 ; i < 1000000 ; i++);
00211 
00212     // display a header to user
00213     print_dbg("\x1B[2J\x1B[H\r\nADC Example\r\n");
00214 
00215     // launch conversion on all enabled channels
00216     adc_start(adc);
00217     
00218 #if BOARD == EVK1100 || BOARD == EVK1101
00219     // get value for the temperature adc channel
00220     adc_value_temp = adc_get_value(adc, adc_channel_temp);
00221     // display value to user
00222     print_dbg("HEX Value for Channel temperature : 0x");
00223     print_dbg_hex(adc_value_temp);
00224     print_dbg("\r\n");
00225 #endif
00226 
00227     // get value for the light adc channel
00228     adc_value_light = adc_get_value(adc, adc_channel_light);
00229     // display value to user
00230     print_dbg("HEX Value for Channel light : 0x");
00231     print_dbg_hex(adc_value_light);
00232     print_dbg("\r\n");
00233 
00234 #if BOARD == EVK1100
00235     // get value for the potentiometer adc channel
00236     adc_value_pot = adc_get_value(adc, adc_channel_pot);
00237     // display value to user
00238     print_dbg("HEX Value for Channel pot : 0x");
00239     print_dbg_hex(adc_value_pot);
00240     print_dbg("\r\n");
00241 #endif
00242   }
00243   // Disable the ADC channels.
00244 #if BOARD == EVK1100 || BOARD == EVK1101
00245   adc_disable(adc,adc_channel_temp);
00246 #endif
00247   adc_disable(adc,adc_channel_light);
00248 #if BOARD == EVK1100
00249   adc_disable(adc,adc_channel_pot);
00250 #endif
00251   return 0;
00252 }


Variable Documentation

volatile int true_var = TRUE

Definition at line 139 of file adc_example.c.

Referenced by main().


Generated on Fri Feb 19 02:24:11 2010 for AVR32 UC3 - ADC Driver by  doxygen 1.5.5