#include<REG51.H>
sfr lcd_port = 0xA0; //LCD connected to p2
sfr ADC_port = 0x90; //ADC connected to p1
sbit rs=P3^0; //rs connected to p3.0
sbit rw=P3^6; //rw connected to p3.6
sbit en=P3^2; //en connected to p3.2
sbit rd=P3^3; //rd connected to p3.2
sbit wr=P3^4; //wr connected to p3.4
sbit intr=P3^5; //intr connected to p3.5
sbit busy=P2^7; //busy flag= p2.7
unsigned int ADC_value, buf;
void delay(unsigned int count) //delay subroutine
{
unsigned int i,j;
for(i=0;i<=count;i++)
for(j=0;j<=1275;j++);
}
void ready() //subroutine to check if lcd is busy
{
busy=1; //make busy pin input
rs=0; //rs=low
rw=1; //rw=high
while(busy==1) //check if busy flag
{
en=0;
delay(2);
en=1;
}
}
void lcd_cmd(unsigned int value) //sub to write commands
{
ready();
lcd_port=value;
rs=0;
rw=0;
en=1;
delay(2);
en=0;
}
void lcd_data(unsigned int value) //sub to write data
{
ready();
lcd_port=value;
rs=1;
rw=0;
en=1;
delay(2);
en=0;
}
void lcd_init() //lcd initializtion
{
rs=0; //rs=output
rw=0; //rw=output
en=0; //en=output
lcd_port=0x00; //lcd_port=port2=output
lcd_cmd(0x38);
lcd_cmd(0x0E);
lcd_cmd(0x01);
lcd_cmd(0x06);
lcd_cmd(0x80);
}
void ADC_init() //ADC initialization
{
intr=1;
rd=0;
wr=0;
ADC_port=0xFF; //ADC_port=port1=input
wr=0; //start of conversion
delay(2);
wr=1;
while(intr==0) //check for intr flag
{
rd=0;
ADC_value=ADC_port; //read digital o/p;A2D value stored in variable ADC_value
rd=1;
}
}
void hex2ascii() //conversion to ASCII for display on LCD
{
unsigned int one, ten, hundred;
buf=ADC_value;
one=buf%10;
ten=buf/10;
ten=ten%10;
hundred=buf/100;
one=one|0x30;
ten=ten|0x30;
hundred=hundred|0x30;
lcd_data('m'); //char 'm' sent for testing of LCD
lcd_data(one); //one's bit sent to LCD
lcd_data(ten); //ten's bit sent to LCD
lcd_data(hundred); //hundred's bit sent to LCD
}
void main()
{
while(1)
{
lcd_init(); //LCD initialization
ADC_init(); //ADC initialization
hex2ascii(); //HEX 2 ASCII conversion of digital output
}
}