[Lanqiao Cup Embedded Expansion Board]—Detailed explanation of temperature and humidity sensor DHT11 (program source code attached)

1. Temperature and humidity sensor

Insert picture description here ————————————Insert picture description here

In general applications, Pin1 is connected to VDD, Pin4 is connected to GND, and Pin2 is used as the communication pin by default.

2. Communication process: serial communication

  1. Receive commands from the MCU or send data to the MCU through a port. When sending data, a complete data contains 40 bits, and the sensor sends higher data bit first (the sensor sends higher data bit first).
  2. Data format (sent from left to right)

Insert picture description here

  1. Check format
    Take the lower eight bits of the sum result to be
    ![Insert picture description here](https://img-blog.csdnimg.cn/20210226150306617.png)
    correct. Check : Return True
    Insert picture description here
    error. Check : Return Fault.
    Insert picture description here

Three, communication sequence diagram

1. Preview of the overall timing diagram :process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzQ1Njg5Nzkw,size_16,color_FFFFFF,t_70)

2. MCU sends start signal to DHT

The idle state of the data single bus is a high voltage level. When the single-chip microcomputer communicates with the single-chip microcomputer, DHT11 starts, the program of the single-chip microcomputer changes the voltage level of the data single bus from high to low. This process requires at least 18ms to ensure that the DHT detects the signal of the single-chip microcomputer, and then the single-chip microcomputer pulls up the voltage and waits for 20-40us DHT response

Insert picture description here

3. DHT responds to MCU

After DHT detects the start signal, it will send out a low voltage response signal for 80us. Then the DHT program sets the data single bus voltage from low to high to 80us in preparation for DHT to send data.
When the data single bus is at a low voltage level, it means that the DHT is sending a response signal. Once the DHT sends out a response signal, it pulls up the voltage and maintains 80us to prepare for data transmission.
When DHT sends data to the microcontroller, each bit of data starts from a low voltage level of 50us, and the length of the subsequent high voltage level signal determines whether the data bit is 0 or 1. (See Figure 4 and Figure 5 below).
Insert picture description here
Insert picture description here

Fourth, the procedure

Insert picture description here
DHT11 power supply is 3-5.5V DC. When powering the sensor, do not send any command to the sensor within one second to pass the unstable state. A 100nF capacitor can be added between VDD and GND for power supply filtering.

1、Mode_Out(uint8_t signal)

//MCU 控制 PA7输出 “0” 或 “1”
void Mode_Out(uint8_t signal)
{
    
     
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
	
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	if(!signal)
	{
    
    
		GPIO_ResetBits(GPIOA, GPIO_Pin_7);
	}
	else 
	{
    
    	
		GPIO_SetBits(GPIOA, GPIO_Pin_7);
	}
}

2、void Mode_In(void)

//控制MCU接收数据
void Mode_In(void)
{
    
    
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_Init(GPIOA, &GPIO_InitStructure);
}

3、void DHT11_Init(void)

void DHT11_Init(void)
{
    
    
	Mode_Out(1);	//开始之前  先拉高信号线		
}

4、uint8_t Receive_Data(void)

//接收 1 Byte 数据
uint8_t Receive_Data(void)
{
    
    
	u8 i,temp=0,j=220;
	for(i=0;i<8;i++)
	{
    
    
		while(!GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7));//50us低电平结束
		
		Delay_us(40);
		
		if(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7))//为高时
		{
    
    
			temp=(temp<<1)|1;
			while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7)&&(j--));	
		}	
		else
		{
    
    
			temp=(temp<<1)|0;
		}
	}
	return temp;
}

5、uint8_t Ready_DHT11(void)


uint8_t Ready_DHT11(void)
{
    
    
	DHT11_Init();
	
	Mode_Out(0);Delay_us(18000);//18ms
	Mode_Out(1);Delay_us(30);//30us
	
	Mode_In();//输入模式
	if(!GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7)) //判断从机的低电平相应
	{
    
    
		while(!GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7));//从机发出的80us低电平相应信号结束
		while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7));//从机发出的80us高电平信号结束
		
		Humidity_H = Receive_Data(); //接收高8位湿度数据
		Humidity_L = Receive_Data(); //接收低8位湿度数据
		
		Temperature_H = Receive_Data(); //接收高8位温度数据
		Temperature_L = Receive_Data();	//接收低8位温度数据
		
		Check_Out = Receive_Data();	//接收校验位	
	}
	if(((Humidity_H+Humidity_L+Temperature_H+Temperature_L)&(0xFF))==Check_Out) //校验
	{
    
    
		return 1;		
	}
	else
	{
    
    
		return 0;
	}
			
}

6. Delay used

void Delay_us(u32 nTime)
{
    
    
	SysTick_Config(SystemCoreClock/1000000);
	DelayTiming = nTime;
	while(DelayTiming != 0);
}
void SysTick_Handler(void)
{
    
    
	if(DelayTiming != 0){
    
    
		DelayTiming--;
	}
}

7、main

#include "stm32f10x.h"
#include "bsp_seg.h"
#include "lcd.h"
#include "bsp_dht11.h"
#include <stdio.h>
u32 DelayTiming = 0;

void Delay_Ms(u32 nTime)
{
    
    
	DelayTiming = nTime;

	while(DelayTiming != 0);
}
char txt[20];
int main(void)
{
    
    
	SysTick_Config(SystemCoreClock/1000000);
	
	STM3210B_LCD_Init();
	LCD_Clear(White);
	STM3210B_LCD_Init();
	SEG_GPIO_Config();

	LCD_SetTextColor(White);
	LCD_SetBackColor(Blue);

	LCD_ClearLine(Line0);
	LCD_ClearLine(Line1);
	LCD_ClearLine(Line2);
	LCD_ClearLine(Line3);
	LCD_ClearLine(Line4);
	
	LCD_DisplayStringLine(Line1,(u8*)"    SDHT11  DEMO     ");
	LCD_SetTextColor(Blue);
	LCD_SetBackColor(White);

	while(1)
	{
    
    
			if(1==Ready_DHT11())
			{
    
    
				sprintf(txt,  "Moisture:%2d%%",Humidity_H);
				LCD_DisplayStringLine(Line6,txt);
				sprintf(txt,  "Temperature:%2d",Temperature_H);
				LCD_DisplayStringLine(Line8,txt);
				SEG_Display(Temperature_H/10,Temperature_H%10,12);								
			} 
			Delay_us(1000000);//延时1s
	}
}

Display, just read the integer part of the corresponding temperature and humidity

Guess you like

Origin blog.csdn.net/qq_45689790/article/details/114129398