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
- ADCx_Init_Advanced
- ADC_Get_Sample
- ADC_Read
ADC_Init
ADCx_Init_Advanced
Prototype | void ADCx_Init_Advanced(unsigned AdcMode, unsigned Reference); | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns | Nothing. | ||||||||||||||||||
Description |
AdcMode and Reference determine the work
mode for ADC module and can have the following values:
| ||||||||||||||||||
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 |
unsigned ADC_Get_Sample(unsigned short channel); // for XMEGA family of MCUs unsigned ADCx_Get_Sample(unsigned short channel); |
---|---|
Returns |
10-bit or 12-bit unsigned value (MCU dependent) from the specified
channel . |
Description |
Routine acquires analog value from the specified channel. Parameter channel represents the channel from which the analog
value is to be acquired. Refer to the appropriate datasheet for channel-to-pin
mapping.For XMEGA family of MCUs change the X in the routine prototype with A or B. |
Requires |
|
Example | unsigned adc_value;
...
ADC_Init(); // Initialize ADC module with default settings
adc_value = ADC_Get_Sample(2); // Acquire analog value from channel 2
|
ADC_Read
Prototype |
unsigned ADC_Read(char channel); // for XMEGA family of MCUs unsigned ADCx_Read(char channel); |
---|---|
Returns |
10-bit or 12-bit unsigned value (MCU dependent) from the specified
channel . |
Description |
Routine initializes internal ADC module and acquires analog value
from the specified channel . Clock determines the time period
necessary for performing A/D conversion.Parameter channel represents the channel from which the analog
value is to be acquired. Refer to the appropriate datasheet for channel-to-pin
mapping.For XMEGA family of MCUs change the X in the routine prototype with A or B. |
Requires | Nothing. |
Example | unsigned adc_value;
...
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.
Code For ADC
#include <built_in.h>
unsigned int adc_rd;
void main() {
DDRB = 0xFF; // Set PORTB as output
DDRC = 0xFF; // Set PORTC as output
while (1) {
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]
}
}
No comments:
Post a Comment