1. Overview of DHT11
Data transfer logic
A complete data transmission of the module is 40 bits, with the high bit first
Data Format
8bit humidity integer data + 8bit humidity decimal data + 8bit temperature integer data + 8bit temperature decimal data + 8bit checksum
Communication process sequence diagram
2. Check whether the module exists
According to the following sequence diagram, perform communication initialization and check whether the module exists and whether the function is normal.
Sequential logic analysis
a : dht = 1
b :dht = 0
Delay at least 18ms
c: dht = 1
Stuck point d: while(dht)
Card e point: while(!dht)
Stuck point f: while(dht)
Code
void DHT11_Start()
{
dht = 1;
dht = 0;
//Delay 30ms
Delay30ms();
dht = 1;
//Stuck point d: while(dht); stuck point e: while(!dht); stuck point f: while(dht)
while(dht);
while(!dht);
while(dht);
}
3. Read temperature and humidity data
Timing analysis of DHT11 transmitting 0
Timing analysis of DHT11 transmission 1
Code
char dates[5];
void Read_Data_From_DHT()
{
int i;//wheel
int j;//How many times to read in each round
char tmp;
char flag;
DHT11_Start();
for(i=0;i<5;i++){
for(j=0;j<8;j++){
while(!dht);//Waiting for stuck g point
Delay40us();//Valid data are all high level, the duration is different, 40us reading, low level 0, high level 1
if(dht == 1){
flag = 1;
while(dht);
}else{
flag = 0;
}
tmp = tmp << 1;
tmp |= flag;
}
datas[i] = tmp;
}
}
4. Serial port printing
#include "reg52.h"
#include "intrins.h"
sbit ledOne = P3^7;
sbit dht = P3^3;//The data of the module is inserted in p3.3
char dates[5];
sfr AUXR = 0x8E;
void UartInit(void) //9600bps@11.0592MHz
{
AUXR = 0x01;
SCON = 0x40; //Configure serial port working mode 1, REN disables reception
TMOD &= 0xF0;
TMOD |= 0x20; //Timer 1 working mode bit 8-bit automatic reloading
TH1 = 0xFD;
TL1 = 0xFD; //Initial value of 9600 baud rate
TR1 = 1;//Start the timer
}
void sendByte(char data_msg)
{
SBUF = data_msg;
while(!IF);
IF = 0;
}
void sendString(char* str)
{
while( *str != ''){
sendByte(*str);
str++;
}
}
void Delay30ms() //@11.0592MHz
{
unsigned char i, j;
i = 54;
j = 199;
do
{
while (--j);
} while (--i);
}
void Delay40us() //@11.0592MHz
{
unsigned char i;
_nop_();
i = 15;
while (--i);
}
void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
i = 8;
j = 1;
k = 243;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void DHT11_Start()
{
dht = 1;
dht = 0;
//Delay 30ms
Delay30ms();
dht = 1;
//Stuck point d: while(dht1); stuck point e: while(!dht); stuck point f: while(dht)
while(dht);
while(!dht);
while(dht);
}
void Read_Data_From_DHT()
{
int i;//wheel
int j;//How many times to read in each round
char tmp;
char flag;
DHT11_Start();
for(i= 0;i < 5;i++){
for(j=0;j<8;j++){
while(!dht);//Waiting for stuck g point
Delay40us();//Valid data are all high level, the duration is different, 40us reading, low level 0, high level 1
if(dht == 1){
flag = 1;
while(dht);
}else{
flag = 0;
}
tmp = tmp << 1;
tmp |= flag;
}
datas[i] = tmp;
}
}
void main()
{
ledOne = 1;
UartInit();
Delay1000ms();
Delay1000ms();
while(1){
Delay1000ms();
Read_Data_From_DHT();
sendString("H:"); //Humidity
sendByte(datas[0]/10 + 0x30);
sendByte(datas[0]%10 + 0x30);
sendByte('.');
sendByte(datas[1]/10 + 0x30);
sendByte(datas[1]%10 + 0x30);
sendString("rn");
sendString("T:"); //Temperature
sendByte(datas[2]/10 + 0x30);
sendByte(datas[2]%10 + 0x30);
sendByte('.');
sendByte(datas[3]/10 + 0x30);
sendByte(datas[3]%10 + 0x30);
sendString("rn");
//The decimal part of the humidity is 0; the decimal part of the temperature can only display 1 decimal digit, and the code needs to be modified.
}
}
Previous article:[51 microcontroller STC89C52] IO port simulates PWM control of SG90 servo
Next article:[51 MCU STC89C52] Use of LCD1602 LCD screen
Recommended ReadingLatest update time:2024-11-16 22:40
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- What is the function of decoupling capacitor in microcontroller?
- [Shanghai Hangxin ACM32F070 development board + touch function evaluation board]——GPIO routine (edited with Sphinx document)
- Classic time series
- TI - Delivering Ultra-High Power Density for 100W USB Power Delivery Adapters
- What challenges does the epidemic bring to engineers?
- Seeking routines for driving LPS33HW pressure sensor of STM32F103 series
- Just to tell you a joke, I spent the whole afternoon trying to write a for loop properly.
- Three solutions for implementing motor control design, which one is better?
- 【GD32307E-START】+Serial communication function test
- [HPM-DIY] HPM6750 openmv transplantation is successful, using cherryusb as repl interaction