Xmega Application Note


ADC channel
[Analog to Digital Converter (ADC)]

Collaboration diagram for ADC channel:

Data Structures

struct  adc_channel_config
 ADC channel configuration. More...

Defines

#define CONFIG_ADC_INTLVL   ADC_CH_INTLVL_LO_gc
 Default ADC channel interrupt level.

Enumerations

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_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_TEMPSENSE, ADCCH_POS_BANDGAP, ADCCH_POS_SCALED_VCC
}
 

ADC channel positive input.

More...

ADC channel configuration



static void adcch_disable_interrupt (struct adc_channel_config *ch_conf)
 Disable interrupts on ADC channel.
static void adcch_enable_interrupt (struct adc_channel_config *ch_conf)
 Enable interrupts on ADC channel.
static uint8_t adcch_get_gain_setting (uint8_t gain)
 Get ADC channel setting for specified gain.
void adcch_read_configuration (ADC_t *adc, uint8_t ch, struct adc_channel_config *ch_conf)
 Read configuration from ADC channel.
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.
static void adcch_set_interrupt_mode (struct adc_channel_config *ch_conf, enum adcch_mode mode)
 Set ADC channel interrupt mode.
void adcch_write_configuration (ADC_t *adc, uint8_t ch, const struct adc_channel_config *ch_conf)
 Write configuration to ADC channel.

ADC channel management



static int16_t adcch_get_signed_result (ADC_t *adc, uint8_t ch)
 Get signed result from ADC channel.
static uint16_t adcch_get_unsigned_result (ADC_t *adc, uint8_t ch)
 Get unsigned result from ADC channel.
#define adcch_get_result(adc, ch)   ((&(adc)->CH0 + ch)->RES)
 Get result from ADC channel.

Detailed Description

Management and configuration functions for the individual ADC channels.

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


Define Documentation

#define adcch_get_result ( adc,
ch   )     ((&(adc)->CH0 + ch)->RES)

Get result from ADC channel.

Gets the latest conversion result from the ADC channel.

Parameters:
adc Pointer to ADC module.
ch ADC channel number.
Returns:
Latest conversion result of ADC channel. 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 866 of file adc.h.

Referenced by main().

#define CONFIG_ADC_INTLVL   ADC_CH_INTLVL_LO_gc

Default ADC channel interrupt level.

Note:
To override the channel interrupt level, define this symbol as the desired level in conf_intlvl.h.

Definition at line 753 of file adc.h.

Referenced by adcch_enable_interrupt().


Enumeration Type Documentation

enum adcch_mode

ADC channel interrupt modes.

Enumerator:
ADCCH_MODE_COMPLETE 

Set interrupt flag when conversions complete.

ADCCH_MODE_BELOW 

Set interrupt flag when conversion result is below compare value.

ADCCH_MODE_ABOVE 

Set interrupt flag when conversion result is above compare value.

Definition at line 839 of file adc.h.

00839                 {
00841         ADCCH_MODE_COMPLETE = ADC_CH_INTMODE_COMPLETE_gc,
00843         ADCCH_MODE_BELOW    = ADC_CH_INTMODE_BELOW_gc,
00845         ADCCH_MODE_ABOVE    = ADC_CH_INTMODE_ABOVE_gc,
00846 };

ADC channel negative input.

Identifies the external signals that can be used as negative input to the ADC channels in differential mode.

Note:
The ADC must be set in signed mode to use differential measurements. For single-ended measurements.
Enumerator:
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_NONE 

Input for single-ended measurements.

Definition at line 818 of file adc.h.

00818                           {
00820 
00821         ADCCH_NEG_PIN0,
00822         ADCCH_NEG_PIN1,
00823         ADCCH_NEG_PIN2,
00824         ADCCH_NEG_PIN3,
00826 
00828 
00829         ADCCH_NEG_PIN4,
00830         ADCCH_NEG_PIN5,
00831         ADCCH_NEG_PIN6,
00832         ADCCH_NEG_PIN7,
00834 
00835         ADCCH_NEG_NONE,          
00836 };

ADC channel positive input.

Identifies the external and internal signals that can be used as positive input to the ADC channels.

Note:
Not all devices can use pins on an alternate port. Refer to the device datasheet for information on the number of available ADC inputs.
Enumerator:
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_TEMPSENSE 

