当前位置:网站首页>[STM32 learning] (15) STM32 realizes DHT11 temperature and humidity acquisition and display
[STM32 learning] (15) STM32 realizes DHT11 temperature and humidity acquisition and display
2022-07-24 09:51:00 【Use small materials】

Have a look first DHT11 The appearance of temperature and humidity .
DHT11 It is a wet temperature integrated digital sensor produced by Guangzhou Aosong Co., Ltd . The sensor consists of a resistive humidity measuring element and a NTC Temperature measuring element , And with a high performance 8 Bit single chip microcomputer connected . Through the simple circuit connection of MCU and other microprocessors, we can collect local humidity and temperature in real time . DHT11 Simple single bus can be used to communicate with MCU , Just one I/O mouth . Sensor internal humidity and temperature data 40Bit The data is transmitted to the single chip microcomputer at one time , The data is verified by checksum , Effectively ensure the accuracy of data transmission . DHT11 Low power consumption , 5V At supply voltage , Average maximum operating current 0.5mA.
Operating voltage range :3.3V-5.5V
Working current : Average 0.5mA
Output : Single bus digital signal
measuring range : humidity 20~90%RH, temperature 0~50℃
precision : humidity ±5%, temperature ±2℃
The resolution of the : humidity 1%, temperature 1℃
DHT11 The digital wet temperature sensor adopts single bus data format . A single data pin port completes the bidirectional transmission of input and output .
Its data packet consists of 5Byte(40Bit) form . The data is divided into decimal part and integer part , A complete data transfer is 40bit, High first out .
DHT11 The data format of is :8bit Humidity integer data +8bit Humidity decimal data +8bit Temperature integer data +8bit Temperature decimal data +8bit The checksum .
among The checksum data is the sum of the first four bytes . The sensor data output is uncoded binary data . data ( humidity 、 temperature 、 Integers 、 decimal ) Should be handled separately .
The sensor data output is uncoded binary data . data ( humidity 、 temperature 、 Integers 、 decimal ) Should be handled separately .
DHT11 Start sending data flow

After the host sends the start signal , Delay waiting for 20us-40us Post read DH11T Your response signal , The read bus is low , explain DHT11 Send a response signal , DHT11 After sending the response signal , Then pull up the bus , Prepare to send data , each bit The data starts at a low level , The format is shown in the figure below . If the read response signal is high , be DHT11 No response , Please check whether the line is connected properly .
First, the host sends a start signal , namely : Pull down the data line , keep t1( At least 18ms) Time , Then pull up the data cable t2(20~40us) Time , Then read DHT11 Response , Normal word ,DHT11 Will pull down the data cable , keep t3(40~50us) Time , As a response signal , then DHT11 Pull up the data line , Protect t4(40~50us) After time , Start outputting data .
Host reset signal and DHT11 response signal 
Numbers ‘ 0’ Signal representation


The program should distinguish data 0 And data 1 The format of : First judge the level state of the pin at this time , If it is low level, it will keep waiting , Until the high level appears , Delay after high level appears 40us, And read the level state after delay , If it is high , Then the data is 1, Otherwise 0
The transmission is over 40 After bit data , The sensor outputs another 50us After the low level of , Release the data bus , The acquisition process ends .
The code is as follows :
The selected single chip microcomputer model :STM32L052K8* In fact, it doesn't matter which MCU , Mainly the realization of temperature and humidity module
connection :PA6 Connected to the DATA On the mouth

