Microchip® Advanced Software Framework

adc.h File Reference

AVR XMEGA Analog to Digital Converter driver.

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

#include <compiler.h>
#include <conf_adc.h>
#include <nvm.h>
#include <parts.h>
#include <sleepmgr.h>
#include <sysclk.h>

Data Structures

struct  adc_channel_config
 ADC channel configuration. More...
 
struct  adc_config
 ADC configuration. More...
 

Macros

#define ADC_CH_GAIN_DIV2_gc   (0x07 << 2)
 
#define ADC_CH_OFFSET_gp   4 /* Positive MUX setting offset group position. */
 
#define ADC_EVACT_SYNCSWEEP_gc   (0x06 << 0)
 
#define ADC_REFSEL_INTVCC_gc   (0x01 << 4)
 
#define ADC_REFSEL_VCCDIV2_gc   (0x04 << 4)
 
#define CONFIG_ADC_INTLVL   ADC_CH_INTLVL_LO_gc
 Default ADC channel interrupt level. More...
 
#define SCAN   reserved_0x6
 
Calibration data addresses
Note
The temperature sensor calibration is sampled at 85 degrees Celsius with unsigned, 12-bit conversion.
#define ADCACAL0   offsetof(NVM_PROD_SIGNATURES_t, ADCACAL0)
 ADC A, calibration byte 0. More...
 
#define ADCACAL1   offsetof(NVM_PROD_SIGNATURES_t, ADCACAL1)
 ADC A, calibration byte 1. More...
 
#define ADCBCAL0   offsetof(NVM_PROD_SIGNATURES_t, ADCBCAL0)
 ADC B, calibration byte 0. More...
 
#define ADCBCAL1   offsetof(NVM_PROD_SIGNATURES_t, ADCBCAL1)
 ADC B, calibration byte 1. More...
 
#define TEMPSENSE0   offsetof(NVM_PROD_SIGNATURES_t, TEMPSENSE0)
 Temperature sensor calibration byte 0. More...
 
#define TEMPSENSE1   offsetof(NVM_PROD_SIGNATURES_t, TEMPSENSE1)
 Temperature sensor calibration byte 1. More...
 
#define HOTTEMP   offsetof(NVM_PROD_SIGNATURES_t, HOTTEMP)
 Temperature at which TEMPSENSE1/TEMPSENSE0 is measured. More...
 
#define ROOMTEMP   offsetof(NVM_PROD_SIGNATURES_t, ROOMTEMP)
 Temperature at which TEMPSENSE3/TEMPSENSE2 is measured. More...
 
#define TEMPSENSE2   offsetof(NVM_PROD_SIGNATURES_t, TEMPSENSE2)
 Temperature sensor calibration byte 2. More...
 
#define TEMPSENSE3   offsetof(NVM_PROD_SIGNATURES_t, TEMPSENSE3)
 Temperature sensor calibration byte 3. More...
 
ADC channel masks
#define ADC_CH0   (1U << 0)
 ADC channel 0. More...
 
Internal ADC input masks
#define ADC_INT_TEMPSENSE   ADC_TEMPREF_bm
 Temperature sensor. More...
 
#define ADC_INT_BANDGAP   ADC_BANDGAP_bm
 Bandgap reference. More...
 

Enumerations

enum  adc_calibration_data {
  ADC_CAL_ADCA,
  ADC_CAL_ADCB,
  ADC_CAL_TEMPSENSE,
  ADC_CAL_HOTTEMP,
  ADC_CAL_TEMPSENSE2,
  ADC_CAL_ROOMTEMP
}
 ADC calibration data. More...
 
enum  adc_reference {
  ADC_REF_BANDGAP = ADC_REFSEL_INT1V_gc,
  ADC_REF_VCC = ADC_REFSEL_INTVCC_gc,
  ADC_REF_AREFA = ADC_REFSEL_AREFA_gc,
  ADC_REF_AREFB = ADC_REFSEL_AREFB_gc,
  ADC_REF_VCCDIV2 = ADC_REFSEL_VCCDIV2_gc
}
 ADC reference settings. More...
 
enum  adc_resolution {
  ADC_RES_8 = ADC_RESOLUTION_8BIT_gc,
  ADC_RES_12 = ADC_RESOLUTION_12BIT_gc,
  ADC_RES_12_LEFT = ADC_RESOLUTION_LEFT12BIT_gc
}
 ADC resolution settings. More...
 
enum  adc_sign {
  ADC_SIGN_OFF,
  ADC_SIGN_ON = ADC_CONMODE_bm
}
 ADC signedness settings. More...
 
enum  adc_trigger {
  ADC_TRIG_MANUAL,
  ADC_TRIG_FREERUN,
  ADC_TRIG_EVENT_SINGLE,
  ADC_TRIG_EVENT_SYNCSWEEP
}
 ADC conversion trigger settings. More...
 
