Use STM32F103ZET6 to collect DHT11 temperature and humidity serial port display

About DHT11

DHT11 is a digital temperature and humidity sensor, DHT11 is a temperature and humidity composite sensor with calibrated digital signal output. It uses dedicated digital module acquisition technology and temperature and humidity sensing technology to ensure that the product has reliable stability, fast response, and strong anti-interference ability. The sensor includes a high polymer resistive humidity sensing element and an NTC temperature measuring element, and is connected with a high-performance 8-bit single-chip microcomputer, and communicates with the microprocessor through a single bus, only one wire is needed.
Insert picture description here

Related parameters

1. DHT11 data structure
DHT11 digital humidity temperature sensor adopts single bus data format. That is, a single data pin port completes input and output bidirectional transmission. Its data packet is composed of 5Byte (40Bit). The data is divided into decimal part and integer part. The specific format is explained below.
A complete data transmission is 40bit, and the high order comes out first.
Data format: 8bit humidity integer data + 8bit humidity decimal data + 8bit temperature integer data + 8bit temperature decimal data + 8bit checksum The
checksum data is the addition of the first four bytes.
The output of sensor data is uncoded binary data. Data (humidity, temperature, integer, decimal)
should be handled separately. If, at a certain time, read the following 5Byte data from the sensor: the
Insert picture description here
calculation method of temperature and humidity can be obtained from the above data format
humi (humidity) = byte4. Byte3=45.0 (%RH)
temp (temperature) = byte2. Byte1=28.0 (℃)
jiaoyan(check)= byte4+ byte3+ byte2+ byte1=73(=humi+temp)(check correct)
2. DHT11 transmission data timing After the
host sends the start signal, wait for 20us-40us before reading the response signal of DH11T, read Taking the bus as a low level means that DHT11 sends a response signal. After DHT11 sends a response signal, pull the bus high and prepare to send data. Each bit of data starts with a low level.
Insert picture description here
3. The number "0" and number "1" of DHT11 indicate the method
The number 0 and number 1 of DHT11 are different from the common ones. The general data means 0 is low level and 1 is high level. DHT11 is different. After data transmission starts, the number 0 means high level. 26-28us, the representation of number 1 is high level 116-118us.
Insert picture description here

Insert picture description here

Code articles

The IO port defined in this experiment is PB11, and the transmitted data is serial port
1. The host sends the start signal to DHT11, that is, the microcontroller is pulled down for 18ms and then pulled up for 20-40us.

static void DHT11_Rst(void)
{
    
                    
		GPIO_SETOUT();											//配置成输出模式
    GPIO_ResetBits(DHT11_IO,DHT11_PIN); //拉低数据线
    Delay_ms(20);    										//拉低至少18ms
    GPIO_SetBits(DHT11_IO,DHT11_PIN); 	//拉高数据线 
	  Delay_us(30);     									//主机拉高20~40us
		GPIO_ResetBits(DHT11_IO,DHT11_PIN);
}

2. DHT11 sends a response signal to the host (32 single-chip microcomputer), the data line of DHT11 will be pulled down by 40-50us, and by 40-50us, the IO port of the single-chip computer will detect the signal.

函数名:static u8 DHT11_Check(void)
参数说明:无
返回值:检测到回应-->返回1,否则0
函数作用:检测DHT11的响应信号
***********************************************/
static u8 DHT11_Check(void) 	   
{
    
       
	u8 retry=0;
	GPIO_SETIN();			//设置为输入模式	
	
  while (!GPIO_ReadInputDataBit(DHT11_IO,DHT11_PIN) && retry<100)//DHT11会拉低40~50us
	{
    
    
		retry++;
		Delay_us(1);
	}
	if(retry >= 100)	//超时未响应/未收到开始信号,退出检测
		return 0;
	else 
		retry = 0;
  while (GPIO_ReadInputDataBit(DHT11_IO,DHT11_PIN) && retry<100)//DHT11拉低后会再次拉高40~50us
	{
    
    
		retry++;
		Delay_us(1);
	}
	if(retry>=100)		//超时,DHT11工作出错,退出检测
		return 0;
	return 1;					//设备正常响应,可以正常工作
}

3. DHT11 starts to transmit data to the single-chip microcomputer. There are two data transmission functions, one is to transmit bytes, and the function to transmit an 8-bit string is written by the function of transmitting bytes.

函数名:static u8 DHT11_Read_Bit(void)
参数说明:无
返回值:返回从DHT11上读取的一个Bit数据
函数作用:从DHT11上读取一个Bit数据
***********************************************/
static u8 DHT11_Read_Bit(void)
{
    
    
 	u8 retry = 0;
	//DHT11的Bit开始信号为12-14us低电平
	while(GPIO_ReadInputDataBit(DHT11_IO,DHT11_PIN) && retry<100)//等待变为低电平(等待Bit开始信号)
	{
    
    
		retry++;
		Delay_us(1);
	}
	retry = 0;
	while(!GPIO_ReadInputDataBit(DHT11_IO,DHT11_PIN) && retry<100)//等待变高电平(代表数据开始传输)
	{
    
    
		retry++;
		Delay_us(1);
	}
	Delay_us(30);//等待30us
	//0信号为26-28us,1信号则为116-118us,所以说超过30us去读取引脚状态就可以知道传输的值了
	if(GPIO_ReadInputDataBit(DHT11_IO,DHT11_PIN)) return 1;
	else return 0;		   
}


/***********************************************************************
函数名:static u8 DHT11_Read_Byte(void)
参数说明:无
返回值:返回从DHT11上读取的一个byte数据
函数作用:从DHT11上读取一个byte数据
************************************************************************/
static u8 DHT11_Read_Byte(void)    
{
    
            
  u8 i,dat;
  dat=0;
	
	for (i=0;i<8;i++) 
	{
    
    
   	dat<<=1; 
	  dat|=DHT11_Read_Bit();
  }	
	
  return dat;
}

4. After DHT11 data collection is completed, it is displayed on the serial port through the main function

函数名:int main(void)
参数说明:无
返回值:无
函数作用:主函数
***************************************************************************/
 int main(void)
 {
    
    	
	 
   clock_init();
	 uart_init(115200);		//初始化串口
	 printf("wecome to DHT11");
	 
	 
	 //初始化DHT11(有BUG,第一次上电总是失败,按一下复位按钮又能进了)
	 if(!DHT11_Init()){
    
    
		 printf("\r\n EEROR! THE DHT11 HAS NO RESPOND...");
		 //while(1);
	 }
	 printf("\r\n THE DHT11 HAS RESPOND");
	 Delay_ms(10);		//这里延时10ms主要是因为,刚刚接收到响应信息,要等DHT11发送完信息
	 
	 while(1)
	 {
    
    
		 if(DHT11_Read_Data(&temp,&humi))
			printf("\r\n temp:%d,humi:%d",temp,humi);
		 else
		 //printf("\r\n EEROR! THE DHT11 HAS NO RESPOND...");//由于是库函数编程,不能准确把握函数的执行时间,
																													//所以会经常出现这条警告信息
		 Delay_ms(100);
	 }
	  
 }
 

Wiring and experimental results

Insert picture description here

Insert picture description here

to sum up

DHT11 is a temperature and humidity module with a relatively high cost performance. It is relatively intuitive to display temperature and humidity with a serial port. If you publish such articles for a long time, please pay attention to it.

Guess you like

Origin blog.csdn.net/qq_42250136/article/details/112963129