Microchip® Advanced Software Framework

adc_oversampling.c File Reference

This file contains the function implementations for XMEGA ADC Oversampling Application.It shows how to use oversampling to increase the resolution.In this example configuration has been been selected for oversampling from 12 bit signed to 16 bit signed result.

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

#include <compiler.h>
#include <sleepmgr.h>
#include <sysclk.h>
#include <conf_oversampling.h>
#include <adc_oversampling.h>
#include <asf.h>

Functions

static void adc_handler (ADC_t *adc, uint8_t ch_mask, adc_result_t result)
 Callback function for ADCB-CH0 interrupts. More...
 
static int8_t adc_offset_get_signed (void)
 This function get the offset of the ADCB when it is configured in signed mode. More...
 
void adc_oversampled (void)
 This function processes sampled ADC values and calculate the oversampling result. More...
 
static void convert_to_ascii (uint8_t *buf_index, uint64_t dec_val)
 This Function converts a decimal value to ASCII. More...
 
void display_adccount (uint64_t adc_rawcount, uint8_t x_cordinate, uint8_t sign_flag)
 This Function Display ADC count on LCD. More...
 
void init_adc (void)
 This function initialize the ADCB,gets ADCB-CH0 offset and configure ADCB-CH0 for oversampling. More...
 

Variables

static struct adc_channel_config adc_ch_conf
 
static struct adc_config adc_conf
 
static int16_t adc_offset = 0
 Static variable to store total ADC Offset value for total number of sample. More...
 
static int8_t adc_offset_one_sample = 0
 
volatile bool adc_oversampled_flag = false
 Global variable/flag to indicate that one set of oversampling is done for start processing. More...
 
static int64_t adc_result_accum_processed = 0
 
static volatile int64_t adc_result_accumulator = 0
 
static volatile int64_t adc_result_one_sample = 0
 
static int64_t adc_result_one_sample_processed = 0
 
static volatile uint16_t adc_samplecount = 0
 
static int64_t v_input = 0
 Static variable to find analog value at ADC input after oversampling process. More...
 
static uint8_t v_input_ascii_buf [ASCII_BUFFER_SIZE] = {"+1.123456"}
 Static buffer variable to store ASCII value of calculated input voltage for display. More...
 
static int64_t v_input_one_sample = 0
 Static variable to find analog value at ADC Input without oversampling process. More...
 

static void adc_handler ( ADC_t *  adc,
uint8_t  ch_mask,
adc_result_t  result 
)
static

Callback function for ADCB-CH0 interrupts.

  • Interrupt is configured for Conversion Complete Interrupt
  • ADCA CH0 result is accumulated
  • ADC sample count is incremented
  • Check If ADC sample count reached up to number of oversampling required
  • If so, disable ADC interrupt and set flag to start oversampling process
Parameters
adcPointer to ADC module.
ch_maskADC channel mask.
resultConversion result from ADC channel.

References ADC_CH0, adc_ch_conf, adc_clear_interrupt_flag(), ADC_OVER_SAMPLED_NUMBER, adc_oversampled_flag, adc_result_accumulator, adc_result_one_sample, adc_samplecount, adcch_disable_interrupt(), and adcch_write_configuration().

Referenced by init_adc().

static int8_t adc_offset_get_signed ( void  )
static

This function get the offset of the ADCB when it is configured in signed mode.

Note
The ADC must be configured and enabled before this function is run.
Returns
Offset on the ADCB

References ADC_CH0, adc_get_result, adc_start_conversion(), and adc_wait_for_interrupt_flag().

Referenced by init_adc().

void adc_oversampled ( void  )

This function processes sampled ADC values and calculate the oversampling result.

Function to process sampled ADC values.

  • Offset error compensation is applied on accumulated ADC value
  • After, scaling is done with scaled factor.
  • Finally, Analog value at ADC input pin is calculated
  • Reset all variable used in ADC ISR and enable ADC interrupt to start next oversampling Process.

References abs, ADC_CH0, adc_ch_conf, ADC_GAIN_ERROR_FACTOR, ADC_NO_OVER_SAMP_MAX_COUNT, adc_offset, adc_offset_one_sample, ADC_OVER_SAMP_MAX_COUNT, ADC_OVER_SAMP_REF_VOLT_IN_MICRO, ADC_OVER_SAMP_SCALING_FACTOR, adc_result_accum_processed, adc_result_accumulator, adc_result_one_sample, adc_result_one_sample_processed, adc_samplecount, adcch_enable_interrupt(), ADCCH_MODE_COMPLETE, adcch_set_interrupt_mode(), adcch_write_configuration(), ASCII_BUFFER_SIZE, convert_to_ascii(), display_adccount(), gfx_mono_draw_string(), sysfont, v_input, v_input_ascii_buf, and v_input_one_sample.

