/********************* ADC-0808****************************/
#include<REG51.H> //including the header file registers of 8051
sfr adcdata=0x90; //assigning adc data to port 1
sfr ldata=0x80; ` //assigning lcd data to port 0
sbit rs=P2^7; // assining control pins to port2.5,6,7
sbit rw=P2^6;
sbit en=P2^5;
// channel selection bits of adc
sbit ch_a=P3^2;
sbit ch_b=P3^3;
sbit ch_c=P3^4;
// control pins of adc
sbit ALE=P3^5; //address latch enble pin
sbit EOC=P3^6; // end of converssion pin
sbit SC=P3^7; //select chip pin
sbit motor=P2^0; //assigning motor
sbit LED0=P2^1; //assigning led’s
sbit LED1=P2^2;
sbit LED2=P2^3;
sbit LED3=P2^4;
void cmd(unsigned char y); //lcd command function
void lcddata(unsigned char y); //lcd data function
void disp_str(unsigned char *P); //to display string in lcd
void delay(unsigned int k); //delay function
//void lcdinit(void);
unsigned char convertanddispaly(unsigned char value);
void disp_loc(unsigned char , unsigned char *);
unsigned char value,z,c,TEMP,T,LDR,L;
unsigned char adc_data(unsigned char);
/**************************convert program*************************/
// the function is used to convert the integer value to ascii value
unsigned char convertanddispaly(unsigned char value)
{
unsigned char x,d1,d2,d3;
x=value/10;
d1=value%10;
d2=x%10;
d3=x/10;
lcddata(d3+0x30); //move data d3 to lcd
lcddata(d2+0x30); //move data d2 to lcd
lcddata(d1+0x30); //move data d1 to lcd
z=d1*100+d2*10+d3;
return(z);
}
/**********************main program start************************/
void main()
{
adcdata=0xff;
EOC=1; // initial condition values of ADC control pin
ALE=0;
SC=0;
lcdinit(); //initialize the lcd
delay(10); //delay of 10
cmd(0x01); //lcd command to clear the screen
disp_loc(0xc0," ADC Project ");
delay(100);
cmd(0x01);
disp_loc(0x80,"TEMP:");
disp_loc(0x89,"LDR:");
while(1)
{
TEMP=adc_data(1);
cmd(0x85); //lcd command to write in the location 85
T=convertanddispaly(TEMP); //convert and display temperature
delay(1);//delay of 1
LDR=adc_data(0); // the adc_data is moved to LDR
cmd(0x8D); //lcd command to write in th location 8D
L=convertanddispaly(LDR); //convert and display light
delay(1);
}
}
/***********************switch cases******************************/
//The function to select the adc channel.
unsigned char adc_data(unsigned char c)
{
switch(c)
{
case 0: ch_a=0; ch_b=0; ch_c=0; // channel0 selection
break;
case 1: ch_a=1; ch_b=0; ch_c=0; // channel1 selection
break;
case 2: ch_a=0; ch_b=1; ch_c=0; // channel2 selection
break;
case 3: ch_a=1; ch_b=1; ch_c=0; // channel3 selection
break;
case 4: ch_a=0; ch_b=0; ch_c=1; // channel4 selection
break;
case 5: ch_a=1; ch_b=0; ch_c=1; // channel5 selection
break;
case 6: ch_a=0; ch_b=1; ch_c=1; // channel6 selection
break;
default: ch_a=1; ch_b=1; ch_c=1; // channel7 selection
break;
}
delay(1);
ALE=1;
delay(1);
SC=1;
delay(1);
ALE=0;
SC=0;
while(EOC==1); //monitoring end of conversion bit until EOC equal to 0
delay(1);
value=adcdata;
EOC=1;
return(adcdata);
}
void disp_loc(unsigned char r, unsigned char *s)
{
ldata=r;
rs=0;
rw=0;
en=1;
delay(2);
en=0;
while(*s!='\0')
lcddata(*s++);
}
lcdinit() //lcd initialize
{
cmd(0x38); // 2 lines and 5x7 matrix
cmd(0x0e); //display on, cursor blinking
cmd(0x01); //clear display screen
cmd(0x06); //increment cursor
cmd(0x80); //force cursor to beginning of 1st line
disp_str("welcome");
}
void cmd(unsigned char y)
{
ldata=y;
rs=0;
rw=0;
en=1;
delay(10);
en=0;
}
void lcddata(unsigned char y)
{
ldata=y;
rs=1;
rw=0;
en=1;
delay(10);
en=0;
}
void disp_str(unsigned char *P)
{
while(*P!='\0')
lcddata(*P++);
}
void delay(unsigned int k)
{
unsigned int i,j;
for(i=0;i<k;i++)
for(j=0;j<1200;j++);
}