Microchip® Advanced Software Framework

adc_example.c File Reference

Analog-to-Digital Converter (ADC/ADC12B) example for SAM.

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

#include <string.h>
#include "asf.h"
#include "conf_board.h"

Macros

#define ADC_DONE_MASK   ( (1<<NUM_CHANNELS) - 1 )
 There are two peripherals ADC and ADC12B in SAM3U, you can select one of them. More...
 
#define BUFFER_SIZE   NUM_CHANNELS
 Size of the receive buffer and transmit buffer. More...
 
#define MENU_HEADER
 
#define STRING_EOL   "\r"
 ADC channel for potentiometer. More...
 
#define STRING_HEADER
 
#define VOLT_REF   (3300)
 Reference voltage for ADC, in mv. More...
 

Enumerations

enum  {
  TRIGGER_MODE_SOFTWARE = 0,
  TRIGGER_MODE_ADTRG,
  TRIGGER_MODE_TIMER
}
 ADC trigger modes. More...
 

Functions

static void configure_console (void)
 Configure UART console. More...
 
static void configure_time_trigger (void)
 Configure to trigger ADC by TIOA output of timer. More...
 
static void display_menu (void)
 Display ADC configuration menu. More...
 
int main (void)
 adc12 Application entry point. More...
 
static void mdelay (uint32_t ul_dly_ticks)
 Wait for the given number of milliseconds (using the dwTimeStamp generated by the SAM microcontrollers' system tick). More...
 
static void set_adc_test_mode (void)
 Set ADC test mode. More...
 
static void start_adc (void)
 Read converted data through PDC channel. More...
 
void SysTick_Handler (void)
 Systick handler. More...
 

Variables

enum { ... }  e_trigger_mode
 ADC trigger modes. More...
 
struct {
   uint8_t   uc_ch_num [NUM_CHANNELS]
 
   uint16_t   us_done
 
   uint16_t   us_value [NUM_CHANNELS]
 
g_adc_sample_data
 ADC sample data. More...
 
struct {
   uint8_t   uc_gain_en
 
   uint8_t   uc_offset_en
 
   uint8_t   uc_pdc_en
 
   uint8_t   uc_sequence_en
 
   uint8_t   uc_trigger_mode
 
g_adc_test_mode
 ADC test mode structure. More...
 
static volatile uint32_t gs_ul_ms_ticks = 0
 Global timestamp in milliseconds since start of application. More...
 

#define ADC_DONE_MASK   ( (1<<NUM_CHANNELS) - 1 )

There are two peripherals ADC and ADC12B in SAM3U, you can select one of them.

Total number of ADC channels in use ADC convention done mask.

Referenced by main().

#define BUFFER_SIZE   NUM_CHANNELS

Size of the receive buffer and transmit buffer.

Referenced by start_adc().

#define MENU_HEADER
Value:
"\n\r" \
"===================================================\n\r" \
"Menu: press a key to change the configuration.\n\r" \
"---------------------------------------------------------\n\r"

Referenced by display_menu().

#define STRING_EOL   "\r"

ADC channel for potentiometer.

#define STRING_HEADER
Value:
"-- ADC Example --\r\n" \
"-- "BOARD_NAME" --\r\n" \
"-- Compiled: "__DATE__" "__TIME__" --"STRING_EOL
#define BOARD_NAME
Definition: inc/app_init.h:140
#define STRING_EOL
ADC channel for potentiometer.
Definition: adc_example.c:177

Referenced by main().

#define VOLT_REF   (3300)

Reference voltage for ADC, in mv.

Referenced by main().

anonymous enum

ADC trigger modes.

Enumerator
TRIGGER_MODE_SOFTWARE 
TRIGGER_MODE_ADTRG 
TRIGGER_MODE_TIMER 

static void configure_console ( void  )
static
static void configure_time_trigger ( void  )
static

Configure to trigger ADC by TIOA output of timer.

References ADC_TRIG_TIO_CH_0, gpio_configure_pin, pmc_enable_periph_clk(), sysclk_get_cpu_hz(), tc_find_mck_divisor(), tc_init(), and tc_start().

Referenced by start_adc().

static void display_menu ( void  )
static

Display ADC configuration menu.

References g_adc_test_mode, MENU_HEADER, TRIGGER_MODE_ADTRG, TRIGGER_MODE_SOFTWARE, and TRIGGER_MODE_TIMER.

Referenced by main(), and set_adc_test_mode().

static void mdelay ( uint32_t  ul_dly_ticks)
static

Wait for the given number of milliseconds (using the dwTimeStamp generated by the SAM microcontrollers' system tick).

Parameters
ul_dly_ticksDelay to wait for, in milliseconds.

References gs_ul_ms_ticks.

Referenced by main().

static void set_adc_test_mode ( void  )
static
static void start_adc ( void  )
static

Read converted data through PDC channel.

Parameters
p_adcThe pointer of adc peripheral.
p_s_bufferThe destination buffer.
ul_sizeThe size of the buffer. Start ADC sample. Initialize ADC, set clock and timing, and set ADC to given mode.

References ADC_CHANNEL_POTENTIOMETER, adc_enable_channel(), adc_enable_interrupt(), ADC_GAINVALUE_0, ADC_GAINVALUE_2, adc_get_status(), adc_read_buffer(), ADC_TRIG_EXT, ADC_TRIG_SW, BUFFER_SIZE, configure_time_trigger(), g_adc_sample_data, g_adc_test_mode, gpio_configure_pin, pmc_enable_periph_clk(), sysclk_get_cpu_hz(), TRIGGER_MODE_ADTRG, TRIGGER_MODE_SOFTWARE, and TRIGGER_MODE_TIMER.

Referenced by main().

void SysTick_Handler ( void  )

Systick handler.

References gs_ul_ms_ticks.

enum { ... } e_trigger_mode

ADC trigger modes.

struct { ... } g_adc_sample_data

ADC sample data.

Referenced by main(), and start_adc().

struct { ... } g_adc_test_mode

ADC test mode structure.

Referenced by display_menu(), main(), set_adc_test_mode(), and start_adc().

volatile uint32_t gs_ul_ms_ticks = 0
static

Global timestamp in milliseconds since start of application.

Referenced by mdelay(), and SysTick_Handler().

uint8_t uc_ch_num[NUM_CHANNELS]
uint8_t uc_gain_en
uint8_t uc_offset_en
uint8_t uc_pdc_en
uint8_t uc_sequence_en
uint8_t uc_trigger_mode
uint16_t us_done
uint16_t us_value[NUM_CHANNELS]