I. Hardware INTRODUCTION
RH is relative humidity, is defined by 0 point temperature, generally, RH is the most suitable between 45%~65%.
Note: NTC is a thermistor, the output is: single-bus digital signal, single-line bidirectional serial communication.
Note: When the pull-up resistor is configured as an open-drain output, the drive capability can be increased to reduce the CPU power consumption.
Two. Program key points: 1) Accurate switch of pin input and output, 2) accurate delay of output level, 3) wait and delay reading of input level and time limit of waiting.
1.MCU How to set the start signal?
/*
The MCU sends a start signal to the DHT11, the bus idle state is high, the MCU pulls the bus low by at least 18ms,
Ensure that the DHT11 can detect the start signal, after the host sends the start signal, can switch to the input mode, or switch to
The high-power average can be switched to high-power normally 20~40us, and then start reading the response signal.
*/
void Dht11_rst (void)
{
Dht11_io_out (); Configuration pin for general push-pull output mode
Daobb = 0; Pull Low DQ
Systickdelay (20000);//At least 18ms
Daobb = 1;
Systickdelay (30);//host Pull High 20~40us
}
2.MCU How to accept the response signal?
/*
Waiting for DHT11 's response
Return 1: The presence of DHT11 is not detected
Return 0: Wait for the start signal of the host to end after the DHT11 has received the start signal from the host
Then send a 80US low-level response signal, after sending the response signal, pull the bus up 80us, ready to send data */
U8 Dht11_check (void)
{
U8 retry=0;
Dht11_io_in ();//set A11 port as input
The priority of/*&& and < is < higher than &&*/
while (dht11_dq_in&&retry<100)//dht11 pulls down 40~80US
{
retry++;
Systickdelay (1);//Here the system tick function is configured to 1US interrupts
}
if (retry>=100) return 1;
else retry=0;
while (! DHT11_DQ_IN&&RETRY<100)//dht11 will pull up again after low 40~80us
{
retry++;
Systickdelay (1);
}
if (retry>=100) return 1;
return 0;
}
3. How to read the high and low level of a bit? The premise is that there is already a response signal, so that reading the data is interesting.
A bit of the high and low level is based on the bus on the length of time to determine the height, plainly, is the low level once the end, and then over 28us, the bus level is high, the level of the bit is high, the bus level is low, the level of this bit is low level. So we just wait for the low end of the bus, after 28us and then before 70us to read the level on the bus, we can know the level of the specific bit is how much. But we can't wait indefinitely for the low end, if the device fails or other reasons, the bus is always low, so the program will die in the waiting, so we have to add a wait time limit, over the waiting time, we give up this wait. In order to make the high and low levels on the bus not affect the next read data, we also added a time-limit wait.
/* read a bit from DHT11, return value: 1/0.
Each bit of data starts at a low order of 50US, and the width of the high level determines 0 or 1 of the bit data
When the high state is at 26~28us, the data bit is 0, and the high state at 70US indicates that the data bit is 1.
Dht11_dq_in is to read the input level of the corresponding port PIN, which is configured as pull-down input mode before reading
*/
U8 dht11_read_bit (void)
{
U8 retry=0;
while (DHT11_DQ_IN&&RETRY<100)//Read High, the wait becomes low,/*&& and < priority is < higher than &&*/
{
retry++;
Systickdelay (1);
}
retry=0;
while (! DHT11_DQ_IN&&RETRY<100)//Read low, wait for high level
{
retry++;
Systickdelay (1);
}
Systickdelay (40);//Wait 40us
if (dht11_dq_in) return 1;
else return 0;
}
Read a byte from DHT11
Return value: The data read
U8 Dht11_read_byte (void)
{
U8 I,dat;
dat=0;
for (i=0;i<8;i++)
{
dat<<=1;
Dat|=dht11_read_bit ();
}
return dat;
}
Read data once from DHT11
Temp: Temperature Value (range: 0~50°)
Humi: Humidity Value (range: 20%~90%)
Return value: 0, normal; 1, read failed
U8 Dht11_read_data (U8 *temp,u8 *humi)
{
U8 Buf[5];
U8 I;
Dht11_rst ();
if (Dht11_check () ==0)
{
for (i=0;i<5;i++)//Read 40-bit data
{
Buf[i]=dht11_read_byte ();
}
if ((Buf[0]+buf[1]+buf[2]+buf[3]) ==buf[4])
{
*HUMI=BUF[0];
*TEMP=BUF[2];
}
}else return 1;
return 0;
}
DHT11 Temperature and humidity sensor