Referenced by main().

static void convert_to_ascii ( uint8_t *  buf_index,
uint64_t  dec_val 
)
static

This Function converts a decimal value to ASCII.

  • It will extract each digit from decimal value and add to v_input_ascii_buf
  • It will take care to keep decimal point at correct position
    Parameters
    buf_indexPointer to buffer used to store ASCII value.
    dec_valDecimal to be converted as ASCII.

References NUMBER_OF_DIGITS_IN_RESULT.

Referenced by adc_oversampled().

void display_adccount ( uint64_t  adc_rawcount,
uint8_t  x_cordinate,
uint8_t  sign_flag 
)

This Function Display ADC count on LCD.

Function to display raw ADC count on LCD.

  • It will extract each digit from ADC count and convert to ASCII
  • Use GFX service for displaying each character
    Parameters
    adc_rawcountADC raw count value to be displayed.
    x_cordinateX-coordinate where display should end.
    sign_flagSign of ADC count.If negative sign_flag is set.

References gfx_mono_draw_char(), NUMBER_OF_DIGITS_IN_ADCCOUNT, and sysfont.

Referenced by adc_oversampled().

void init_adc ( void  )

This function initialize the ADCB,gets ADCB-CH0 offset and configure ADCB-CH0 for oversampling.

Function to initialize the ADC.

  • ADCB-CH0 is configured in 12bit, signed differential mode without gain
  • To read ADC offset, ADCB-Pin3(PB3) used as both +ve and -ve input
  • After reading ADC offset,to start oversampling,ADCB +ve and -ve input are configured

References ADC_CH0, adc_ch_conf, adc_conf, adc_disable(), adc_enable(), adc_handler(), adc_offset, adc_offset_get_signed(), adc_offset_one_sample, ADC_OVER_SAMP_NEGATIVE_PIN, ADC_OVER_SAMP_POSTIVE_PIN, ADC_OVER_SAMPLED_NUMBER, adc_read_configuration(), ADC_REF_AREFB, ADC_RES_12, adc_set_callback(), adc_set_clock_rate(), adc_set_conversion_parameters(), adc_set_conversion_trigger(), ADC_SIGN_ON, ADC_TRIG_MANUAL, adc_write_configuration(), adcch_enable_interrupt(), ADCCH_MODE_COMPLETE, ADCCH_NEG_PIN3, ADCCH_POS_PIN3, adcch_read_configuration(), adcch_set_input(), adcch_set_interrupt_mode(), and adcch_write_configuration().

Referenced by main().

struct adc_channel_config adc_ch_conf
static
struct adc_config adc_conf
static

Referenced by init_adc().

int16_t adc_offset = 0
static

Static variable to store total ADC Offset value for total number of sample.

Referenced by adc_oversampled(), and init_adc().

int8_t adc_offset_one_sample = 0
static

Referenced by adc_oversampled(), and init_adc().

volatile bool adc_oversampled_flag = false

Global variable/flag to indicate that one set of oversampling is done for start processing.

Static variable/flag to indicate that one set of oversampling is done for start processing.

Referenced by adc_handler(), and main().

int64_t adc_result_accum_processed = 0
static

Referenced by adc_oversampled().

volatile int64_t adc_result_accumulator = 0
static

Referenced by adc_handler(), and adc_oversampled().

volatile int64_t adc_result_one_sample = 0
static

Referenced by adc_handler(), and adc_oversampled().

int64_t adc_result_one_sample_processed = 0
static

Referenced by adc_oversampled().

volatile uint16_t adc_samplecount = 0
static

Referenced by adc_handler(), and adc_oversampled().

int64_t v_input = 0
static

Static variable to find analog value at ADC input after oversampling process.

Referenced by adc_oversampled().

uint8_t v_input_ascii_buf[ASCII_BUFFER_SIZE] = {"+1.123456"}
static

Static buffer variable to store ASCII value of calculated input voltage for display.

Referenced by adc_oversampled().

int64_t v_input_one_sample = 0
static

Static variable to find analog value at ADC Input without oversampling process.

Referenced by adc_oversampled().