材料:开发板模块(本人用的STC89C52RC)DHT11温湿度模块,杜邦线若干,LED灯模块(模块上有可直接用);
ps:主要温度模块的时序图;
#include <reg52.h>
#include <intrins.h>
sbit led1 = P3^7;
sbit dht = P3^5;
void Delay60us(){
unsigned char i;
i = 25;
while (--i);
}//延时60 us
void Delay30ms(){
unsigned char i, j;
i = 54;
j = 199;
do{
while (--j);
} while (--i);
}//延时30 ms
void Delay1000ms(){
unsigned char i, j, k;
_nop_();
i = 8;
j = 1;
k = 243;
do{
do{
while (--k);
}while (--j);
}while (--i);
}//延时1 ms
void check_dht(){
dht = 1;
dht = 0;
Delay30ms(); //延时30 ms以启动模块,至少18 ms
dht = 1; //软件拉高电平
Delay60us(); //若模块存在模块会将电平拉低,在20—40 us内
if(dht == 0){
led1 = 0; //点亮D5
}//60 us后检测dht是否被模块拉低低电平,若成功拉低表示模块存在
}//根据模块时序图检测模块是否存在
void main(){
led1 = 1;
Delay1000ms();
Delay1000ms(); //上电后延迟2 s让模块
check_dht();
while(1); //保持程序一直运行,避免程序运行太快肉眼观察不到灯的状态
}