#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit ad_cs=P1^4;
sbit ad_clk=P1^2;
sbit ad_dat=P1^3;
sbit lcd_rs=P2^3;
sbit lcd_e=P2^4;
sbit key1=P2^6;
sbit key2=P2^7;
sbit key3=P3^7;
sbit relay=P3^4;
sbit FENG =P2^5;
bit flag=1;
bit flag_motor=1;
bit flag_jia=0;
bit flag_jian=0;
uchar Hum_H=80;
uchar Hum_L=20;
uchar num=0;
void delay_ms(uint a)
{
uint i;
while(a--)
for(i=0;i<125;i++);
}
void delay_50us(uint a)
{
uint i;
while(a--)
for(i=0;i<19;i++);
}
uchar read_ad(uchar channel)
{
uchar i;
uchar dat1,dat2;
ad_clk=0;
ad_cs=0;
ad_dat=1;
ad_clk=1;
ad_clk=0;
ad_dat=1;
ad_clk=1;
ad_clk=0;
ad_dat=channel;
ad_clk=1;
ad_clk=0;
ad_dat=1;
for(i=0;i<8;++i)
{
ad_clk=1;
ad_clk=0;
dat1=dat1<<1;
if(ad_dat)
dat1|=0x01;
}
for(i=0;i<8;i++)
{
dat2=dat2>>1;
if(ad_dat)
dat2|=0x80;
ad_clk=1;
ad_clk=0;
}
ad_cs=1;
return (dat1==dat2)?dat1:0;
}
void write_com(uchar com)
{
lcd_e=0;
lcd_rs=0;
P0=com;
delay_50us(10);
lcd_e=1;
delay_50us(10);
lcd_e=0;
}
void write_data(uchar dat)
{
lcd_e=0;
lcd_rs=1;
P0=dat;
delay_50us(10);
lcd_e=1;
delay_50us(10);
lcd_e=0;
}
void init_lcd1602()
{
delay_50us(300);
write_com(0x38);
delay_50us(100);
write_com(0x38);
delay_50us(100);
write_com(0x38);
write_com(0x38);
write_com(0x08);
write_com(0x01);
write_com(0x06);
write_com(0x0c);
}
void display_shu(uchar add,uchar dat)
{
uchar l,m;
// k=dat/100;
l=dat%100/10;
m=dat%10;
write_com(0x80+add);
// write_data(k+0x30);
write_data(l+0x30);
write_data(m+0x30);
}
void display_string(uchar add,uchar *dat)
{
uchar i;
write_com(0x80+add);
while(dat!=0&&(*dat!='\0')&&i<16)
{
write_data(*dat);
dat++;
i++;
if(i==15) i=0;
}
}
void main()
{
init_lcd1602();
display_string(0,"Hum: %");
display_string(11,"H: %");
display_string(0x40,"Mode:");
display_string(0x40+11,"L: %");
while(1)
{
uchar i;
i=100-read_ad(0)*0.39;
display_shu(4,i);
display_shu(13,Hum_H);
display_shu(0x40+13,Hum_L);
if(key1==0)
{
delay_50us(100);
if(key1==0)
{
while(!key1);
num++;
if(num>1)num=0;
}
switch (num)
{
case 0: //write_com(0x80+15);
//write_com(0x0f);
//delay_50us(1000);
//write_com(0x0c);
flag_jia=1;
flag_jian=0;
break;
case 1: //write_com(0x80+0x40+15);
//write_com(0x0f);
//delay_50us(1000);
//write_com(0x0c);
flag_jian=1;
flag_jia=0;
break;
}
if(flag==0&&num==1)
{
flag=~flag;
num=0;
flag_jia=0;
flag_jian=0;
}
}
if(flag==0)
{
break;
}
else
{
display_string(0x40+5,"AT");
if(i<Hum_L)
{
relay=0;
FENG=0;
}
if(i>Hum_L&&i<Hum_H)
{
relay=0;
FENG=1;
}
if(i>Hum_H)
{
relay=1;
FENG=1;
}
}
if(key2==0)
{
delay_50us(100);
if(key2==0)
{
while(!key2);
flag_motor=~flag_motor;
if(flag&&flag_jia)
{
//write_com(0x80+9);
//write_com(0x0f);
//delay_50us(1000);
//write_com(0x0c);
//delay_50us(1000);
Hum_H++;
if(Hum_H>=99) Hum_H=99;
}
if(flag&&flag_jian)
{
//write_com(0x80+0x40+9);
//write_com(0x0f);
//delay_50us(1000);
//write_com(0x0c);
//delay_50us(1000);
Hum_L++;
if(Hum_L>=Hum_H) Hum_L=Hum_H-1;
}
}
}
if(key3==0)
{
delay_50us(20);
if(key3==0)
{
while(!key3);
if(flag&&flag_jian)
{
//write_com(0x80+0x40+9);
//write_com(0x0f);
//delay_50us(1000);
//write_com(0x0c);
//delay_50us(1000);
Hum_L--;
if(Hum_L<=1) Hum_L=1;
}
if(flag&&flag_jia)
{
//write_com(0x80+9);
//write_com(0x0f);
//delay_50us(1000);
//write_com(0x0c);
//delay_50us(1000);
Hum_H--;
if(Hum_H<=Hum_L) Hum_H=Hum_L+1;
}
}
}
}
}
|