Xmega Application Note


ADC module
[Analog to Digital Converter (ADC)]

Collaboration diagram for ADC module:

Data Structures

struct  adc_config
 ADC configuration. More...

Enumerations

enum  adc_calibration_data { ADC_CAL_ADCA, ADC_CAL_ADCB, ADC_CAL_TEMPSENSE }
 

ADC calibration data.

More...
enum  adc_reference { ADC_REF_BANDGAP = ADC_REFSEL_INT1V_gc, ADC_REF_VCC = ADC_REFSEL_VCC_gc, ADC_REF_AREFA = ADC_REFSEL_AREFA_gc, ADC_REF_AREFB = ADC_REFSEL_AREFB_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_SWEEP, ADC_TRIG_EVENT_SINGLE }
 

ADC conversion trigger settings.

More...

ADC module management



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

ADC module configuration



static void adc_disable_internal_input (struct adc_config *conf, uint8_t int_inp)
 Disable internal ADC input.
static void adc_enable_internal_input (struct adc_config *conf, uint8_t int_inp)
 Enable internal ADC input.
void adc_read_configuration (ADC_t *adc, struct adc_config *conf)
 Read configuration from ADC module.
static void adc_set_clock_rate (struct adc_config *conf, uint32_t clk_adc)
 Set ADC prescaler to get desired clock rate.
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.
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.
void adc_write_configuration (ADC_t *adc, const struct adc_config *conf)
 Write configuration to ADC module.
#define adc_get_config_compare_value(conf)   (conf->cmp)
 Get ADC compare value from configuration.
#define adc_set_config_compare_value(conf, val)
 Set ADC compare value in configuration.

ADC channel masks



#define ADC_CH0   (1U << 0)
 ADC channel 0.

Internal ADC input masks



#define ADC_INT_BANDGAP   ADC_BANDGAP_bm
 Bandgap reference.
#define ADC_INT_TEMPSENSE   ADC_TEMPREF_bm
 Temperature sensor.

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.
#define ADCACAL1   offsetof(NVM_PROD_SIGNATURES_t, ADCACAL1)
 ADC A, calibration byte 1.
#define ADCBCAL0   offsetof(NVM_PROD_SIGNATURES_t, ADCBCAL0)
 ADC B, calibration byte 0.
#define ADCBCAL1   offsetof(NVM_PROD_SIGNATURES_t, ADCBCAL1)
 ADC B, calibration byte 1.
#define TEMPSENSE0   offsetof(NVM_PROD_SIGNATURES_t, TEMPSENSE0)
 Temperature sensor calibration byte 0.
#define TEMPSENSE1   offsetof(NVM_PROD_SIGNATURES_t, TEMPSENSE1)
 Temperature sensor calibration byte 1.

Detailed Description

Management and configuration functions for the ADC module.

The API functions and definitions can be divided in three groups:


Define Documentation

#define ADC_CH0   (1U << 0)

ADC channel 0.

Definition at line 158 of file adc.h.

Referenced by main().

#define adc_get_compare_value ( adc   )     ((adc)->CMP)

Get compare value directly from ADC.

Gets the compare value directly from the ADC, for quick access while the ADC is enabled.

Parameters:
adc Pointer to ADC module.
Returns:
Current compare value of the ADC. Signedness does not matter.
Note:
This macro does not protect the 16-bit read from interrupts. If an interrupt may do a 16-bit read or write to the ADC while this macro is executing, interrupts must be temporarily disabled to avoid corruption of the read.

Definition at line 416 of file adc.h.

#define adc_get_config_compare_value ( conf   )     (conf->cmp)

Get ADC compare value from configuration.

Parameters:
conf Pointer to ADC module configuration.

Definition at line 727 of file adc.h.

#define ADC_INT_BANDGAP   ADC_BANDGAP_bm

Bandgap reference.

Definition at line 172 of file adc.h.

#define ADC_INT_TEMPSENSE   ADC_TEMPREF_bm

Temperature sensor.

Definition at line 171 of file adc.h.

#define adc_set_compare_value ( adc,
val   ) 
Value:
do {                                                                   \
                irqflags_t ATPASTE2(adc_flags, __LINE__) =  cpu_irq_save();    \
                (adc)->CMP = val;                                              \
                cpu_irq_restore(ATPASTE2(adc_flags, __LINE__));                \
        } while (0)