Temperature sensor.

ADCCH_POS_BANDGAP 

Bandgap reference.

ADCCH_POS_SCALED_VCC 

VCC scaled down by 10.

Definition at line 772 of file adc.h.

00772                           {
00773         ADCCH_POS_PIN0,
00774         ADCCH_POS_PIN1,
00775         ADCCH_POS_PIN2,
00776         ADCCH_POS_PIN3,
00777         ADCCH_POS_PIN4,
00778         ADCCH_POS_PIN5,
00779         ADCCH_POS_PIN6,
00780         ADCCH_POS_PIN7,
00781 
00783 
00784 #if XMEGA_A4 || XMEGA_D
00785         ADCCH_POS_PIN8,
00786         ADCCH_POS_PIN9,
00787         ADCCH_POS_PIN10,
00788         ADCCH_POS_PIN11,
00789 #endif
00790 #if XMEGA_D3
00791         ADCCH_POS_PIN12,
00792         ADCCH_POS_PIN13,
00793         ADCCH_POS_PIN14,
00794         ADCCH_POS_PIN15,
00795 #endif
00796 
00797 
00799 
00800         ADCCH_POS_TEMPSENSE,     
00801         ADCCH_POS_BANDGAP,       
00802         ADCCH_POS_SCALED_VCC,    
00803 #if XMEGA_A
00804         ADCCH_POS_DAC,           
00805 #endif
00806 
00807 };


Function Documentation

static void adcch_disable_interrupt ( struct adc_channel_config ch_conf  )  [inline, static]

Disable interrupts on ADC channel.

Parameters:
ch_conf Pointer to ADC channel configuration.

Definition at line 1058 of file adc.h.

References adc_channel_config::intctrl.

Referenced by adc_ch3_callback().

01059 {
01060         ch_conf->intctrl &= ~ADC_CH_INTLVL_gm;
01061         ch_conf->intctrl |= ADC_CH_INTLVL_OFF_gc;
01062 }

static void adcch_enable_interrupt ( struct adc_channel_config ch_conf  )  [inline, static]

Enable interrupts on ADC channel.

Parameters:
ch_conf Pointer to ADC channel configuration.

Definition at line 1047 of file adc.h.

References CONFIG_ADC_INTLVL, and adc_channel_config::intctrl.

Referenced by main().

01048 {
01049         ch_conf->intctrl &= ~ADC_CH_INTLVL_gm;
01050         ch_conf->intctrl |= CONFIG_ADC_INTLVL;
01051 }

static uint8_t adcch_get_gain_setting ( uint8_t  gain  )  [inline, static]

Get ADC channel setting for specified gain.

For internal use only.

Returns the setting that corresponds to specified gain.

Parameters:
gain Valid gain factor for the measurement.
Returns:
Gain setting of type ADC_CH_GAIN_t.

Definition at line 940 of file adc.h.

References Assert.

Referenced by adcch_set_input().

00941 {
00942         switch (gain) {
00943         case 1: return ADC_CH_GAIN_1X_gc;
00944 
00945         case 2: return ADC_CH_GAIN_2X_gc;
00946 
00947         case 4: return ADC_CH_GAIN_4X_gc;
00948 
00949         case 8: return ADC_CH_GAIN_8X_gc;
00950 
00951         case 16: return ADC_CH_GAIN_16X_gc;
00952 
00953         case 32: return ADC_CH_GAIN_32X_gc;
00954 
00955         case 64: return ADC_CH_GAIN_64X_gc;
00956 
00957         default:
00958                 Assert(0);
00959                 return 0;
00960         }
00961 }

static int16_t adcch_get_signed_result ( ADC_t *  adc,
uint8_t  ch 
) [inline, static]

Get signed result from ADC channel.

Returns the latest conversion result from the ADC channel as a signed type, with interrupt protection of the 16-bit read.

Parameters:
adc Pointer to ADC module.
ch ADC channel number.
Returns:
Latest conversion result of ADC channel, as signed 16-bit integer.

Definition at line 879 of file adc.h.

References cpu_irq_restore(), and cpu_irq_save().

Referenced by adc_ch3_callback(), and main().

