|
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar code seg[]={0x30,0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38};
sbit date=P3^7;
sbit lcd_rs=P1^0;
sbit lcd_rw=P1^1;
sbit lcd_en=P1^2;
sbit led=P1^4;
uchar RH,RL,TH,TL,check;
void delayms(uint m) //@12.000MHz
{
unsigned char i, j;
while(m--)
{
i = 2;
j = 239;
do
{
while (--j);
} while (--i);
}
}
void delayus(uint n) //@12.000MHz
{
while(n--)
_nop_();
}
void write_com(uint com)//lcd1602寫(xiě)命令函數(shù)
{
lcd_rs=0;
P2=com;
delayms(2);
lcd_en=1;
delayms(2);
lcd_en=0;
}
void write_date(uint date)//lcd1602寫(xiě)數(shù)據(jù)函數(shù)
{
lcd_rs=1;
P2=date;
delayms(2);
lcd_en=1;
delayms(2);
lcd_en=0;
}
void init()//lcd1602初始化,設(shè)置顯示方式
{
lcd_rw=0;
lcd_en=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
}
uchar recive_byte(void)//一個(gè)字節(jié)接受函數(shù)
{
uchar i,dat;
dat=0;
for(i=0;i<8;i++)
{
//while(date);//等待DHT11拉低.輸出的數(shù)據(jù)總是從低電平開(kāi)始的
while(date==0);//等待DHT11拉高
delayus(40);
dat=dat<<1;
if(date==1)
{
dat=dat+1;
}
while(date);//等待高電平結(jié)束
led=0;
}
//led=0;
return dat; //將接受到的數(shù)據(jù)返回
}
void read_DHT11()
{
delayms(5000);//調(diào)試用,將此函數(shù)放入主循環(huán),實(shí)現(xiàn)5S執(zhí)行一次
//發(fā)送起始信號(hào)
date= 0;
delayms(20);
date= 1;
while(date);//這里是等待DHT11將總線拉低
while(date==0);//等待DHT11拉高總線
while(date);//等待數(shù)據(jù)拉低,然后可以接受數(shù)據(jù)
//開(kāi)始接收數(shù)據(jù)
RH= recive_byte();
RL= recive_byte();
TH= recive_byte();
TL= recive_byte();
check= recive_byte();
//接收 DHT11 輸出的結(jié)束信號(hào)
delayus(54);//延遲后,總線會(huì)是高電平。實(shí)際上這個(gè)高電平是由DHT11輸出的
}
void main()
{
init();
//delayms(1000) ;//開(kāi)機(jī)等待1秒
while(1)
{
read_DHT11();
write_com(0x80);
write_date('T');
write_date('E');
write_date('M');
write_date('P');
write_com(0x80+0x05);
write_date(seg[RH/10]);
write_date(seg[RH%10]);
write_date('.');
write_date(seg[RL%10]);
}
}
|
-
|