ADC Library
ADC (Analog to Digital Converter) module is available with a number of AVR MCUs. Several library routines are included to provide you comfortable work with the module in single-ended mode.
Library Routines
ADC_Init
| Prototype |
sub procedure ADC_Init() ' for XMEGA family of MCUssub procedure ADCx_Init() |
|---|---|
| Returns |
Nothing. |
| Description |
Initializes internal ADC module to work with XTAL frequency prescaled by 128. Clock determines the time period necessary for performing A/D conversion. For XMEGA family of MCUs change the X in the routine prototype with A or B. |
| Requires |
|
| Example |
ADC_Init() ' Initialize ADC module with default settings ADCA_Init() ' Initialize ADC module with default settings |
ADCx_Init_Advanced
| Prototype |
sub procedure ADCx_Init_Advanced(dim AdcMode, Reference as byte) |
||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Returns |
Nothing. |
||||||||||||||||||
| Description |
Parameters
|
||||||||||||||||||
| Requires |
|
||||||||||||||||||
| Example |
ADCA_Init_Advanced(_ADC_12bit, _ADC_INTERNAL_REF_1V) ' Initialize ADC module with 12bit resolution and internal voltage reference of 1V |
||||||||||||||||||
ADC_Get_Sample
| Prototype |
sub function ADC_Get_Sample(dim channel as byte) as word ' for XMEGA family of MCUssub function ADCx_Get_Sample(dim channel as byte) as word |
|---|---|
| Returns |
10-bit or 12-bit unsigned value (MCU dependent) from the specified |
| Description |
Routine acquires analog value from the specified channel. Parameter For XMEGA family of MCUs change the X in the routine prototype with A or B. |
| Requires |
|
| Example |
dim adc_value as word ... ADC_Init() ' Initialize ADC module with default settings adc_value = ADC_Get_Sample(2) ' Acquire analog value from channel 2 |
ADC_Read
| Prototype |
sub function ADC_Read(dim channel as byte) as word ' for XMEGA family of MCUssub function ADC_Read(dim channel as byte) as word |
|---|---|
| Returns |
10-bit or 12-bit unsigned value (MCU dependent) from the specified |
| Description |
Routine initializes internal ADC module and acquires analog value from the specified Parameter For XMEGA family of MCUs change the X in the routine prototype with A or B. |
| Requires |
Nothing. |
| Example |
dim adc_value as word ... adc_value = ADC_Read(2) ' initialize ADC module and acquire analog value from ADC module channel 2 |
Library Example
This example code reads analog value from channel 2 and displays it on PORTB and PORTC.
program ADC_on_LEDs
dim adc_rd as word
main:
DDRB = 0xFF ' set PORTB as output
DDRC = 0xFF ' set PORTC as output
while TRUE
adc_rd = ADC_Read(2) ' get ADC value from 2nd channel
PORTB = adc_rd ' display adc_rd[7..0]
PORTC = Hi(adc_rd) ' display adc_rd[9..8]
wend
end.
HW Connection

ADC HW connection
What do you think about this topic ? Send us feedback!




