当前位置:网站首页>STM32外接DHT11显示温湿度
STM32外接DHT11显示温湿度
2022-07-04 03:46:00 【华为云】
1 DTH11温湿度传感器
1.1 数据读取协议
微控制器MCU与 DHT11之间的通讯和同步,采用单总线数据格式,一次通讯时间4ms左右。
用户MCU发送一次开始信号后,DHT11从低功耗模式转换到高速模式,等待主机开始信号结束后,DHT11发送响应信号,送出40bit的数据,并触发一次信号采集,用户可选择读取部分数据。
从模式下,DHT11接收到开始信号触发一次温湿度采集,如果没有接收到主机发送开始信号,DHT11不会主动进行温湿度采集。采集数据后转换到低速模式。
1.1.1 起始信号
总线空闲状态为高电平,MCU把总线拉低等待DHT11响应,MCU把总线拉低必须大于18毫秒,保证DHT11能检测到起始信号。
DHT11接收到主机的开始信号后,等待MCU的开始信号结束,然后发送80us低电平响应信号。
MCU发送开始信号结束后,延时等待20-40us后,读取DHT11的响应信号,MCU发送开始信号后,可以切换到输入模式,或者输出高电平均可,总线由上拉电阻拉高。
1.1.2 数据数字信号
总线为低电平,说明DHT11发送响应信号,DHT11发送响应信号后,再把总线拉高80us,准备发送数据,每一bit数据都以50us低电平时隙开始,高电平的长短定了数据位是0还是1。
数字0和数字1的表示,如下面图示:
数字0:50us低电平开始后,26-28us的高电平表示0
数字1:50us低电平开始后,70us的高电平表示1
如果读取响应信号为高电平,则DHT11没有响应,需要检查线路是否连接正常。
当最后一bit数据传送完毕后,DHT11拉低总线50us,随后总线由上拉电阻拉高进入空闲状态。
1.1.3 温湿度数据格式
一次完整的数据传输为40bit,高位先出。数据分小数部分和整数部分,数据格式:
8bit湿度整数数据
8bit湿度小数数据
8bit温度整数数据
8bit温度小数数据
8bit校验和
数据传送正确时校验和数据等于“ 8bit 湿度整数数据 +8bit 湿度小数数据+8bit温度整数数据 +8bit 温度小数数据 ”所得结果的末8位。
1.2 硬件接线
DHT11的数据读取只需要一根线,我使用的是PB8,另外,OLED用来显示温湿度的值,使用IIC通信,使用的是PB6和PB7。
2 程序编写
根据DHT11的数据读取协议,编写对应的数据读取函数。
2.1 DHT11复位和检测响应函数
首先是MCU向DHT11发送的起始信号,拉低20ms,再拉高30us。
u8 DHT11RstAndCheck(void){ u8 timer = 0; __set_PRIMASK(1); //关总中断 DHT11_OUT = 0; //输出低电平 delay_ms(20); //拉低至少18ms DHT11_OUT = 1; //输出高电平 delay_us(30); //拉高20~40us while (!DHT11_IN) //等待总线拉低,DHT11会拉低40~80us作为响应信号 { timer++; //总线拉低时计数 delay_us(1); } if (timer>100 || timer<20) //判断响应时间 { __set_PRIMASK(0); //开总中断 return 0; } timer = 0; while (DHT11_IN) //等待DHT11释放总线,持续时间40~80us { timer++; //总线拉高时计数 delay_us(1); } __set_PRIMASK(0); //开总中断 if (timer>100 || timer<20) //检测响应信号之后的高电平 { return 0; } return 1;}
2.2 数据读取
MCU向DHT11发送起始信号后,就可以接收DHT11的数据返回了,一次读取湿度和温度即可。
/*读取一字节数据,返回值-读到的数据*/u8 DHT11ReadByte(void){ u8 i; u8 byt = 0; __set_PRIMASK(1); //关总中断 for (i=0; i<8; i++) { while (DHT11_IN); //等待低电平,数据位前都有50us低电平时隙 while (!DHT11_IN); //等待高电平,开始传输数据位 delay_us(40); byt <<= 1; //因高位在前,所以左移byt,最低位补0 if (DHT11_IN) //将总线电平值读取到byt最低位中 { byt |= 0x01; } } __set_PRIMASK(0); //开总中断 return byt;}/*读取一次数据,返回参数:Humi-湿度,Temp-温度;返回值: 0-成功,1-失败*/u8 DHT11ReadData(float *Humi, float *Temp){ s8 sta = 0; u8 i; u8 buf[5]; if (DHT11RstAndCheck()) //检测响应信号 { for(i=0;i<5;i++) //读取40位数据 { buf[i]=DHT11ReadByte(); //读取1字节数据 } if(buf[0]+buf[1]+buf[2]+buf[3] == buf[4]) //校验成功 { u8 H_inte = buf[0]; //湿度整数部分数据 u8 H_frac = buf[1]; //湿度小数部分数据 u8 T_inte = buf[2]; //温度整数部分数据 u8 T_frac = buf[3]; //温度小数部分数据 char tmp1[8], tmp2[8]; sprintf(tmp1, "%d.%d",H_inte,H_frac); sscanf(tmp1, "%f", Humi); sprintf(tmp2, "%d.%d",T_inte,T_frac); sscanf(tmp2, "%f", Temp); } sta = 0; } else //响应失败返回-1 { *Humi = 88; //响应失败返回-1 *Temp = 88; //响应失败返回-1 sta = 1; } return sta; }
2.3 初始化
使用DHT11之前,进行引脚的初始化和器件的初始化。
/*DHT11初始化函数*/u8 DHT11Init(void){ GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);//使能GPIOC端口时钟 GPIO_SetBits(GPIOB,GPIO_Pin_8); //设置PC13输出高电平,(先设置引脚电平可以避免IO初始化过程中可能产生的毛刺) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //设置DHT11数据引脚->PC13 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; //设置为开漏输出模式 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置输出速率为50MHz GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIOC端口 return DHT11RstAndCheck(); //返回DHT11状态}
3 测试
在移植过U8g2库的hello_world例程上进行修改,在屏幕上显示温湿度。注意摄氏度单位的小圆圈,不知道怎么直接以符号的形式显示出来,我这里是单独画了一个小空心圆。
int main(void){ delay_init(); //延时函数初始化 LED_Init(); //初始化与LED连接的硬件接口 IIC_Init(); u8g2_t u8g2; u8g2Init(&u8g2); u8g2_SetFontMode(&u8g2, 1); u8g2_SetFont(&u8g2, u8g2_font_unifont_t_symbols); DHT11Init(); float Temp = 0; float Humi = 0; char strTemp[32]; char strHumi[32]; while(1) { u8g2_FirstPage(&u8g2); do { //draw(&u8g2); DHT11ReadData(&Humi, &Temp); sprintf(strTemp, "Temp: %.1f C", Temp); sprintf(strHumi, "Humi: %.1f %%", Humi); u8g2_ClearBuffer(&u8g2); u8g2_DrawStr(&u8g2, 0, 30, strTemp); u8g2_DrawCircle(&u8g2, 84, 22, 2, U8G2_DRAW_ALL); u8g2_DrawStr(&u8g2, 0, 60, strHumi); u8g2_SendBuffer(&u8g2); delay_ms(3000); } while (u8g2_NextPage(&u8g2)); }}
测试效果如下:
4 总结
本篇介绍了如何在STM32上外接温湿度DHT11实现温湿度数据的读取,并通过OLED进行数据显示。
边栏推荐
- PID of sunflower classic
- [untitled]
- 渗透实战-guest账户-mimikatz-向日葵-sql提权-离线解密
- How much does it cost to open a futures account in China? Where is it safe to open an account at present?
- MySQL one master multiple slaves + linear replication
- [PaddleSeg 源码阅读] PaddleSeg Transform 的 Normalize操作
- Don't disagree, this is the most powerful "language" of the Internet
- Objective-C description method and type method
- 新型数据中心,助力加快构建以数据为关键要素的数字经济
- What kind of experience is it when the Institute earns 20000 yuan a month!
猜你喜欢
CesiumJS 2022^ 源码解读[0] - 文章目录与源码工程结构
PID of sunflower classic
Rhcsa day 3
Reduce function under functools
2022-07-03: there are 0 and 1 in the array. Be sure to flip an interval. Flip: 0 becomes 1, 1 becomes 0. What is the maximum number of 1 after turning? From little red book. 3.13 written examination.
Webhook triggers Jenkins for sonar detection
pytest多进程/多线程执行测试用例
Tcpclientdemo for TCP protocol interaction
[untitled]
Infiltration practice guest account mimikatz sunflower SQL rights lifting offline decryption
随机推荐
Easy to win insert sort
Eh, the log time of MySQL server is less than 8h?
static hostname; transient hostname; pretty hostname
Baijia forum the founding of the Eastern Han Dynasty
SQL statement strengthening exercise (MySQL 8.0 as an example)
[PaddleSeg 源码阅读] PaddleSeg计算Dice
Is it safe to buy insurance for your children online? Do you want to buy a million dollar medical insurance for your children?
Consul of distributed service registration discovery and unified configuration management
In my spare time, I like to write some technical blogs and read some useless books. If you want to read more of my original articles, you can follow my personal wechat official account up technology c
Detailed explanation of PPTC self recovery fuse
渗透实战-SQLServer提权
super_ Subclass object memory structure_ Inheritance tree traceability
Katalon framework tests web (XXI) to obtain element attribute assertions
毕业总结
system information
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
MySQL data query optimization -- data structure of index
New year's first race, submit bug reward more!
“软硬皆施”,助力建成新型云计算数据中心
What are the virtual machine software? What are their respective functions?