FRIENDz

FAILURE IS THE SIGN OF SUCCESS!!

Connect ADC with 8051 Code

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 from 1 to 3
    Number of ADC modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

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 :
  • adv_setting: ADC module configuration flags. Predefined library constants (see the table below) can be ORed to form appropriate configuration value.
    Description Predefined library const
    Voltage reference constants:
    Internal voltage reference _INTERNAL_REF
    External voltage reference _EXTERNAL_REF
    Justification constants:
    Left justification _LEFT_ADJUSTMENT
    Right justification _RIGHT_ADJUSTMENT
Note:
  • Advanced configuration of the ADC module is supported only by Silicon Laboratories MCU's. Please consult appropriate datasheet.
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 :
  • channel: represents the channel from which the analog value is to be acquired. Refer to the appropriate datasheet for channel-to-pin mapping.
  • adv_setting: represents the advanced setting for the MCU's that have ADC module routed to different ports. Refer to the appropriate datasheet.
Predefined library constants (see the table below) can be ORed with the desired channel number to form the appropriate configuration value :
    Description Predefined library const
    ADC port constants:
    Port 0 _P0
    Port 1 _P1
    Port 2 _P2
    Port 3 _P3
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 for
WDTCN = 0xAD; // disabling the watchdog timer
OSCICN = 0x83; // Enable internal oscillator (24.5MHz divided by 1)
P0MDOUT |= 0x01; // Configure P0.0 (TX) pin as push-pull
UART2_Init(4800); // Initialize UART2
Delay_100ms();
P1MDIN.B2 = 0; // Configure P1.2 as Analog Input
ADC2_Init(); // Initialize ADC2 module
while (1) {
adc_result = ADC2_Get_Sample(2); // Read AIN2.2 (P1.2) analog input
WordToStr(adc_result, txt); // convert result to string
UART2_Write_Text(txt); // send string to UART
UART2_Write(13);UART2_Write(10); // send new line (CR+LF)
Delay_ms(500);
}
}

HW Connection


No comments:

Post a Comment