Microchip® Advanced Software Framework

xmega/applications/xmega_dac_calibration/main.c File Reference

XMEGA DAC Calibration application Main File.

This is an example on how to calibrate the XMEGA DAC by using the internal ADC.

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

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

Macros

#define ADC_CH_TO_DAC_CH0   ADC_CH0
 
#define ADC_CH_TO_DAC_CH1   ADC_CH1
 
#define ADC_DAC_GAIN_TARGET   ((uint16_t)((2048*1.6)/4))
 
#define ADC_DAC_OFFSET_TARGET   ((uint16_t)((2048*1.6)/2))
 
#define AVG_SAMPLES   32
 
#define CALIBRATION_ADC   ADCA
 
#define DAC_OUTPUT_FOR_GAIN_CALIB   ((uint16_t)(4096/4))
 
#define DAC_OUTPUT_FOR_OFFSET_CALIB   ((uint16_t)(4096/2))
 

Functions

static int16_t adc_get_sample (uint8_t adc_ch)
 Wrapper function for getting and ADC sample. More...
 
static int16_t adc_get_sample_avg (uint8_t adc_ch, uint8_t samples)
 Wrapper function for getting averaged ADC measurements. More...
 
static void configure_adc (void)
 Configure the ADC for DAC calibration. More...
 
static void configure_dac (void)
 Configure the DAC for calibration. More...
 
static void configure_pins (void)
 Configure the DAC pins for analog operation only and as output. More...
 
static void dac_calibrate (void)
 Function for calibration the DAC using the internal ADC. More...
 
static void dac_calibrate_gain (uint8_t dac_ch)
 Gain calibrate a DAC channel. More...
 
static void dac_calibrate_offset (uint8_t dac_ch)
 Offset calibrate the DAC channel. More...
 
static void dac_set_channel_gain (DAC_t *dac, uint8_t dac_ch, uint8_t gain)
 Function for setting the gain calibration value. More...
 
static void dac_set_channel_offset (DAC_t *dac, uint8_t dac_ch, uint8_t offset)
 Function for setting the offset calibration value. More...
 
int main (void)
 Main application rutine. More...
 

Variables

int16_t adc_offset = 0
 

#define ADC_CH_TO_DAC_CH0   ADC_CH0
#define ADC_CH_TO_DAC_CH1   ADC_CH1
#define ADC_DAC_GAIN_TARGET   ((uint16_t)((2048*1.6)/4))

Referenced by dac_calibrate_gain().

#define ADC_DAC_OFFSET_TARGET   ((uint16_t)((2048*1.6)/2))

Referenced by dac_calibrate_offset().

#define AVG_SAMPLES   32
#define CALIBRATION_ADC   ADCA
#define DAC_OUTPUT_FOR_GAIN_CALIB   ((uint16_t)(4096/4))

Referenced by dac_calibrate_gain().

#define DAC_OUTPUT_FOR_OFFSET_CALIB   ((uint16_t)(4096/2))

Referenced by dac_calibrate_offset().

static int16_t adc_get_sample ( uint8_t  adc_ch)
static

Wrapper function for getting and ADC sample.

Parameters
adc_chwhich channel to get the ADC samples from /returns 16-bit signed ADC result

References adc_clear_interrupt_flag(), adc_get_result, adc_start_conversion(), adc_wait_for_interrupt_flag(), CALIBRATION_ADC, and result.

Referenced by adc_get_sample_avg().

static int16_t adc_get_sample_avg ( uint8_t  adc_ch,
uint8_t  samples 
)
static

Wrapper function for getting averaged ADC measurements.

Parameters
adc_chwhich ADC channel to get samples from e.g. ADC_CH0, ADC_CH1
samplesnumber of samples to average over /returns 16-bit signed result of the averaged ADC samples

References adc_get_sample(), and result.

Referenced by configure_adc(), dac_calibrate_gain(), and dac_calibrate_offset().

static void configure_dac ( void  )
static
static void configure_pins ( void  )
static

Configure the DAC pins for analog operation only and as output.

References ioport_configure_pin(), IOPORT_DIR_OUTPUT, and IOPORT_INPUT_DISABLE.

Referenced by dac_calibrate().

static void dac_calibrate ( void  )
static

Function for calibration the DAC using the internal ADC.

This function will calibrate the DAC using the internal ADC, please be aware that signals will be outputted on the DAC pins.

When calibrating always start with the offset then do gain calibration.

References adc_disable(), CALIBRATION_ADC, configure_adc(), configure_dac(), configure_pins(), dac_calibrate_gain(), dac_calibrate_offset(), DAC_CH0, and DAC_CH1.

Referenced by main().

static void dac_calibrate_gain ( uint8_t  dac_ch)
static

Gain calibrate a DAC channel.

Parameters
dac_chwhich DAC channel to calibrate, valid values are DAC_CH0 and DAC_CH1.

References ADC_CH_TO_DAC_CH0, ADC_CH_TO_DAC_CH1, ADC_DAC_GAIN_TARGET, adc_get_sample_avg(), adc_offset, Assert, AVG_SAMPLES, DAC_CH0, DAC_CH1, DAC_OUTPUT_FOR_GAIN_CALIB, dac_set_channel_gain(), dac_set_channel_value(), delay_us, and result.

Referenced by dac_calibrate().

static void dac_calibrate_offset ( uint8_t  dac_ch)
static

Offset calibrate the DAC channel.

Parameters
dac_chwhich DAC channel to calibrate, valid values are DAC_CH0 and DAC_CH1.

References ADC_CH_TO_DAC_CH0, ADC_CH_TO_DAC_CH1, ADC_DAC_OFFSET_TARGET, adc_get_sample_avg(), adc_offset, Assert, AVG_SAMPLES, DAC_CH0, DAC_CH1, DAC_OUTPUT_FOR_OFFSET_CALIB, dac_set_channel_offset(), dac_set_channel_value(), delay_us, and result.

Referenced by dac_calibrate().

static void dac_set_channel_gain ( DAC_t *  dac,
uint8_t  dac_ch,
uint8_t  gain 
)
static

Function for setting the gain calibration value.

Parameters
dacpointer to the DAC hardware module.
dac_chwhich channel to set gain calibration for (DAC_CH0, DAC_CH1)
gainthe calibration value to write.

References DAC_CH0, and DAC_CH1.

Referenced by dac_calibrate_gain().

static void dac_set_channel_offset ( DAC_t *  dac,
uint8_t  dac_ch,
uint8_t  offset 
)
static

Function for setting the offset calibration value.

Parameters
dacpointer to the DAC hardware module.
dac_chwhich channel to set gain calibration for (DAC_CH0, DAC_CH1)
offsetthe calibration value to write.

References DAC_CH0, and DAC_CH1.

Referenced by dac_calibrate_offset().

int main ( void  )