The code is as follows :
DHT11.c
#include "dht11.h"
static GPIO_InitTypeDef GPIO_InitStruct;
uint8_t U8FLAG,k;
uint8_t U8count,U8temp;
uint8_t U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
uint8_t U8T_data_H_temp,U8T_data_L_temp,U8RH_data_H_temp,U8RH_data_L_temp,U8checkdata_temp;
uint8_t U8comdata;
void delay_us(uint16_t j) // Microsecond delay
{
uint8_t i;
for(;j>0;j--)
{
for(i=0;i<8;i++);
}
}
void delay_ms(uint16_t j) // Millisecond delay
{
uint16_t i;
for(;j>0;j--)
{
for(i=0;i<8000;i++);
}
}
void DHT11_INIT(void) // configure port PA6
{
__HAL_RCC_GPIOB_CLK_ENABLE();
GPIO_InitStruct.Pin = DHT11_SDA;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH ;
HAL_GPIO_Init(DHT11_COM, &GPIO_InitStruct);
}
void COM(void) // start-up Read
{
uint8_t i;
for(i=0;i<8;i++)
{
U8FLAG=2; // Initialization number
while((!DHT11_SDA_READ())&&U8FLAG++); // Read port acquisition , Low level indicates starting signal
delay_us(10);
delay_us(10);
delay_us(10); // wait for
U8temp=0;
if(DHT11_SDA_READ())U8temp=1; // There are data to
U8FLAG=2;
while((DHT11_SDA_READ())&&U8FLAG++); // Read
if(U8FLAG==1)break;
U8comdata<<=1;
U8comdata|=U8temp;
}
}
void RH(void) //
{
DHT11_SDA_L(); // Pull it down
delay_ms(18);
DHT11_SDA_H(); // pull up
delay_us(10);
delay_us(10);
delay_us(10);
delay_us(10); // wait for
if(!DHT11_SDA_READ()) // Low level entry
{
U8FLAG=2;
while((!DHT11_SDA_READ())&&U8FLAG++);// Data waiting
U8FLAG=2;
while((DHT11_SDA_READ())&&U8FLAG++); // start-up
COM(); //
U8RH_data_H_temp=U8comdata; // Read humidity high
COM();
U8RH_data_L_temp=U8comdata; // Read the low humidity
COM();
U8T_data_H_temp=U8comdata; // Read the high temperature
COM();
U8T_data_L_temp=U8comdata; // Read the low temperature
COM();
U8checkdata_temp=U8comdata; // Check bit
DHT11_SDA_H();
U8temp=(U8T_data_H_temp+U8T_data_L_temp+U8RH_data_H_temp+U8RH_data_L_temp);//40 Bit data addition
if(U8temp==U8checkdata_temp) // data verification contrast
{
U8RH_data_H=U8RH_data_H_temp; // Pay the read value
U8RH_data_L=U8RH_data_L_temp;
U8T_data_H=U8T_data_H_temp;
U8T_data_L=U8T_data_L_temp;
U8checkdata=U8checkdata_temp;
}
}
}
DHT11.h
#ifndef __DHT11_H
#define __DHT11_H
/* Includes ------------------------------------------------------------------*/
#include "stm32l0xx_hal.h"
#define Data_0_time 4
#define DHT11_SDA GPIO_PIN_6
#define DHT11_COM GPIOA
#define DHT11_SDA_H() HAL_GPIO_WritePin(DHT11_COM,DHT11_SDA,GPIO_PIN_SET)
#define DHT11_SDA_L() HAL_GPIO_WritePin(DHT11_COM,DHT11_SDA,GPIO_PIN_RESET)
#define DHT11_SDA_READ() HAL_GPIO_ReadPin(DHT11_COM,DHT11_SDA) // Macro definition read data
extern uint8_t U8T_data_H,U8T_data_L,U8RH_data_H,U8RH_data_L,U8checkdata;
void delay_us(uint16_t j);
void delay_ms(uint16_t j);
void DHT11_INIT(void);
void COM(void);
void RH(void);
#endif /* __DHT11_H */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
My implementation is realized through serial port , The configuration information of the serial port is also very simple , The code information of the serial port is not posted here , The effect is as follows :

边栏推荐
- [STM32 learning] (6) use of serial port 1 (usart1)
- Reading makes people improve my list
- It's eleven again. Those jokes about nagging programmers going home for blind dates
- Getting started with web security - open source firewall pfsense installation configuration
- MySQL status view qps/tps/ cache hit rate view
- Basic knowledge of PHP - complete collection of PHP functions
- 【机器人学习】机构运动学分析与matlab仿真(三维模型+word报告+matlab程序)
- Trie tree template 2
- Spark Learning: compile spark source code in win10
- Lung CT segmentation challenge 2017 dataset download and description
猜你喜欢

This article takes you to understand the dynamic memory allocation of C language

C#/VB. Net: convert word or EXCEL documents to text

Build practical product help documents to improve user satisfaction
![Cyclicbarrier and countdownlatch [concurrent programming]](/img/38/3305a0cdb6de40e1370cc93c8e5014.png)
Cyclicbarrier and countdownlatch [concurrent programming]
![[MySQL] - deep understanding of index](/img/a6/6ca1356fe11bd33ec7362ce7cdc652.png)
[MySQL] - deep understanding of index

Raspberry Pie: serial port login does not display print information

When the hot tea is out of stock, what does the new tea drink rely on to continue its life?

It's eleven again. Those jokes about nagging programmers going home for blind dates

Recursion - if the function calls itself internally, then the function is a recursive function & the effect is the same as that of the loop & the push condition return should be added, otherwise stack

Add SSH key to bitbucket
随机推荐
Installation UMI tutorial (error reporting and solutions)
PHP Basics - PHP magic constants
Spark Learning: a form of association in a distributed environment?
Huawei wireless device security policy configuration command
Scala learning: why emphasize immutable objects?
Reading makes people improve my list
Common evaluation indexes of medical image segmentation
Linux deployment mysql8.0
Scarcity in Web3: how to become a winner in a decentralized world
Re6: reading paper licin: a heterogeneous graph based approach for automatic legal stat identification fro
Cyclicbarrier and countdownlatch [concurrent programming]
ASI-20220222-Implicit PendingIntent
[STM32 learning] (6) use of serial port 1 (usart1)
The heads of the five major international institutions called for urgent action to deal with the global food security crisis
Use of jstack "JVM common commands"
The most complete solution for distributed transactions
获取所有股票历史行情数据
Boundless dialogue | participate in the live broadcast on July 25 and win the prize
Why add where exists() to the update select statement? And update with a with statement
[Luogu p3426] SZA template (string) (KMP)