Aim:
To write a C program Convert a particular analog input to its corresponding digital
data using ADC.

Algorithm :
Step1. Declare control word register at address 0xe003
Step 2. Initialize Port A, Port B & Port C with memory locations 0xe000, 0xe001 and 0xe003
respectively
Step 3. Set the SCON and TMOD for setting baud rate and timer mode
Step 4. Configure control word to 0x92 for setting port a and port b as input and port c as
output ports
Step 5. Convert the input value to digital value
Step 6. Conversion value was viewed by using UART
Step 7. Once conversion is completed Check whether the key entered is '0x2c'
Step 8. If the key is 0x2c display the next channel result in UART
Step 9. Stop the execution
Program
#include <REG51xD2.H>
#include <stdio.h>
#include <intrins.h>
unsigned char xdata porta _at_ 0xe000;
unsigned char xdata portb _at_ 0xe001;
unsigned char xdata portc _at_ 0xe002;
unsigned char xdata ctrl _at_ 0xe003;
unsigned char xdata result[3] _at_ 0x9000;         //to store the channel no. and the digital value
void delay();
void again();
void convert()
{
unsigned char temp = 0x00;
temp = result[0];
temp <<= 2;
temp |= 0x23;
portc = temp;
delay();
again();
}
void delay()
{
unsigned char ch = 0;
for(ch = 0; ch <= 0x10; ch ++) ;
}
void again()
{
ctrl = 0x02;
_nop_();
ctrl = 0x03;
_nop_();
_nop_();
_nop_();
_nop_();
ctrl = 0x00;                        //send PC0 low to start the conversion
result[1] = porta;            //Read the LSB of the digital value from porta
result[2] = portb;            //Read the MSB of the digital value from portb
printf("\nChannel : ")    //Print the channel value
SBUF = (result[0]+0x30);
_nop_();
printf("\nDigital Value = 0x0%x%x",result[2],result[1]);    //Print the digital value
putchar(0x8)
 putchar(0x20);
}
void main()
{
unsigned char input = 0x00;
SCON = 0x52;                                                                        //Initialize UART of 8051
TMOD |= 0x20;
TH1 = 0xe6;
TR1 = 1;
result[0] = 0x00;
ctrl = 0x92;                                                                            //Configure porta and portb as input and portc
as output ports
portc = 0x03;                                                                      //Latch is edge triggeres
convert();
while(1)
{
input = getchar();                                                            //Get the input from the PC keyboard
if(input == 0x2c)                                                             //Check whether the key entered is ',' or '-' or '-'
{
result[0] += 1;
result[0] &= 0x07;
convert();
}
}