当前位置:网站首页>STM32 external DHT11 display temperature and humidity
STM32 external DHT11 display temperature and humidity
2022-07-04 03:57:00 【Hua Weiyun】
1 DTH11 Temperature and humidity sensor
1.1 Data reading protocol
Micro controller MCU And DHT11 Communication and synchronization between , Use single bus data format , Primary communication time 4ms about .
user MCU Send a start signal after ,DHT11 From low power mode to high speed mode , Wait for the end of the host start signal ,DHT11 Send a response signal , Send out 40bit The data of , And trigger a signal acquisition , Users can choose to read some data .
From mode ,DHT11 Receive the start signal to trigger a temperature and humidity acquisition , If no start signal is received from the host ,DHT11 Do not actively collect temperature and humidity . After collecting data, switch to low speed mode .
1.1.1 Start signal
The bus idle state is high ,MCU Pull the bus down and wait DHT11 Respond to ,MCU Pull down the bus must be greater than 18 millisecond , Guarantee DHT11 Can detect the start signal .
DHT11 After receiving the start signal from the host , wait for MCU The start signal is over , And then send 80us Low level response signal .
MCU After sending the start signal , Delay waiting for 20-40us after , Read DHT11 The response signal ,MCU After sending the start signal , You can switch to input mode , Or the average output high power can , The bus is pulled up by a pull-up resistor .
1.1.2 Data digital signal
The bus is low , explain DHT11 Send a response signal ,DHT11 After sending the response signal , Then pull up the bus 80us, Prepare to send data , each bit All the data are based on 50us The low level slot starts , The length of the high level determines that the data bit is 0 still 1.
Numbers 0 And number 1 It means , As shown in the figure below :
Numbers 0:50us After the low level starts ,26-28us High level of Express 0
Numbers 1:50us After the low level starts ,70us High level of Express 1
If the read response signal is high , be DHT11 No response , It is necessary to check whether the line is connected normally .
When the last one bit After the data is transmitted ,DHT11 Pull down the bus 50us, Then, the bus is pulled up by the pull-up resistance and enters the idle state .
1.1.3 Temperature and humidity data format
A complete data transfer is 40bit, High first out . The data is divided into decimal part and integer part , data format :
8bit Humidity integer data
8bit Humidity decimal data
8bit Temperature integer data
8bit Temperature decimal data
8bit The checksum
When the data transmission is correct, the check sum data is equal to “ 8bit Humidity integer data +8bit Humidity decimal data +8bit Temperature integer data +8bit Temperature decimal data ” The end of the result 8 position .
1.2 Hardware wiring
DHT11 Only one line is needed for data reading , I'm using PB8, in addition ,OLED Used to display the value of temperature and humidity , Use IIC signal communication , It uses PB6 and PB7.
2 Programming
according to DHT11 Data reading protocol , Write the corresponding data reading function .
2.1 DHT11 Reset and detect response functions
First of all MCU towards DHT11 Start signal sent , Pull it down 20ms, Pull up again 30us.
u8 DHT11RstAndCheck(void){ u8 timer = 0; __set_PRIMASK(1); // General manager Guan interrupted DHT11_OUT = 0; // Output low level delay_ms(20); // Pull down at least 18ms DHT11_OUT = 1; // Output high level delay_us(30); // pull up 20~40us while (!DHT11_IN) // Wait for the bus to pull down ,DHT11 Will pull down 40~80us As a response signal { timer++; // Count when the bus is pulled down delay_us(1); } if (timer>100 || timer<20) // Determine the response time { __set_PRIMASK(0); // General interruption return 0; } timer = 0; while (DHT11_IN) // wait for DHT11 Release the bus , The duration of the 40~80us { timer++; // Count when the bus is pulled high delay_us(1); } __set_PRIMASK(0); // General interruption if (timer>100 || timer<20) // Detect the high level after the response signal { return 0; } return 1;}
2.2 data fetch
MCU towards DHT11 After sending the start signal , You can receive DHT11 The data returned , Read the humidity and temperature at one time .
/* Read one byte of data , Return value - Data read */u8 DHT11ReadByte(void){ u8 i; u8 byt = 0; __set_PRIMASK(1); // General manager Guan interrupted for (i=0; i<8; i++) { while (DHT11_IN); // Wait for low level , Before the data bits 50us Low level slot while (!DHT11_IN); // Wait for high level , Start transmitting data bits delay_us(40); byt <<= 1; // Because of the high position , So move left byt, The lowest point is made up of 0 if (DHT11_IN) // Read the bus level value to byt Lowest middle { byt |= 0x01; } } __set_PRIMASK(0); // General interruption return byt;}/* Read data once , Returns the parameter :Humi- humidity ,Temp- temperature ; Return value : 0- success ,1- Failure */u8 DHT11ReadData(float *Humi, float *Temp){ s8 sta = 0; u8 i; u8 buf[5]; if (DHT11RstAndCheck()) // Detect the response signal { for(i=0;i<5;i++) // Read 40 Bit data { buf[i]=DHT11ReadByte(); // Read 1 Bytes of data } if(buf[0]+buf[1]+buf[2]+buf[3] == buf[4]) // Check success { u8 H_inte = buf[0]; // Humidity integer part data u8 H_frac = buf[1]; // Humidity fractional data u8 T_inte = buf[2]; // Temperature integer part data u8 T_frac = buf[3]; // Temperature fractional data 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 // The response failed and returned -1 { *Humi = 88; // The response failed and returned -1 *Temp = 88; // The response failed and returned -1 sta = 1; } return sta; }
2.3 initialization
Use DHT11 Before , Conduct pin initialization and device initialization .
/*DHT11 Initialization function */u8 DHT11Init(void){ GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);// Can make GPIOC Port clock GPIO_SetBits(GPIOB,GPIO_Pin_8); // Set up PC13 Output high level ,( Setting the pin level first can avoid IO Possible burrs during initialization ) GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; // Set up DHT11 Data pins ->PC13 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD; // Set to open drain output mode GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // Set the output rate to 50MHz GPIO_Init(GPIOB, &GPIO_InitStructure); // initialization GPIOC port return DHT11RstAndCheck(); // return DHT11 state }
3 test
After transplantation U8g2 Library hello_world Make modifications on the routine , Display temperature and humidity on the screen . Notice the small circle in degrees Celsius , I don't know how to show it directly in the form of symbols , I drew a small hollow circle here alone .
int main(void){ delay_init(); // Delay function initialization LED_Init(); // Initialization and LED Connected hardware interface 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)); }}
The test results are as follows :
4 summary
This article introduces how to use STM32 External temperature and humidity DHT11 Realize the reading of temperature and humidity data , And pass OLED Data display .
边栏推荐
- Consul of distributed service registration discovery and unified configuration management
- Formulaire day05
- Jenkins configures IP address access
- A review of reverse reinforcement learning at Virginia Tech (VT)
- SQL語句加强練習(MySQL8.0為例)
- Objective C attribute keyword
- SQL statement strengthening exercise (MySQL 8.0 as an example)
- Reduce function under functools
- Recent learning fragmentation (14)
- Summary of Chinese remainder theorem
猜你喜欢
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.
GUI Graphical user interface programming (XIV) optionmenu - what do you want your girlfriend to wear on Valentine's day
Es network layer
Summary of Chinese remainder theorem
Reduce function under functools
Mindmanager2022 efficient and easy to use office mind map MindManager
Leetcode51.n queen
If you have just joined a new company, don't be fired because of your mistakes
New year's first race, submit bug reward more!
Cesiumjs 2022^ source code interpretation [0] - article directory and source code engineering structure
随机推荐
Smart subway | cloud computing injects wisdom into urban subway transportation
Aperçu du code source futur - série juc
Apple submitted the new MAC model to the regulatory database before the spring conference
Day05 錶格
Recursive structure
Development of digital collection trading platform development of digital collection platform
My opinion on how to effectively telecommute | community essay solicitation
vue多级路由嵌套怎么动态缓存组件
Objective-C description method and type method
Reduce function under functools
[source code analysis] model parallel distributed training Megatron (5) -- pipestream flush
微信公众号网页授权
Rhcsa day 3
How to pipe several commands in Go?
Katalon framework test web (XXVI) automatic email
Nbear introduction and use diagram
ctf-pikachu-CSRF
Deep thinking on investment
Mindmanager2022 efficient and easy to use office mind map MindManager
Want to do something in production? Then try these redis commands