enum  adcch_mode {
  ADCCH_MODE_COMPLETE = ADC_CH_INTMODE_COMPLETE_gc,
  ADCCH_MODE_BELOW = ADC_CH_INTMODE_BELOW_gc,
  ADCCH_MODE_ABOVE = ADC_CH_INTMODE_ABOVE_gc
}
 ADC channel interrupt modes. More...
 
enum  adcch_negative_input {
  ADCCH_NEG_PIN0,
  ADCCH_NEG_PIN1,
  ADCCH_NEG_PIN2,
  ADCCH_NEG_PIN3,
  ADCCH_NEG_PIN4,
  ADCCH_NEG_PIN5,
  ADCCH_NEG_PIN6,
  ADCCH_NEG_PIN7,
  ADCCH_NEG_PAD_GND,
  ADCCH_NEG_INTERNAL_GND,
  ADCCH_NEG_NONE
}
 ADC channel negative input. More...
 
enum  adcch_positive_input {
  ADCCH_POS_PIN0,
  ADCCH_POS_PIN1,
  ADCCH_POS_PIN2,
  ADCCH_POS_PIN3,
  ADCCH_POS_PIN4,
  ADCCH_POS_PIN5,
  ADCCH_POS_PIN6,
  ADCCH_POS_PIN7,
  ADCCH_POS_PIN8,
  ADCCH_POS_PIN9,
  ADCCH_POS_PIN10,
  ADCCH_POS_PIN11,
  ADCCH_POS_PIN12,
  ADCCH_POS_PIN13,
  ADCCH_POS_PIN14,
  ADCCH_POS_PIN15,
  ADCCH_POS_TEMPSENSE,
  ADCCH_POS_BANDGAP,
  ADCCH_POS_SCALED_VCC,
  ADCCH_POS_DAC
}
 ADC channel positive input. More...
 

Functions

Internal functions for driver
static __always_inline ADC_CH_t * adc_get_channel (ADC_t *adc, uint8_t ch_mask)
 Get ADC channel pointer from channel mask. More...
 

ADC interrupt callback function

#define CONFIG_ADC_CALLBACK_ENABLE
 Configuration symbol to enable callback on ADC interrupts. More...
 
#define CONFIG_ADC_CALLBACK_TYPE   uint16_t
 Configuration symbol for datatype of result parameter for callback. More...
 
typedef CONFIG_ADC_CALLBACK_TYPE adc_result_t
 Datatype of ADC conversion result parameter for callback. More...
 
typedef void(* adc_callback_t )(ADC_t *adc, uint8_t ch_mask, adc_result_t res)
 ADC interrupt callback function pointer. More...
 
void adc_set_callback (ADC_t *adc, adc_callback_t callback)
 Set ADC interrupt callback function. More...
 

ADC module management

#define adc_get_result(adc, ch_mask)   (adc_get_channel(adc, ch_mask)->RES)
 Get result from ADC channel. More...
 
#define adc_set_compare_value(adc, val)
 Set compare value directly to ADC. More...
 
#define adc_get_compare_value(adc)   ((adc)->CMP)
 Get compare value directly from ADC. More...
 
void adc_enable (ADC_t *adc)
 Enable ADC. More...
 
void adc_disable (ADC_t *adc)
 Disable ADC. More...
 
bool adc_is_enabled (ADC_t *adc)
 Check if the ADC is enabled. More...
 
static void adc_start_conversion (ADC_t *adc, uint8_t ch_mask)
 Start one-shot conversion on ADC channel(s) More...
 
static int16_t adc_get_signed_result (ADC_t *adc, uint8_t ch_mask)
 Get signed result from ADC channel. More...
 
static uint16_t adc_get_unsigned_result (ADC_t *adc, uint8_t ch_mask)
 Get unsigned result from ADC channel. More...
 
static uint8_t adc_get_interrupt_flag (ADC_t *adc, uint8_t ch_mask)
 Get interrupt flag of ADC channel(s) More...
 
static void adc_clear_interrupt_flag (ADC_t *adc, uint8_t ch_mask)
 Clear interrupt flag of ADC channel(s) More...
 
static void adc_wait_for_interrupt_flag (ADC_t *adc, uint8_t ch_mask)
 Wait for interrupt flag of ADC channel(s) More...
 
static void adc_flush (ADC_t *adc)
 Flush the ADC. More...
 
static int16_t adc_get_signed_compare_value (ADC_t *adc)
 Get signed compare value directly from ADC. More...
 
static uint16_t adc_get_unsigned_compare_value (ADC_t *adc)
 Get unsigned compare value directly from ADC. More...
 
static void adc_set_sample_value (ADC_t *adc, uint8_t val)
 Set sample time value directly to ADC. More...
 
static uint8_t adc_get_sample_value (ADC_t *adc)
 Get sample time value directly from ADC. More...
 
static uint16_t adc_get_calibration_data (enum adc_calibration_data cal)
 Get calibration data. More...
 

ADC module configuration

#define adc_set_config_compare_value(conf, val)
 Set ADC compare value in configuration. More...
 