00880 {
00881         int16_t    val;
00882         irqflags_t flags;
00883         ADC_CH_t   *adc_ch;
00884 
00885         adc_ch = &adc->CH0 + ch;
00886 
00887         flags = cpu_irq_save();
00888         val = adc_ch->RES;
00889         cpu_irq_restore(flags);
00890 
00891         return val;
00892 }

Here is the call graph for this function:

static uint16_t adcch_get_unsigned_result ( ADC_t *  adc,
uint8_t  ch 
) [inline, static]

Get unsigned result from ADC channel.

Returns the latest conversion result from the ADC channel as an unsigned type, with interrupt protection of the 16-bit read.

Parameters:
adc Pointer to ADC module.
ch ADC channel number.
Returns:
Latest conversion result of ADC channel, as unsigned 16-bit integer.

Definition at line 905 of file adc.h.

References cpu_irq_restore(), and cpu_irq_save().

00906 {
00907         uint16_t   val;
00908         irqflags_t flags;
00909         ADC_CH_t   *adc_ch;
00910 
00911         adc_ch = &adc->CH0 + ch;
00912 
00913         flags = cpu_irq_save();
00914         val = adc_ch->RES;
00915         cpu_irq_restore(flags);
00916 
00917         return val;
00918 }

Here is the call graph for this function:

void adcch_read_configuration ( ADC_t *  adc,
uint8_t  ch,
struct adc_channel_config ch_conf 
)

Read configuration from ADC channel.

Reads out the current configuration from the ADC channel to the specified buffer.

Parameters:
adc Pointer to ADC module.
ch ADC channel number.
ch_conf Pointer to ADC channel configuration.

Definition at line 490 of file adc.c.

References adc_disable_clock(), adc_enable_clock(), Assert, cpu_irq_restore(), cpu_irq_save(), adc_channel_config::ctrl, adc_channel_config::intctrl, and adc_channel_config::muxctrl.

00492 {
00493         ADC_CH_t *adc_ch;
00494         irqflags_t flags;
00495 
00496         Assert(ch < ADC_NR_OF_CHANNELS);
00497         adc_ch = &adc->CH0 + ch;
00498 
00499         flags = cpu_irq_save();
00500 
00501         adc_enable_clock(adc);
00502         ch_conf->ctrl = adc_ch->CTRL;
00503         ch_conf->intctrl = adc_ch->INTCTRL;
00504         ch_conf->muxctrl = adc_ch->MUXCTRL;
00505         adc_disable_clock(adc);
00506 
00507         cpu_irq_restore(flags);
00508 }

Here is the call graph for this function:

static void adcch_set_input ( struct adc_channel_config ch_conf,
enum adcch_positive_input  pos,
enum adcch_negative_input  neg,
uint8_t  gain 
) [inline, static]

Set ADC channel input mode, multiplexing and gain.

Sets up an ADC channel's input mode and multiplexing according to specified input signals, as well as the gain.

Parameters:
ch_conf Pointer to ADC channel configuration.
pos Positive input signal.
neg Negative input signal:

  • ADCCH_NEG_NONE for single-ended measurements.
  • ADCCH_NEG_PINn , where n specifies a pin, for differential measurements.
gain Gain factor for measurements:

  • 1 for single-ended or differential with pin 0, 1, 2 or 3 as negative input.
  • 1, 2, 4, 8, 16, 32 or 64 for differential with pin 4, 5, 6 or 7 as negative input.

Definition at line 981 of file adc.h.

References adcch_get_gain_setting(), ADCCH_NEG_NONE, ADCCH_NEG_PIN4, ADCCH_POS_TEMPSENSE, Assert, adc_channel_config::ctrl, and adc_channel_config::muxctrl.

Referenced by main().