Set compare value directly to ADC.

Sets the compare value directly to the ADC, for quick access while the ADC is enabled.

Parameters:
adc Pointer to ADC module.
val Compare value to set, either signed or unsigned.
Note:
The ADC must be enabled for this function to have any effect.

Definition at line 394 of file adc.h.

#define adc_set_config_compare_value ( conf,
val   ) 
Value:
do {                                       \
                conf->cmp = (uint16_t)val;         \
        } while (0)

Set ADC compare value in configuration.

Parameters:
conf Pointer to ADC module configuration.
val Compare value to set.

Definition at line 717 of file adc.h.

#define ADCACAL0   offsetof(NVM_PROD_SIGNATURES_t, ADCACAL0)

ADC A, calibration byte 0.

Definition at line 129 of file adc.h.

Referenced by adc_get_calibration_data().

#define ADCACAL1   offsetof(NVM_PROD_SIGNATURES_t, ADCACAL1)

ADC A, calibration byte 1.

Definition at line 131 of file adc.h.

Referenced by adc_get_calibration_data().

#define ADCBCAL0   offsetof(NVM_PROD_SIGNATURES_t, ADCBCAL0)

ADC B, calibration byte 0.

Definition at line 133 of file adc.h.

Referenced by adc_get_calibration_data().

#define ADCBCAL1   offsetof(NVM_PROD_SIGNATURES_t, ADCBCAL1)

ADC B, calibration byte 1.

Definition at line 135 of file adc.h.

Referenced by adc_get_calibration_data().

#define TEMPSENSE0   offsetof(NVM_PROD_SIGNATURES_t, TEMPSENSE0)

Temperature sensor calibration byte 0.

Definition at line 137 of file adc.h.

Referenced by adc_get_calibration_data().

#define TEMPSENSE1   offsetof(NVM_PROD_SIGNATURES_t, TEMPSENSE1)

Temperature sensor calibration byte 1.

Definition at line 139 of file adc.h.

Referenced by adc_get_calibration_data().


Enumeration Type Documentation

ADC calibration data.

Enumerator:
ADC_CAL_ADCA 

ADC A pipeline calibration data.

ADC_CAL_ADCB 

ADC B pipeline calibration data.

ADC_CAL_TEMPSENSE 

Temperature sensor calibration data.

Note:
12-bit unsigned, measured at 85 degrees Celsius, equivalent to 358.15 kelvin.

Definition at line 144 of file adc.h.

00144                           {
00145         ADC_CAL_ADCA,    
00146         ADC_CAL_ADCB,    
00147 
00152         ADC_CAL_TEMPSENSE,
00153 };

ADC reference settings.

Enumerator:
ADC_REF_BANDGAP 

Internal 1 V from bandgap reference.

ADC_REF_VCC 

VCC divided by 1.6.

ADC_REF_AREFA 

External reference on AREFA pin.

ADC_REF_AREFB 

External reference on AREFB pin.

Definition at line 228 of file adc.h.

00228                    {
00230         ADC_REF_BANDGAP = ADC_REFSEL_INT1V_gc,
00232         ADC_REF_VCC     = ADC_REFSEL_VCC_gc,
00234         ADC_REF_AREFA   = ADC_REFSEL_AREFA_gc,
00236         ADC_REF_AREFB   = ADC_REFSEL_AREFB_gc,
00237 };

ADC resolution settings.

Enumerator:
ADC_RES_8 

8-bit resolution, right-adjusted.

ADC_RES_12 

12-bit resolution, right-adjusted.

ADC_RES_12_LEFT 

12-bit resolution, left-adjusted.

Definition at line 218 of file adc.h.

00218                     {
00220         ADC_RES_8       = ADC_RESOLUTION_8BIT_gc,
00222         ADC_RES_12      = ADC_RESOLUTION_12BIT_gc,
00224         ADC_RES_12_LEFT = ADC_RESOLUTION_LEFT12BIT_gc,
00225 };

enum adc_sign

ADC signedness settings.

Enumerator:
ADC_SIGN_OFF 

Unsigned conversions.

ADC_SIGN_ON 

Signed conversions.

Definition at line 212 of file adc.h.

00212               {
00213         ADC_SIGN_OFF,                    
00214         ADC_SIGN_ON = ADC_CONMODE_bm,    
00215 };

