pic单片机程序如下
#include"lian_pic.h"
#include"DHT11.h"
#include"DELAY.h"
#include"LCD.h"
#define _XTAL_FREQ 4000000
uchar data_byte;
uchar RH,RL,TH,TL,R,T;
void start(void)
{
DH_out;
DH_io=1;
__delay_us(8);
DH_io=0;
__delay_ms(25);//主机拉低总线发送开始信号 大于18ms
DH_io=1;
__delay_us(8);
__delay_us(8);//主机发送开始信号结束,延时20-40us等待DHT11响应
__delay_us(8);
}
unsigned char receive_byte(void)
{
uchar i,temp;
DH_in;
for(i=0;i<8;i++)
{
while(!DH_io);
__delay_us(8);
__delay_us(8);
__delay_us(8);
temp=0;
if(DH_io==1)
temp=1;
while(DH_io);
data_byte<<=1;
data_byte|=temp;
}
return data_byte;
}
void receive()
{
uchar T_H,T_L,R_H,R_L,check,num_check,i;
start();
DH_in;
if(!DH_io)
{
while(!DH_io);
while(DH_io);
R_H=receive_byte();
R_L=receive_byte();
T_H=receive_byte();
T_L=receive_byte();
check=receive_byte();
DH_out;
DH_io=0;
for(i=0;i<8;i++)
__delay_us(8);
DH_io=1;
num_check=R_H+R_L+T_H+T_L;
if(num_check==check)
{
RH=R_H;
RL=R_L;
TH=T_H;
TL=T_L;
check=num_check;
}
}
}
void show_DHT11(void)
{
write_com(0x80+2);
write_data(RH/10+0x30);
write_data(RH%10+0X30);
write_com(0x80+0x40+2);
write_data(TH/10+0x30);
write_data(TH%10+0x30);
}
|