#define adc_get_config_compare_value(conf)   (conf->cmp)
 Get ADC compare value from configuration. More...
 
void adc_write_configuration (ADC_t *adc, const struct adc_config *conf)
 Write configuration to ADC module. More...
 
void adc_read_configuration (ADC_t *adc, struct adc_config *conf)
 Read configuration from ADC module. More...
 
static void adc_set_clock_rate (struct adc_config *conf, uint32_t clk_adc)
 Set ADC prescaler to get desired clock rate. More...
 
static void adc_set_conversion_parameters (struct adc_config *conf, enum adc_sign sign, enum adc_resolution res, enum adc_reference ref)
 Set ADC conversion parameters. More...
 
static void adc_set_conversion_trigger (struct adc_config *conf, enum adc_trigger trig, uint8_t nr_of_ch, uint8_t base_ev_ch)
 Set ADC conversion trigger. More...
 
static void adc_enable_internal_input (struct adc_config *conf, uint8_t int_inp)
 Enable internal ADC input. More...
 
static void adc_disable_internal_input (struct adc_config *conf, uint8_t int_inp)
 Disable internal ADC input. More...
 

ADC channel configuration

#define ADCCH_FORCE_1X_GAINSTAGE   0xff
 Force enabling of gainstage with unity gain. More...
 
enum  adcch_sampnum {
  ADC_SAMPNUM_2X = ADC_SAMPNUM_2X_gc,
  ADC_SAMPNUM_4X = ADC_SAMPNUM_4X_gc,
  ADC_SAMPNUM_8X = ADC_SAMPNUM_8X_gc,
  ADC_SAMPNUM_16X = ADC_SAMPNUM_16X_gc,
  ADC_SAMPNUM_32X = ADC_SAMPNUM_32X_gc,
  ADC_SAMPNUM_64X = ADC_SAMPNUM_64X_gc,
  ADC_SAMPNUM_128X = ADC_SAMPNUM_128X_gc,
  ADC_SAMPNUM_256X = ADC_SAMPNUM_256X_gc,
  ADC_SAMPNUM_512X = ADC_SAMPNUM_512X_gc,
  ADC_SAMPNUM_1024X = ADC_SAMPNUM_1024X_gc
}
 ADC channel sample number settings. More...
 
void adcch_write_configuration (ADC_t *adc, uint8_t ch_mask, const struct adc_channel_config *ch_conf)
 Write configuration to ADC channel. More...
 
void adcch_read_configuration (ADC_t *adc, uint8_t ch_mask, struct adc_channel_config *ch_conf)
 Read configuration from ADC channel. More...
 
static uint8_t adcch_get_gain_setting (uint8_t gain)
 Get ADC channel setting for specified gain. More...
 
static void adcch_set_input (struct adc_channel_config *ch_conf, enum adcch_positive_input pos, enum adcch_negative_input neg, uint8_t gain)
 Set ADC channel input mode, multiplexing and gain. More...
 
static void adcch_set_pin_scan (struct adc_channel_config *ch_conf, uint8_t start_offset, uint8_t max_offset)
 Set ADC channel 0 pin scan. More...
 
static void adcch_set_interrupt_mode (struct adc_channel_config *ch_conf, enum adcch_mode mode)
 Set ADC channel interrupt mode. More...
 
static void adcch_enable_interrupt (struct adc_channel_config *ch_conf)
 Enable interrupts on ADC channel. More...
 
static void adcch_disable_interrupt (struct adc_channel_config *ch_conf)
 Disable interrupts on ADC channel. More...
 
static void adcch_enable_correction (struct adc_channel_config *ch_conf, uint16_t offset_corr, uint16_t expected_value, uint16_t captured_value)
 Enable gain & offset corrections on ADC channel. More...
 
static void adcch_disable_correction (struct adc_channel_config *ch_conf)
 Disable gain & offset correction on ADC channel. More...
 
static void adcch_enable_averaging (struct adc_channel_config *ch_conf, enum adcch_sampnum sample)
 Enables ADC channel averaging. More...
 
static void adcch_disable_averaging (struct adc_channel_config *ch_conf)
 Disables ADC channel averaging. More...
 
static void adcch_enable_oversampling (struct adc_channel_config *ch_conf, enum adcch_sampnum sample, uint8_t resolution)
 Enables ADC channel over-sampling. More...
 
static void adcch_disable_oversampling (struct adc_channel_config *ch_conf)
 Disables ADC channel over-sampling. More...
 

#define ADC_CH_GAIN_DIV2_gc   (0x07 << 2)

Referenced by adcch_get_gain_setting().

#define ADC_CH_OFFSET_gp   4 /* Positive MUX setting offset group position. */

Referenced by adcch_set_pin_scan().

#define ADC_EVACT_SYNCSWEEP_gc   (0x06 << 0)
#define ADC_REFSEL_INTVCC_gc   (0x01 << 4)
#define ADC_REFSEL_VCCDIV2_gc   (0x04 << 4)
#define SCAN   reserved_0x6