ADC conversion trigger settings.

Note:
The choice in conversion triggers varies between device families. Refer to the device manual for detailed information.
Enumerator:
ADC_TRIG_MANUAL 

Manually triggered conversions.

ADC_TRIG_FREERUN_SWEEP 

Freerunning conversion sweeps.

Note:
These will start as soon as the ADC is enabled.
ADC_TRIG_EVENT_SINGLE 

Event-triggered conversions on individual channels Pairs each event channel with an ADC channel.

Note:
The maximum base event channel that can be used is determined by the number of channels to trigger conversions on.

Definition at line 182 of file adc.h.

00182                  {
00184         ADC_TRIG_MANUAL,
00189         ADC_TRIG_FREERUN_SWEEP,
00196         ADC_TRIG_EVENT_SINGLE,
00197 #if XMEGA_A
00198 
00202         ADC_TRIG_EVENT_SWEEP,
00207         ADC_TRIG_EVENT_SYNCSWEEP,
00208 #endif
00209 };


Function Documentation

static void adc_clear_interrupt_flag ( ADC_t *  adc,
uint8_t  ch_mask 
) [inline, static]

Clear interrupt flag of ADC channel(s).

Parameters:
adc Pointer to ADC module.
ch_mask Mask of ADC channel(s):

  • ADC_CHn , where n specifies the channel. (These can be OR'ed together.)
Note:
The ADC must be enabled for this function to have any effect.

Definition at line 342 of file adc.h.

Referenced by adc_wait_for_interrupt_flag().

00343 {
00344         adc->INTFLAGS = ch_mask << ADC_CH0IF_bp;
00345 }

void adc_disable ( ADC_t *  adc  ) 

Disable ADC.

Disables the ADC and unlocks IDLE mode for the sleep manager.

Parameters:
adc Pointer to ADC module

Definition at line 296 of file adc.c.

References adc_disable_clock(), cpu_irq_restore(), cpu_irq_save(), SLEEPMGR_IDLE, and sleepmgr_unlock_mode().

Referenced by adc_ch3_callback(), and main().

00297 {
00298         irqflags_t flags = cpu_irq_save();
00299         adc->CTRLA &= ~ADC_ENABLE_bm;
00300         adc_disable_clock(adc);
00301         cpu_irq_restore(flags);
00302 
00303         sleepmgr_unlock_mode(SLEEPMGR_IDLE);
00304 }

Here is the call graph for this function:

static void adc_disable_internal_input ( struct adc_config conf,
uint8_t  int_inp 
) [inline, static]

Disable internal ADC input.

Parameters:
conf Pointer to ADC module configuration.
int_inp Internal input to disable:

  • ADC_INT_TEMPSENSE for temperature sensor.
  • ADC_INT_BANDGAP for bandgap reference.

Definition at line 705 of file adc.h.

References adc_config::refctrl.

00707 {
00708         conf->refctrl &= ~int_inp;
00709 }

void adc_enable ( ADC_t *  adc  ) 

Enable ADC.

Enables the ADC and locks IDLE mode for the sleep manager.

Parameters:
adc Pointer to ADC module
Note:
After enabling the ADC, one dummy conversion should be done to allow for its common mode voltage to settle.

Definition at line 279 of file adc.c.

References adc_enable_clock(), cpu_irq_restore(), cpu_irq_save(), SLEEPMGR_IDLE, and sleepmgr_lock_mode().

Referenced by main().

00280 {
00281         irqflags_t flags = cpu_irq_save();
00282         adc_enable_clock(adc);
00283         adc->CTRLA |= ADC_ENABLE_bm;
00284         cpu_irq_restore(flags);
00285 
00286         sleepmgr_lock_mode(SLEEPMGR_IDLE);
00287 }

Here is the call graph for this function:

static void adc_enable_internal_input ( struct adc_config conf,
uint8_t  int_inp 
) [inline, static]

Enable internal ADC input.

Parameters:
conf Pointer to ADC module configuration.
int_inp Internal input to enable:

  • ADC_INT_TEMPSENSE for temperature sensor.
  • ADC_INT_BANDGAP for bandgap reference.

Definition at line 691 of file adc.h.

References adc_config::refctrl.

00693 {
00694         conf->refctrl |= int_inp;
00695 }

static void adc_flush ( ADC_t *  adc  )  [inline, static]

Flush the ADC.

Forces the ADC to abort any ongoing conversions and restart its clock on the next peripheral clock cycle. Pending conversions are started after the clock reset.

Parameters:
adc Pointer to ADC module.
Note:
The ADC must be enabled for this function to have any effect.

Definition at line 376 of file adc.h.

References cpu_irq_restore(), and cpu_irq_save().

00377 {
00378         irqflags_t flags = cpu_irq_save();
00379         adc->CTRLA |= ADC_FLUSH_bm;
00380         cpu_irq_restore(flags);
00381 }

Here is the call graph for this function:

static uint16_t adc_get_calibration_data ( enum adc_calibration_data  cal  )  [inline, static]

Get calibration data.

Parameters:
cal Identifier for calibration data to get.

Definition at line 463 of file adc.h.

References ADC_CAL_ADCA, ADC_CAL_ADCB, ADC_CAL_TEMPSENSE, ADCACAL0, ADCACAL1, ADCBCAL0, ADCBCAL1, Assert, nvm_read_production_signature_row(), TEMPSENSE0, and TEMPSENSE1.

Referenced by adc_write_configuration(), and main().

00464 {
00465         uint16_t data;
00466 
00467         switch (cal) {
00468 #ifdef ADCA
00469         case ADC_CAL_ADCA:
00470                 data = nvm_read_production_signature_row(ADCACAL1);
00471                 data <<= 8;
00472                 data |= nvm_read_production_signature_row(ADCACAL0);
00473                 break;
00474 #endif
00475 
00476 #ifdef ADCB
00477         case ADC_CAL_ADCB:
00478                 data = nvm_read_production_signature_row(ADCBCAL1);
00479                 data <<= 8;
00480                 data |= nvm_read_production_signature_row(ADCBCAL0);
00481                 break;
00482 #endif
00483 
00484 #if defined(ADCA) || defined(ADCB)
00485         case ADC_CAL_TEMPSENSE:
00486                 data = nvm_read_production_signature_row(TEMPSENSE1);
00487                 data <<= 8;
00488                 data |= nvm_read_production_signature_row(TEMPSENSE0);
00489                 break;
00490 #endif
00491 
00492         default:
00493                 Assert(0);
00494                 data = 0;
00495         }
00496 
00497         return data;
00498 }

Here is the call graph for this function:

static uint8_t adc_get_interrupt_flag ( ADC_t *  adc,
uint8_t  ch_mask 
) [inline, static]

Get interrupt flag of ADC channel(s).

Returns the interrupt flag of the masked channels. The meaning of the interrupt flag depends on what mode the individual channels are in.

Parameters:
adc Pointer to ADC module.
ch_mask Mask of ADC channel(s):

  • ADC_CHn , where n specifies the channel. (These can be OR'ed together.)
Returns:
Mask with interrupt flags.

Definition at line 327 of file adc.h.

Referenced by adc_wait_for_interrupt_flag().

00328 {
00329         return (adc->INTFLAGS >> ADC_CH0IF_bp) & ch_mask;
00330 }

static int16_t adc_get_signed_compare_value ( ADC_t *  adc  )  [inline, static]

Get signed compare value directly from ADC.

Gets the signed compare value directly from the ADC, with interrupt protection of the 16-bit read, for quick access while the ADC is enabled.

Parameters:
adc Pointer to ADC module.

Definition at line 426 of file adc.h.

References cpu_irq_restore(), and cpu_irq_save().

00427 {
00428         int16_t val;
00429         irqflags_t flags;
00430 
00431         flags = cpu_irq_save();
00432         val = adc->CMP;
00433         cpu_irq_restore(flags);
00434 
00435         return val;
00436 }

Here is the call graph for this function:

static uint16_t adc_get_unsigned_compare_value ( ADC_t *  adc  )  [inline, static]

Get unsigned compare value directly from ADC.

Gets the unsigned compare value directly from the ADC, with interrupt protection of the 16-bit read, for quick access while the ADC is enabled.

Parameters:
adc Pointer to ADC module.

Definition at line 446 of file adc.h.

References cpu_irq_restore(), and cpu_irq_save().

00447 {
00448         uint16_t val;
00449         irqflags_t flags;
00450 
00451         flags = cpu_irq_save();
00452         val = adc->CMP;
00453         cpu_irq_restore(flags);
00454 
00455         return val;
00456 }

Here is the call graph for this function:

bool adc_is_enabled ( ADC_t *  adc  ) 

Check if the ADC is enabled.

Parameters:
adc Pointer to ADC module.
Return values:
true if ADC is enabled.
false if ADC is disabled.

Definition at line 314 of file adc.c.

References Assert.

00315 {
00316         /* It is sufficient to return the state of the ADC enable counters
00317          * since all driver functions that change the counts are protected
00318          * against interrupts and only the enable/disable functions leave the
00319          * counts incremented/decremented upon return.
00320          */
00321 #ifdef ADCA
00322         if ((uintptr_t)adc == (uintptr_t)&ADCA) {
00323                 return adca_enable_count;
00324         } else
00325 #endif
00326 
00327 #ifdef ADCB
00328         if ((uintptr_t)adc == (uintptr_t)&ADCB) {
00329                 return adcb_enable_count;
00330         } else
00331 #endif
00332 
00333         {
00334                 Assert(0);
00335                 return false;
00336         }
00337 }

void adc_read_configuration ( ADC_t *  adc,
struct adc_config conf 
)

Read configuration from ADC module.

Reads out the current configuration of the ADC module to the specified buffer.

Parameters:
adc Pointer to ADC module.
conf Pointer to ADC module configuration.

Definition at line 405 of file adc.c.

References adc_disable_clock(), adc_enable_clock(), adc_config::cmp, cpu_irq_restore(), cpu_irq_save(), adc_config::ctrlb, adc_config::evctrl, adc_config::prescaler, and adc_config::refctrl.

00406 {
00407         irqflags_t flags = cpu_irq_save();
00408 
00409         adc_enable_clock(adc);
00410 
00411 #if XMEGA_A
00412         conf->ctrla = adc->CTRLA & ADC_DMASEL_gm;
00413 #endif
00414 
00415         conf->cmp = adc->CMP;
00416         conf->refctrl = adc->REFCTRL;
00417         conf->prescaler = adc->PRESCALER;
00418         conf->evctrl = adc->EVCTRL;
00419         conf->ctrlb = adc->CTRLB;
00420 
00421         adc_disable_clock(adc);
00422 
00423         cpu_irq_restore(flags);
00424 }

Here is the call graph for this function:

static void adc_set_clock_rate ( struct adc_config conf,
uint32_t  clk_adc 
) [inline, static]

Set ADC prescaler to get desired clock rate.

Sets the ADC prescaling so that its clock rate becomes _at most_ clk_adc_hz. This is done by computing the ratio of the peripheral clock rate to the desired ADC clock rate, and rounding it upward to the nearest prescaling factor.

Parameters:
conf Pointer to ADC module configuration.
clk_adc Desired ADC clock rate.
Note:
The sample rate is not determined solely by the ADC clock rate for all devices. Refer to the device manual for detailed information on conversion timing.

Definition at line 523 of file adc.h.

References Assert, adc_config::prescaler, and sysclk_get_per_hz().

Referenced by main().

00524 {
00525         uint32_t clk_per;
00526         uint16_t ratio;
00527         uint8_t  psc;
00528 
00529         Assert(clk_adc);
00530 #if XMEGA_A
00531         Assert(clk_adc <= 2000000UL);
00532 #elif XMEGA_D
00533         Assert(clk_adc <= 1400000UL);
00534 #endif
00535 
00536         clk_per = sysclk_get_per_hz();
00537         ratio = clk_per / clk_adc;
00538 
00539         // Round ratio up to the nearest prescaling factor.
00540         if(ratio <= 4) {
00541                 psc = ADC_PRESCALER_DIV4_gc;
00542         } else if (ratio <= 8) {
00543                 psc = ADC_PRESCALER_DIV8_gc;
00544         } else if (ratio <= 16) {
00545                 psc = ADC_PRESCALER_DIV16_gc;
00546         } else if (ratio <= 32) {
00547                 psc = ADC_PRESCALER_DIV32_gc;
00548         } else if (ratio <= 64) {
00549                 psc = ADC_PRESCALER_DIV64_gc;
00550         } else if (ratio <= 128) {
00551                 psc = ADC_PRESCALER_DIV128_gc;
00552         } else if (ratio <= 256) {
00553                 psc = ADC_PRESCALER_DIV256_gc;
00554         } else {
00555                 psc = ADC_PRESCALER_DIV512_gc;
00556         }
00557 
00558         conf->prescaler = psc;
00559 }

Here is the call graph for this function:

static void adc_set_conversion_parameters ( struct adc_config conf,
enum adc_sign  sign,
enum adc_resolution  res,
enum adc_reference  ref 
) [inline, static]

Set ADC conversion parameters.

Sets the signedness, resolution and voltage reference for conversions in the ADC module configuration.

Parameters:
conf Pointer to ADC module configuration.
sign Conversion signedness.
res Resolution of conversions.
ref Voltage reference to use.

Definition at line 572 of file adc.h.

References adc_config::ctrlb, and adc_config::refctrl.

Referenced by main().

00575 {
00576         // Preserve all but conversion and resolution config.
00577         conf->ctrlb &= ~(ADC_CONMODE_bm | ADC_RESOLUTION_gm);
00578         conf->ctrlb |= (uint8_t)res | (uint8_t)sign;
00579 
00580         conf->refctrl &= ~ADC_REFSEL_gm;
00581         conf->refctrl |= ref;
00582 }

static void adc_set_conversion_trigger ( struct adc_config conf,
enum adc_trigger  trig,
uint8_t  nr_of_ch,
uint8_t  base_ev_ch 
) [inline, static]

Set ADC conversion trigger.

Configures the conversion triggering of the ADC.

For automatic triggering modes, the number of channels to start conversions on must be specified with nr_of_ch. The channel selection for these modes is incrementally inclusive, always starting with channel 0.

For event triggered modes, the base event channel must also be specified with base_ev_ch. The event channels are assigned to the ADC channels in an incremental fashion without wrap-around (in single-trigger event mode). This means that the maximum base event channel that can be used is determined by the number of ADC channels to start conversions on, i.e., nr_of_ch.

Parameters:
conf Pointer to ADC module configuration.
trig Conversion trigger to set.
nr_of_ch Number of ADC channels to trigger conversions on:

  • 1 - ADC_NR_OF_CHANNELS (must be non-zero).
base_ev_ch Base event channel, if used.

Definition at line 605 of file adc.h.

References ADC_TRIG_EVENT_SINGLE, ADC_TRIG_FREERUN_SWEEP, ADC_TRIG_MANUAL, Assert, adc_config::ctrlb, and adc_config::evctrl.

Referenced by adc_ch3_callback(), and main().

00607 {
00608         Assert(nr_of_ch);
00609         Assert(nr_of_ch <= ADC_NR_OF_CHANNELS);
00610 #if XMEGA_A
00611         Assert(base_ev_ch <= 7);
00612 #elif XMEGA_D
00613         Assert(base_ev_ch <= 3);
00614 #endif
00615 
00616         switch (trig) {
00617         case ADC_TRIG_MANUAL:
00618                 conf->ctrlb &= ~ADC_FREERUN_bm;
00619                 conf->evctrl = ADC_EVACT_NONE_gc;
00620                 break;
00621 
00622         case ADC_TRIG_FREERUN_SWEEP:
00623                 conf->ctrlb |= ADC_FREERUN_bm;
00624                 conf->evctrl = (nr_of_ch - 1) << ADC_SWEEP_gp;
00625                 break;
00626 
00627         case ADC_TRIG_EVENT_SINGLE:
00628                 conf->ctrlb &= ~ADC_FREERUN_bm;
00629                 conf->evctrl = (base_ev_ch << ADC_EVSEL_gp) |
00630                                 (nr_of_ch << ADC_EVACT_gp);
00631                 break;
00632 
00633 #if XMEGA_A
00634         case ADC_TRIG_EVENT_SWEEP:
00635                 conf->ctrlb &= ~ADC_FREERUN_bm;
00636                 conf->evctrl = (nr_of_ch - 1) << ADC_SWEEP_gp |
00637                                 (base_ev_ch << ADC_EVSEL_gp) |
00638                                 ADC_EVACT_SWEEP_gc;
00639                 break;
00640 
00641         case ADC_TRIG_EVENT_SYNCSWEEP:
00642                 conf->ctrlb &= ~ADC_FREERUN_bm;
00643                 conf->evctrl = ((nr_of_ch - 1) << ADC_SWEEP_gp) |
00644                                 (base_ev_ch << ADC_EVSEL_gp) |
00645                                 ADC_EVACT_SYNCHSWEEP_gc;
00646                 break;
00647 #endif
00648 
00649         default:
00650                 Assert(0);
00651         }
00652 }

static void adc_start_conversion ( ADC_t *  adc,
uint8_t  ch_mask 
) [inline, static]

Start one-shot conversion on ADC channel(s).

Parameters:
adc Pointer to ADC module.
ch_mask Mask of ADC channel(s):

  • ADC_CHn , where n specifies the channel. (These can be OR'ed together.)
Note:
The ADC must be enabled for this function to have any effect.

Definition at line 307 of file adc.h.

References cpu_irq_restore(), and cpu_irq_save().

Referenced by main().

00308 {
00309         irqflags_t flags = cpu_irq_save();
00310         adc->CTRLA |= ch_mask << ADC_CH0START_bp;
00311         cpu_irq_restore(flags);
00312 }

Here is the call graph for this function:

static void adc_wait_for_interrupt_flag ( ADC_t *  adc,
uint8_t  ch_mask 
) [inline, static]

Wait for interrupt flag of ADC channel(s).

Waits for the interrupt flag of the specified channel(s) to be set, then clears it before returning. If several channels are masked, the function will wait for all interrupt flags to be set.

Parameters:
adc Pointer to ADC module.
ch_mask Mask of ADC channel(s):

  • ADC_CHn , where n specifies the channel. (These can be OR'ed together.)

Definition at line 359 of file adc.h.

References adc_clear_interrupt_flag(), and adc_get_interrupt_flag().

Referenced by main().

00360 {
00361         do { } while (adc_get_interrupt_flag(adc, ch_mask) != ch_mask);
00362         adc_clear_interrupt_flag(adc, ch_mask);
00363 }

Here is the call graph for this function:

void adc_write_configuration ( ADC_t *  adc,
const struct adc_config conf 
)

Write configuration to ADC module.

Disables the ADC and flushes its pipeline before writing the specified configuration and factory calibration value to it. If the ADC was enabled upon entry of the function, it is enabled upon function return.

Parameters:
adc Pointer to ADC module.
conf Pointer to ADC module configuration.

Definition at line 349 of file adc.c.

References ADC_CAL_ADCA, ADC_CAL_ADCB, adc_disable_clock(), adc_enable_clock(), adc_get_calibration_data(), Assert, adc_config::cmp, cpu_irq_restore(), cpu_irq_save(), adc_config::ctrlb, adc_config::evctrl, adc_config::prescaler, and adc_config::refctrl.

Referenced by adc_ch3_callback(), and main().

00350 {
00351         uint16_t cal;
00352         uint8_t  enable;
00353         irqflags_t flags;
00354 
00355 #ifdef ADCA
00356         if ((uintptr_t)adc == (uintptr_t)&ADCA) {
00357                 cal = adc_get_calibration_data(ADC_CAL_ADCA);
00358         } else
00359 #endif
00360 
00361 #ifdef ADCB
00362         if ((uintptr_t)adc == (uintptr_t)&ADCB) {
00363                 cal = adc_get_calibration_data(ADC_CAL_ADCB);
00364         } else
00365 #endif
00366 
00367         {
00368                 Assert(0);
00369                 return;
00370         }
00371 
00372         flags = cpu_irq_save();
00373         adc_enable_clock(adc);
00374 
00375         enable = adc->CTRLA & ADC_ENABLE_bm;
00376 
00377         adc->CTRLA = ADC_FLUSH_bm;
00378         adc->CAL = cal;
00379         adc->CMP = conf->cmp;
00380         adc->REFCTRL = conf->refctrl;
00381         adc->PRESCALER = conf->prescaler;
00382         adc->EVCTRL = conf->evctrl;
00383         adc->CTRLB = conf->ctrlb;
00384 
00385 #if XMEGA_A
00386         adc->CTRLA = enable | conf->ctrla;
00387 #elif XMEGA_D
00388         adc->CTRLA = enable;
00389 #endif
00390 
00391         adc_disable_clock(adc);
00392 
00393         cpu_irq_restore(flags);
00394 }

Here is the call graph for this function:

@DOC_TITLE@
Generated on Fri Oct 22 12:15:26 2010 for AVR1300 Using the Xmega ADC by doxygen 1.6.3