00984 {
00985         Assert(gain);
00986 
00987         // Configure for internal input.
00988         if (pos >= ADCCH_POS_TEMPSENSE) {
00989                 Assert(gain == 1);
00990                 Assert(neg == ADCCH_NEG_NONE);
00991 
00992                 ch_conf->ctrl = ADC_CH_INPUTMODE_INTERNAL_gc;
00993                 ch_conf->muxctrl = (pos - ADCCH_POS_TEMPSENSE) <<
00994                                 ADC_CH_MUXPOS_gp;
00995                 return;
00996         }
00997 
00998         // Configure for single-ended measurement.
00999         if (neg == ADCCH_NEG_NONE) {
01000                 Assert(gain == 1);
01001 
01002                 ch_conf->ctrl = ADC_CH_INPUTMODE_SINGLEENDED_gc;
01003                 ch_conf->muxctrl = pos << ADC_CH_MUXPOS_gp;
01004 
01005         // Configure for differential measurement.
01006         } else {
01007                 /* Pins 0-3 can only be used for negative input if the gain
01008                  * stage is not used, i.e., unity gain.
01009                  */
01010                 if (neg < ADCCH_NEG_PIN4) {
01011                         Assert(gain == 1);
01012 
01013                         ch_conf->ctrl = ADC_CH_INPUTMODE_DIFF_gc;
01014                         ch_conf->muxctrl = (pos << ADC_CH_MUXPOS_gp) |
01015                                         (neg << ADC_CH_MUXNEG_gp);
01016                 } else {
01017                 /* Pins 4-7 can be used for all gain settings, including unity
01018                  * gain, which is available even if the gain stage is active.
01019                  */
01020                         ch_conf->ctrl = ADC_CH_INPUTMODE_DIFFWGAIN_gc |
01021                                         adcch_get_gain_setting(gain);
01022                         ch_conf->muxctrl = (pos << ADC_CH_MUXPOS_gp) |
01023                                         ((neg - ADCCH_NEG_PIN4) <<
01024                                         ADC_CH_MUXNEG_gp);
01025                 }
01026         }
01027 }

Here is the call graph for this function:

static void adcch_set_interrupt_mode ( struct adc_channel_config ch_conf,
enum adcch_mode  mode 
) [inline, static]

Set ADC channel interrupt mode.

Parameters:
ch_conf Pointer to ADC channel configuration.
mode Interrupt mode to set.

Definition at line 1035 of file adc.h.

References adc_channel_config::intctrl.

Referenced by main().

01037 {
01038         ch_conf->intctrl &= ~ADC_CH_INTMODE_gm;
01039         ch_conf->intctrl |= mode;
01040 }

void adcch_write_configuration ( ADC_t *  adc,
uint8_t  ch,
const struct adc_channel_config ch_conf 
)

Write configuration to ADC channel.

Writes the specified configuration to the ADC channel.

Parameters:
adc Pointer to ADC module.
ch ADC channel number.
ch_conf Pointer to ADC channel configuration.
Note:
The specified ADC's callback function must be set before this function is called if callbacks are enabled and interrupts are enabled in the channel configuration.

Definition at line 439 of file adc.c.

References adc_disable_clock(), adc_enable_clock(), Assert, cpu_irq_restore(), cpu_irq_save(), adc_channel_config::ctrl, adc_channel_config::intctrl, and adc_channel_config::muxctrl.

Referenced by adc_ch3_callback(), and main().

00441 {
00442         ADC_CH_t   *adc_ch;
00443         irqflags_t flags;
00444 
00445         Assert(ch < ADC_NR_OF_CHANNELS);
00446         adc_ch = &adc->CH0 + ch;
00447 
00448         flags = cpu_irq_save();
00449 
00450 #if defined(CONFIG_ADC_CALLBACK_ENABLE) && defined(_ASSERT_ENABLE_)
00451         if ((adc_ch->INTCTRL & ADC_CH_INTLVL_gm) != ADC_CH_INTLVL_OFF_gc) {
00452 #ifdef ADCA
00453                 if ((uintptr_t)adc == (uintptr_t)&ADCA) {
00454                         Assert(adca_callback);
00455                 } else 
00456 #endif /* ADCA */
00457 
00458 #ifdef ADCB
00459                 if ((uintptr_t)adc == (uintptr_t)&ADCB) {
00460                         Assert(adcb_callback);
00461                 } else
00462 #endif /* ADCB */
00463 
00464                 {
00465                         Assert(0);
00466                         return;
00467                 }
00468         }
00469 #endif /* CONFIG_ADC_CALLBACK_ENABLE */
00470 
00471         adc_enable_clock(adc);
00472         adc_ch->CTRL = ch_conf->ctrl;
00473         adc_ch->INTCTRL = ch_conf->intctrl;
00474         adc_ch->MUXCTRL = ch_conf->muxctrl;
00475         adc_disable_clock(adc);
00476 
00477         cpu_irq_restore(flags);
00478 }

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