#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
#define Data P0
sbit RS=P2^4;
sbit RW=P2^5;
sbit E=P2^6;
sbit DHT=P1^0;(这只有一个DHT,
电路图需要两个)
sbit HADD=P1^3;
sbit HSEC=P1^4;
sbit TADD=P1^5;
sbit TSEC=P1^6;
sbit ADD=P1^7;
sbit SEC=P3^5;
sbit XJT=P3^0;
sbit UPT=P3^1;
sbit XJH=P3^2;
sbit UPH=P3^3;
sbit ADD1=P3^6;
uchar FirstLine[] ="wen:00.0 T:00";
uchar SecondLine[]="shi:00.0 H:00";
unsigned char shiZ,shiX,wenZ,wenX,check,T=25,H=25;
unsigned char tr_shiZ,tr_shiX,tr_wenZ,tr_wenX;
unsigned char flag; unsigned int n=20,m;
void delay_1ms(unsigned int a)
{
unsigned int i;
while(--a!=0)
{
for(i=0;i<600;i++);
}
}
void delay_10us()
{
unsigned char i;
i--;
i--;
i--;
i--;
i--;
i--; }
char read_data()
{
unsigned char i,num,temp;
num=0;
for(i=0;i<8;i++)
{
flag=2;
while((!DHT)&&flag++);
delay_10us();
delay_10us();
delay_10us();
if(DHT==1)
{
temp=1;
flag=2;
while(DHT&&flag++);
}
else
temp=0;
num<<=1;
num|=temp;
}
return(num);
}
void delay(uchar ms)
{
uchar i,j;
for(i=ms;i>0;i--)
for(j=100;j>0;j--);
}
void DelayUs(unsigned char us)
{
unsigned char uscnt;
uscnt=us>>1;
while(--uscnt);
}
void DelayMs(unsigned char ms)
{
while(--ms)
{
DelayUs(250);
DelayUs(250);
DelayUs(250);
DelayUs(250);
}
}
void lcd_write_com(uchar c)
{
DelayMs(5);
E=0;
RS=0;
RW=0;
_nop_();
E=1;
Data=c;
E=0;
}
void lcd_write_dat(uchar c)
{
DelayMs(5);
E=0;
RS=1;
RW=0;
_nop_();
E=1;
Data=c;
E=0;
RS=0;
}
void lcd_init()
{
DelayMs(15);
lcd_write_com(0x38);
lcd_write_com(0x38);
lcd_write_com(0x38);
lcd_write_com(0x06);
lcd_write_com(0x0c);
lcd_write_com(0x01);
}
void ShowChar(uchar pos,uchar c)
{
unsigned char p;
if (pos>=0x10)
p=pos+0xb0;
else
p=pos+0x80;
lcd_write_com(p);
lcd_write_dat(c);
}
void ShowString (uchar line,char *ptr)
{
unsigned char l,*p;
p=ptr;
l=line<<4;
while((*p)!=' ')
{
ShowChar(l++,*(p));
p++;
}
}
void disp(void)
{
ShowString(0,FirstLine);
ShowString(1,SecondLine);
}
void read_init()
{
DHT=0;
delay_1ms(20);
DHT=1;
delay_10us();
delay_10us();
delay_10us();
delay_10us();
DHT=1;
if(!DHT)
{
flag=2;
while((!DHT)&&flag++);
flag=2;
while(DHT&&flag++);
tr_shiZ=read_data();
tr_shiX=read_data();
tr_wenZ=read_data();
tr_wenX=read_data();
check=read_data();
DHT=1;
}
}
void keyscan()
{
if(HADD==0)
{
delay_1ms(10);
if(HADD==0)
{
H++;
if(H==100) H=0;
while(!HADD);
}
}
if(HSEC==0)
{
delay_1ms(10);
if(HSEC==0)
{
H--;
if(H==0) H=100;
while(!HSEC);
}
}
if(TADD==0)
{
delay_1ms(10);
if(TADD==0)
{
T++;
if(T==100) T=0;
while(!TADD);
}
}
if(TSEC==0)
{
delay_1ms(10);
if(TSEC==0)
{
T--;
if(T==0) T=100;
while(!TSEC);
}
}
}
void main()
{
unsigned char temp;
SEC=0;
ADD=0;
lcd_init();
delay(50);
while(1)
{
disp();
read_init();
temp=tr_shiZ+tr_shiX+tr_wenZ+tr_wenX;
if(check==temp)
{
shiZ=tr_shiZ;
shiX=tr_shiX;
wenZ=tr_wenZ;
wenX=tr_wenX;
}
FirstLine[4]='0'+wenZ/10;
FirstLine[5]='0'+wenZ%10;
FirstLine[7]='0'+wenX/10;
FirstLine[12]='0'+T/10;
FirstLine[13]='0'+T%10;
SecondLine[4]='0'+shiZ/10;
SecondLine[5]='0'+shiZ%10;
SecondLine[7]='0'+shiX/10;
SecondLine[12]='0'+H/10;
SecondLine[13]='0'+H%10;
if(wenZ>T)ADD=1,XJT=0,UPT=1;
if(wenZ==T)ADD=0,XJT=1,UPT=1;
if(wenZ<T)ADD=1,XJT=1,UPT=0;
if(shiZ>H)ADD1=1,XJH=0,UPH=1;
if(shiZ==H)ADD1=0,XJH=1,UPH=1;
if(shiZ<H)ADD1=1,XJH=1,UPH=0;
keyscan();
}
}
这个程序只接了一个dht在p1.0,需要两个(在p1.1再接一个)。
一周热门 更多>