This file provides an example for the ADCIFB on AVR32 UC3 devices.
Definition in file adcifb_example1.c.
#include "board.h"
#include "print_funcs.h"
#include "gpio.h"
#include "power_clocks_lib.h"
#include "adcifb.h"
Go to the source code of this file.
Defines | |
ADCIFB channels choice | |
#define | EXAMPLE_BATTERY_ADCIFB_CHANNEL_MASK AT32UC3L_EK_VBAT_ADC_CHANNEL |
#define | EXAMPLE_BATTERY_ADCIFB_FUNCTION AT32UC3L_EK_VBAT_FUNCTION |
#define | EXAMPLE_BATTERY_ADCIFB_PIN AT32UC3L_EK_VBAT_PIN |
Clocks frequencies | |
#define | EXAMPLE_TARGET_CLK_ADC_FREQ_HZ 1500000 |
#define | EXAMPLE_TARGET_DFLL_FREQ_HZ 96000000 |
#define | EXAMPLE_TARGET_MCUCLK_FREQ_HZ 12000000 |
#define | EXAMPLE_TARGET_PBACLK_FREQ_HZ 12000000 |
Functions | |
int | main (void) |
main function : do init and loop to display ADC values | |
Variables | |
Parameters to pcl_configure_clocks(). | |
static scif_gclk_opt_t | gc_dfllif_ref_opt = { SCIF_GCCTRL_SLOWCLOCK, 0, OFF} |
static pcl_freq_param_t | pcl_dfll_freq_param |
#define EXAMPLE_BATTERY_ADCIFB_CHANNEL_MASK AT32UC3L_EK_VBAT_ADC_CHANNEL |
#define EXAMPLE_BATTERY_ADCIFB_FUNCTION AT32UC3L_EK_VBAT_FUNCTION |
#define EXAMPLE_BATTERY_ADCIFB_PIN AT32UC3L_EK_VBAT_PIN |
#define EXAMPLE_TARGET_CLK_ADC_FREQ_HZ 1500000 |
#define EXAMPLE_TARGET_DFLL_FREQ_HZ 96000000 |
Definition at line 105 of file adcifb_example1.c.
#define EXAMPLE_TARGET_MCUCLK_FREQ_HZ 12000000 |
Definition at line 106 of file adcifb_example1.c.
#define EXAMPLE_TARGET_PBACLK_FREQ_HZ 12000000 |
int main | ( | void | ) |
main function : do init and loop to display ADC values
Definition at line 129 of file adcifb_example1.c.
References adcifb_channels_enable(), adcifb_configure(), adcifb_configure_trigger(), adcifb_get_last_data(), adcifb_is_drdy(), adcifb_is_ready(), adcifb_start_conversion_sequence(), EXAMPLE_BATTERY_ADCIFB_CHANNEL_MASK, EXAMPLE_BATTERY_ADCIFB_FUNCTION, EXAMPLE_BATTERY_ADCIFB_PIN, EXAMPLE_TARGET_CLK_ADC_FREQ_HZ, EXAMPLE_TARGET_PBACLK_FREQ_HZ, and pcl_dfll_freq_param.
00130 { 00131 volatile int i; 00132 // GPIO pin/adc-function map. 00133 static const gpio_map_t ADCIFB_GPIO_MAP = 00134 { 00135 {EXAMPLE_BATTERY_ADCIFB_PIN, EXAMPLE_BATTERY_ADCIFB_FUNCTION} 00136 }; 00137 volatile avr32_adcifb_t *adcifb = &AVR32_ADCIFB; // ADCIFB IP registers address 00138 // ADCIFB Configuration 00139 adcifb_opt_t adcifb_opt = { 00140 .resolution = AVR32_ADCIFB_ACR_RES_12BIT, // Resolution mode 00141 .shtim = 15, // Channels Sample & Hold Time in [0,15] 00142 .ratio_clkadcifb_clkadc = EXAMPLE_TARGET_PBACLK_FREQ_HZ/EXAMPLE_TARGET_CLK_ADC_FREQ_HZ, 00143 .startup = 3, // Startup time in [0,127], where Tstartup = startup * 8 * Tclk_adc (assuming Tstartup ~ 15us max) 00144 .sleep_mode_enable = FALSE // ADCIFB Sleep Mode disabled 00145 }; 00146 unsigned long int adc_data; 00147 00148 00149 // Note: on the AT32UC3L-EK board, there is no crystal/external clock connected 00150 // to the OSC0 pinout XIN0/XOUT0. We shall then program the DFLL and switch the 00151 // main clock source to the DFLL. 00152 pcl_configure_clocks(&pcl_dfll_freq_param); 00153 // Note: since it is dynamically computing the appropriate field values of the 00154 // configuration registers from the parameters structure, this function is not 00155 // optimal in terms of code size. For a code size optimal solution, it is better 00156 // to create a new function from pcl_configure_clocks_dfll0() and modify it 00157 // to use preprocessor computation from pre-defined target frequencies. 00158 00159 // init debug serial line 00160 init_dbg_rs232(EXAMPLE_TARGET_PBACLK_FREQ_HZ); 00161 00162 // Assign and enable GPIO pins to the ADC function. 00163 gpio_enable_module(ADCIFB_GPIO_MAP, sizeof(ADCIFB_GPIO_MAP) / sizeof(ADCIFB_GPIO_MAP[0])); 00164 00165 // Enable and configure the ADCIFB module 00166 if(PASS != adcifb_configure(adcifb, &adcifb_opt)) 00167 { 00168 // Config error. 00169 while(1) 00170 { 00171 gpio_tgl_gpio_pin(LED0_GPIO); 00172 for(i=100000; i; i--); // delay 00173 } 00174 } 00175 00176 // Configure the trigger mode as "No trigger, only software trigger can start conversions". 00177 if(PASS != adcifb_configure_trigger( adcifb, AVR32_ADCIFB_TRGMOD_NT, 0 )) 00178 { 00179 // Config error. 00180 while(1) 00181 { 00182 gpio_tgl_gpio_pin(LED1_GPIO); 00183 for(i=10000; i; i--); // delay 00184 } 00185 } 00186 00187 // Enable the ADCIFB channel the battery is connected to. 00188 adcifb_channels_enable( adcifb, EXAMPLE_BATTERY_ADCIFB_CHANNEL_MASK); 00189 00190 // do a loop 00191 for (;;) 00192 { 00193 // Wait until the ADC is ready to perform a conversion. 00194 while(FALSE == adcifb_is_ready(adcifb)); 00195 00196 // Start an ADCIFB conversion sequence. 00197 adcifb_start_conversion_sequence(adcifb); 00198 00199 // Wait until the converted data is available. 00200 while(FALSE == adcifb_is_drdy(adcifb)); 00201 00202 // Get the last converted data. 00203 adc_data = adcifb_get_last_data(adcifb); 00204 00205 // Display the current voltage of the battery. 00206 print_dbg("\x1B[2J\x1B[H\r\nADCIFB Example\r\nHEX Value for VBAT : 0x"); 00207 print_dbg_hex(adc_data&AVR32_ADCIFB_LCDR_LDATA_MASK); 00208 print_dbg("\r\n"); 00209 // Note1: there is a resistor bridge between the battery and the ADC pad on 00210 // the AT32UC3L-EK. The data converted is thus half of the battery voltage. 00211 00212 // Note2: if the battery is not in place, the conversion is out of spec because 00213 // the ADC input is then higher than ADVREF. 00214 } 00215 }
scif_gclk_opt_t gc_dfllif_ref_opt = { SCIF_GCCTRL_SLOWCLOCK, 0, OFF} [static] |
Definition at line 114 of file adcifb_example1.c.
pcl_freq_param_t pcl_dfll_freq_param [static] |
Initial value:
{ .main_clk_src = PCL_MC_DFLL0, .cpu_f = EXAMPLE_TARGET_MCUCLK_FREQ_HZ, .pba_f = EXAMPLE_TARGET_PBACLK_FREQ_HZ, .pbb_f = EXAMPLE_TARGET_PBACLK_FREQ_HZ, .dfll_f = EXAMPLE_TARGET_DFLL_FREQ_HZ, .pextra_params = &gc_dfllif_ref_opt }
Definition at line 115 of file adcifb_example1.c.
Referenced by main().