#include<reg52.h>
#define cmdport P3
#define dataport P2
#define adc_input P1
sbit rs = cmdport^2;
sbit rw = cmdport^1;
sbit e = cmdport^0;
sbit wr= cmdport^4;
sbit rd= cmdport^3;
sbit intr= cmdport^5;
sbit relay1=P0^0;
sbit relay2=P0^1;
sbit relay3=P0^2;
sbit relay4=P0^3;
unsigned char i,a,d1,d2,d3,d4,x1,x2,x3,x4,b,c,d,z[4];
unsigned char text[]= "SW1:OFF SW2:OFF ";
unsigned char text1[]= "SW3:OFF SW4:OFF ";
unsigned char text2[]= "ON ";
unsigned char text3[]= "OFF";
unsigned char i,a,b,c,d,input;
void delay(unsigned int msec)
{
int i,j ;
for(i=0;i<msec;i++)
for(j=0;j<1275;j++);
}
void lcdcmd(unsigned char item)
{
dataport = item;
rs= 0;
rw=0;
e=1;
delay(1);
e=0;
}
void lcddata(unsigned char item)
{
dataport = item;
rs= 1;
rw=0;
e=1;
delay(1);
e=0;
}
void convert(unsigned int value)
{
x1 = value / 10;
d1 = value % 10;
x2 = x1 / 10;
d2 = x1 % 10;
x3 = x2 / 10;
d3 = x2 % 10;
d4 = x3 % 10;
d1=d1|0x30;
d2=d2|0x30;
d3=d3|0x30;
d4=d4|0x30;
lcddata(d4);
lcddata(d3);
lcddata(d2);
lcddata(d1);
}
void main(void)
{
adc_input=0xff;
relay1=0;
relay2=0;
relay3=0;
relay4=0;
delay(50);
lcdcmd(0x38);
delay(100);
lcdcmd(0x0E);
delay(100);
lcdcmd(0x01);
delay(100);
lcdcmd(0x06);
delay(100);
lcdcmd(0x80);
delay(100);
for(i=0;i<=15;i++)
{
lcddata(text[i]);
delay(1);
}
lcdcmd(0xc0);
delay(1);
for(i=0;i<=15;i++)
{
lcddata(text1[i]);
delay(1);
}
lcdcmd(0x80);
while(1)
{
for(i=0;i<3;i++)
{
delay(1);
rd=1;
wr=0;
delay(1);
wr=1;
while(intr==1);
rd=0;
lcdcmd(0x88);
input=adc_input;
delay(1);
intr=1;
z[i]=input;
}
z[3]=(z[0]+z[1]+z[2])/3;
input=z[3];
lcdcmd(0x01);
lcdcmd(0x80);
convert(z[2]);
if(input>20&&input<50)
{
delay(100);
if(a==0)
{
relay1=1;
a=1;
lcdcmd(0x84);
delay(1);
for(i=0;i<=2;i++)
{
lcddata(text2[i]);
delay(1);
}
}
else
{
relay1=0;
a=0;
lcdcmd(0x84);
delay(1);
for(i=0;i<=2;i++)
{
lcddata(text3[i]);
delay(1);
}
}
delay(200);
}
else if(input>70&&input<95)
{
delay(100);
if(b==0)
{
relay2=1;
b=1;
lcdcmd(0x8C);
delay(1);
for(i=0;i<=2;i++)
{
lcddata(text2[i]);
delay(1);
}
}
else
{
relay2=0;
b=0;
lcdcmd(0x8C);
delay(1);
for(i=0;i<=2;i++)
{
lcddata(text3[i]);
delay(1);
}
}
delay(200);
}
else if(input>100&&input<120)
{
delay(100);
if(c==0)
{
relay3=1;
c=1;
lcdcmd(0xc4);
delay(1);
for(i=0;i<=2;i++)
{
lcddata(text2[i]);
delay(1);
}
}
else
{
relay3=0;
c=0;
lcdcmd(0xc4);
delay(1);
for(i=0;i<=2;i++)
{
lcddata(text3[i]);
delay(1);
}
}
delay(200);
}
else if(input>120)
{
delay(100);
if(d==0)
{
relay4=1;
d=1;
lcdcmd(0xCC);
delay(1);
for(i=0;i<=2;i++)
{
lcddata(text2[i]);
delay(1);
}
}
else
{
relay4=0;
d=0;
lcdcmd(0xCC);
delay(1);
for(i=0;i<=2;i++)
{
lcddata(text3[i]);
delay(1);
}
}
delay(200);
}
}
}