当前位置:网站首页>[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 :

边栏推荐
- The most complete solution for distributed transactions
- Firewall off and on command
- RxJS Beginner Guide
- Firewalld firewall related commands
- When the hot tea is out of stock, what does the new tea drink rely on to continue its life?
- 详解LinkedList
- Problem: filesystemversionexception: you have version null and I want version 8
- 【机器人学习】机构运动学分析与matlab仿真(三维模型+word报告+matlab程序)
- Arduino drive Lora module master node
- OPENCV学习DAY5
猜你喜欢

Android Version Description security privacy 13

ASI-20220222-Implicit PendingIntent

Foreign lead operation takes one month to collect money, and the sideline still needs it

Spark Learning: a form of association in a distributed environment?

07 Jason module

Embedded development: Tools - optimizing firmware using DRT
![[STM32 learning] (6) use of serial port 1 (usart1)](/img/b1/430d3501a99e46958c066f7fd7eee9.png)
[STM32 learning] (6) use of serial port 1 (usart1)

Vscode failed to use SSH Remote Connection (and a collection of other problems)
![Cyclicbarrier and countdownlatch [concurrent programming]](/img/38/3305a0cdb6de40e1370cc93c8e5014.png)
Cyclicbarrier and countdownlatch [concurrent programming]

程序的编译与链接
随机推荐
[C language] implementation of three versions of address book small project (including source code)
财务数字化转型
Getting started with identityserver4
Source insight 3.5 comment garbled
Tag the specified commit and submit the tag
Arduino serial port information reading and output
PHP caching system - PHP uses Memcache
[don't bother to strengthen learning] video notes (III) 3. SARS (lambda)
Jenkins post build script does not execute
OPENCV学习DAY5
[don't bother to strengthen learning] video notes (IV) 2. Dqn realizes maze walking
Spark Learning: implement compact table command
Spark Learning: compile spark source code in win10
Server load and CPU performance tuning
Basic knowledge of PHP - complete collection of PHP functions
error: field ‘XXX’ declared as a function
Vscode failed to use SSH Remote Connection (and a collection of other problems)
What are the 6% annualized products?
How does SRE and development of Google cooperate
Nuggets manufacturing industry, digital commerce cloud supply chain collaborative management system to achieve full chain intelligent management and control