检测DHT11温/湿度模块是否损坏

材料:开发板模块(本人用的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);	//保持程序一直运行,避免程序运行太快肉眼观察不到灯的状态
}

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值