ADC (Analog to Digital Converter) module is available with a number of MCUs.
Library routines are included to provide you comfortable work with the module in
single-ended mode.
Library Routines
- ADCx_Init
- ADCx_Init_Advanced
- ADCx_Get_Sample
Notes:
- Some of the MCUs do not support ADCx_Init_Advanced routine. Please, refer to the appropriate datasheet.
- ADC routines require you to specify the module you want to use. To select the desired module, simply change the letter
x
in the prototype for a number from1
to3
.
ADCx_Init
Prototype | void ADCx_Init(); |
---|---|
Returns | Nothing |
Description | Initializes MCU’s internal ADC module to work with default settings; internal voltage reference and right justification of the ADC data. |
Requires | MCU must have ADC module. |
Example | ADC1_Init(); // Initializes MCU’s internal ADC module to work with default settings |
ADCx_Init_Advanced
Prototype | void ADCx_Init_Advanced(unsigned short adv_settting); | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns | Nothing. | ||||||||||||||
Description |
This routine configures and enables the internal ADC module to work with user defined
settings. Parameters :
| ||||||||||||||
Requires | MCU must have ADC module. | ||||||||||||||
Example | ADC1_Init_Advanced(_EXTERNAL_REF | _RIGHT_ADJUSTMENT); // initializes ADC module to work with external voltage reference and right justification of the ADC data. |
ADCx_Get_Sample
Prototype |
unsigned ADCx_Get_Sample(unsigned short channel); // for MCUs with ADC port selection unsigned ADCx_Get_Sample(unsigned short adv_setting); // for MCUs with 16-bit ADC module unsigned ADCx_Get_Sample(); | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
8-bit, 10-bit or 12-bit unsigned value read from the specified channel (MCU
dependant). 16-bit unsigned value read from the dedicated channel (for Silicon Laboratories C8051F06x MCU's). | ||||||||||||
Description |
Reads analog value from the specified port and channel. Parameters :
| ||||||||||||
Requires | ADC module must be initialized before using this function. | ||||||||||||
Example | unsigned tmp; ADC1_Init(); // Initialize ADC module tmp = ADC1_Get_Sample(2); // Reads analog value from channel 2 // for MCU's with advanced channel configuration: tmp = ADC1_Get_Sample(_P1 | 3); // Reads analog value from channel 3 of PORT1 |
Library Example
This example code reads analog value from channel AIN2.2 (P1.2) and sends it
to USART Terminal.
unsigned int adc_result;char txt[6];void main() {WDTCN = 0xDE; // Sequence forWDTCN = 0xAD; // disabling the watchdog timerOSCICN = 0x83; // Enable internal oscillator (24.5MHz divided by 1)P0MDOUT |= 0x01; // Configure P0.0 (TX) pin as push-pullUART2_Init(4800); // Initialize UART2Delay_100ms();P1MDIN.B2 = 0; // Configure P1.2 as Analog InputADC2_Init(); // Initialize ADC2 modulewhile (1) {adc_result = ADC2_Get_Sample(2); // Read AIN2.2 (P1.2) analog inputWordToStr(adc_result, txt); // convert result to stringUART2_Write_Text(txt); // send string to UARTUART2_Write(13);UART2_Write(10); // send new line (CR+LF)Delay_ms(500);}}
No comments:
Post a Comment