#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define DATA P0
sbit rs=P1^0;
sbit rw=P1^1;
sbit en=P2^5;
uchar str1[]={" "},str2[]={" "};
uchar shduh_s,shdul_s,wenduh_s,wendul_s,jiaoyan_s;
uchar shduh,shdul,wenduh,wendul,jiaoyan;
uchar RH_data,RL_data,TH_data,TL_data,CK_data;
uchar FLAG,k,count,temp,comdata;
sbit wshbzh=P1^7;
void delay(xms)
{
uint i,j;
for(i=xms;i>0;i--)
for(j=50;j>0;j--);
}
void Delay_10us(void)
{
uchar i;
i--;
i--;
i--;
i--;
i--;
i--;
}
void minling(uchar comd)
{
rs=0;
rw=0;
DATA=comd;
delay(5);
en=1;
delay(5);
en=0;
}
void writedata(uchar write)
{
rs=1;
rw=0;
DATA=write;
delay(5);
en=1;
delay(5);
en=0;
}
void writechar(uchar x,y,Data)
{
if (x==0)
{
minling(0x80+y-1);
}
else
{
minling(0xC0+y-1);
}
writedata(Data);
}
void lcdinit()
{
minling(0x02);
minling(0x06);
minling(0x0c);
minling(0x38);
minling(0x01);
}
void writestring(uchar x,uchar y,uchar *s)
{
if (x == 1)
{
minling(0x80 + y-1);
}
else
{
minling(0xC0 + y-1);
}
while (*s)
{
writedata( *s);
s ++;
}
}
void COM()
{
uchar i;
for(i=0;i<8;i++)
{
FLAG=2;
while((!wshbzh)&&FLAG++);
Delay_10us();
Delay_10us();
Delay_10us();
temp=0;
if(wshbzh) temp=1;
FLAG=2;
while((wshbzh)&&FLAG++);
if(FLAG==1)
break;
comdata<<=1;
comdata|=temp; //0
}
}
void RH()
{
wshbzh=0;
delay(200);
wshbzh=1;
Delay_10us();
Delay_10us();
Delay_10us();
Delay_10us();
wshbzh=1;
if(!wshbzh) //T !
{
FLAG=2;
while((!wshbzh)&&FLAG++);
FLAG=2;
while((wshbzh)&&FLAG++);
COM();
shduh_s=comdata;
COM();
shdul_s=comdata;
COM();
wenduh_s=comdata;
COM();
wendul_s=comdata;
COM();
jiaoyan_s=comdata;
wshbzh=1; //????
temp=(shduh_s+shdul_s+wenduh_s+wendul_s);
if(temp==jiaoyan_s)
{
shduh=shduh_s;
shdul=shdul_s;
wenduh=wenduh_s;
wendul=wendul_s;
jiaoyan=jiaoyan_s;
}
}
}
int main()
{
rs=0;
rw=0;
en=0;
lcdinit();
writestring(1,1,"shidu:");
writestring(2,1,"wendu:");
writestring(1,10,"%");
writestring(2,10,"C");
while(1)
{
RH();
RH_data=shduh;
RL_data=shdul;
TH_data=wenduh;
TL_data=wendul;
CK_data=jiaoyan;
str1[0]=(char)(0x30+RH_data/10);
str1[1]=(char)(0x30+RH_data%10);
str2[0]=(char)(0x30+TH_data/10);
str2[1]=(char)(0x30+TH_data%10);
writechar(1,8,str1[0]);
writechar(1,9,str1[1]);
writechar(2,8,str2[0]);
writechar(2,9,str2[1]);
delay(2000);
}
}
|