Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
自制DHT11驱动跑不起来,求解
发布于 2019-08-28 16:03:14 浏览:1239
订阅该版
自制DHT11驱动跑不起来,得不到数据,数据都是0,求解main.c```#include
#include
#include
#include
#include "key.h" #include "dht11.h" int main(void) { rt_uint8_t t=0; rt_uint8_t temperature=0; rt_uint8_t humidity=0; LED_Init(); BEEP_Init(); KEY_Init(); led_on(0); DHT11_Init(); while(1) { if(t%10==0) //每100ms读取一次 { DHT11_Read_Data(&temperature,&humidity); //读取温湿度值 rt_kprintf("temperature: %d ℃
",temperature); //显示温度 rt_kprintf("humidity : %d %%
",humidity); //显示湿度 } rt_thread_mdelay(10); t++; if(t==20) { t=0; led_blink(1); } } } ```dht11.c ```#include "dht11.h" #include
#define DHT11_PIN GET_PIN(G, 11) //数据端口 PA0 //复位DHT11 void DHT11_Rst(void) { rt_pin_mode(DHT11_PIN, PIN_MODE_OUTPUT); rt_pin_write(DHT11_PIN, PIN_LOW); //拉低DQ rt_thread_mdelay(20); //拉低至少18ms rt_pin_write(DHT11_PIN, PIN_HIGH); //DQ=1 rt_thread_mdelay(30); //主机拉高20~40us } //等待DHT11的回应 //返回1:未检测到DHT11的存在 //返回0:存在 rt_uint8_t DHT11_Check(void) { rt_uint8_t retry=0; rt_pin_mode(DHT11_PIN, PIN_MODE_INPUT); //SET INPUT while (rt_pin_read(DHT11_PIN)&&retry<100)//DHT11会拉低40~80us { retry++; rt_thread_mdelay(1); }; if(retry>=100)return 1; else retry=0; while (!rt_pin_read(DHT11_PIN)&&retry<100)//DHT11拉低后会再次拉高40~80us { retry++; rt_thread_mdelay(1); }; if(retry>=100)return 1; return 0; } //从DHT11读取一个位 //返回值:1/0 rt_uint8_t DHT11_Read_Bit(void) { rt_uint8_t retry=0; while(rt_pin_read(DHT11_PIN)&&retry<100)//等待变为低电平 { retry++; rt_thread_mdelay(1); } retry=0; while(!rt_pin_read(DHT11_PIN)&&retry<100)//等待变高电平 { retry++; rt_thread_mdelay(1); } rt_thread_mdelay(40);//等待40us if(rt_pin_read(DHT11_PIN))return 1; else return 0; } //从DHT11读取一个字节 //返回值:读到的数据 rt_uint8_t DHT11_Read_Byte(void) { rt_uint8_t i,dat; dat=0; for (i=0;i<8;i++) { dat<<=1; dat|=DHT11_Read_Bit(); } return dat; } //从DHT11读取一次数据 //temp:温度值(范围:0~50°) //humi:湿度值(范围:20%~90%) //返回值:0,正常;1,读取失败 rt_uint8_t DHT11_Read_Data(rt_uint8_t *temp,rt_uint8_t *humi) { rt_uint8_t buf[5]="0"; rt_uint8_t i; DHT11_Rst(); if(DHT11_Check()==0) { for(i=0;i<5;i++)//读取40位数据 { buf*=DHT11_Read_Byte(); } if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4]) { *humi=buf[0]; *temp=buf[2]; } }else return 1; return 0; } //初始化DHT11的IO口 DQ 同时检测DHT11的存在 //返回1:不存在 //返回0:存在 rt_uint8_t DHT11_Init(void) { DHT11_Rst(); //复位DHT11 return DHT11_Check();//等待DHT11的回应 } ```dht11.h ```#ifndef __DHT11_H #define __DHT11_H #include
#define DHT11_PIN GET_PIN(G, 11) //数据端口 PA0 rt_uint8_t DHT11_Init(void);//初始化DHT11 rt_uint8_t DHT11_Read_Data(rt_uint8_t *temp,rt_uint8_t *humi);//读取温湿度 rt_uint8_t DHT11_Read_Byte(void);//读出一个字节 rt_uint8_t DHT11_Read_Bit(void);//读出一个位 rt_uint8_t DHT11_Check(void);//检测是否存在DHT11 void DHT11_Rst(void);//复位DHT11 #endif```
查看更多
1
个回答
默认排序
按发布时间排序
撰写答案
登录
注册新账号
关注者
0
被浏览
1.2k
关于作者
时光片刻
这家伙很懒,什么也没写!
提问
5
回答
3
被采纳
0
关注TA
发私信
相关问题
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
最新文章
1
RT-thred的stm32h723对应bsp包CubeMX添加其他外设报错
2
RT-Thread中的time溢出问题,时间戳溢出,解决方法
3
ART-PI使用env驱动ETH网卡,pc和板子可以ping通
4
SystemView线程名字不显示
5
只用网页也能跑RT-Smart 无门槛腾讯Cloud studio + smart-build快速构建
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
rt-smart
FAL
I2C_IIC
UART
ESP8266
cubemx
WIZnet_W5500
ota在线升级
PWM
BSP
flash
freemodbus
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
keil_MDK
ulog
SFUD
msh
C++_cpp
MicroPython
本月问答贡献
RTT_逍遥
9
个答案
2
次被采纳
三世执戟
8
个答案
1
次被采纳
KunYi
8
个答案
1
次被采纳
winfeng
2
个答案
1
次被采纳
YZRD
2
个答案
1
次被采纳
本月文章贡献
catcatbing
2
篇文章
5
次点赞
swet123
1
篇文章
4
次点赞
Days
1
篇文章
4
次点赞
YZRD
1
篇文章
2
次点赞
阳光的掌控者
1
篇文章
1
次点赞
回到
顶部
发布
问题
分享
好友
手机
浏览
扫码手机浏览
投诉
建议
回到
底部