I. DHT11 INTRODUCTION
DHT11 digital temperature and humidity sensor, is a calibrated digital signal output of the humidity and temperature of the complex sensor. It uses a dedicated digital module acquisition technology and temperature and humidity sensing technology to ensure that the product is highly reliable and has excellent long-term stability. The sensor includes a resistive sensing element and an NTC temperature measurement element, and is connected to a high performance 8-bit single-chip microcomputer. Therefore, the product has excellent quality, ultra-fast response, anti-jamming ability, high cost-effective advantages. Each DHT11 sensor is calibrated in a highly accurate humidity calibration chamber. The calibration coefficients are stored in OTP memory in the form of a program, and the calibration coefficients are called within the sensor during the processing of the heartbeat . Single-wire serial interface makes system integration easy and quick. Ultra-small volume, very low power consumption, signal transmission distance of up to 20 meters, making it the best choice for a variety of applications and even the most demanding applications. The product is packaged in a 4-pin single-row pin. Convenient connection, special package form can be provided according to user needs.
Two. Programming analysis
DHT11 is through the single-bus and single-chip communication, the advantage is to occupy less resources, simple programming, the specific format of sending data for 8bit humidity integer data +8bit humidity fractional data +8bi temperature integer data +8bit temperature fractional data +8bit checksum. Host first to send a start to DHT11 a new number, waiting for DHT11 to respond, and then to detect the temperature and humidity sent and transmitted, the following through a picture to understand the DHT11 transmission process.
From the figure we can see, the host to first pull down the answer line, because the idle time is high, and then pull high again, waiting for DHT11 response, that is, waiting for the data line is pulled low, when the DHT11 ready to output data, and then pull the data line low, output data to a single-chip microcomputer, Single-chip microcomputer can be based on the length of the high-level to determine whether the output of the data is 0 or 1, the final check bit checking, and thus into transmission.
Three. Procedures
The following is a procedure for DHT11 reading data
Uchar Dhtreadbyte (void)
{
Bit bit_i;
Uchar J;
Uchar dat=0;
for (j=0;j<8;j++)
{
while (! TRH); Wait for low level to end
Delay5us (); Delay Delay5us ();
Delay5us (); Delay5us (); Delay
Delay5us (); Delay5us ();
if (TRH)//detect whether the data cable is high-level or low-level
{bit_i=1; while (TRH); }
else {bit_i=0; }
dat<<=1; Save this bit shift to the DAT variable
Dat|=bit_i; }
return (DAT); }
/************** function: void Readtrh (void)---------------------------Description: Sends the read temperature and humidity through the serial port parameters: Return value: Read the byte content ********** */
void Readtrh (void)
{Uchar check; Check byte
Uchar Temhig,temlow,humihig,humilow;
trh=0; The main engine pulls down 18ms delayms (18);
trh=1; The data bus is pulled up by the pull-up resistor for the host delay of 20US
Delay5us (); Delay5us (); Delay5us (); Delay5us (); The host is set as input to determine the slave response signal
while (! TRH); Wait for the low-level end of the DHT 80US
while (TRH); Wait for the high end of the DHT 80US
Humihig = Dhtreadbyte (); 8-bit High humidity
Humilow = Dhtreadbyte (); 8 low humidity, total 0
Temhig = Dhtreadbyte (); 8-bit High temperature
Temlow = Dhtreadbyte (); 8 lower temperature, total 0
Check = Dhtreadbyte (); 8-bit checksum with a value equal to the four bytes read and the low 8-bit trh=1 of the combined sum; Release bus
if (Check==humihig + humilow + temhig + temlow)//If the received data is correct
{Send (Temhig); Send (Humihig); } }
Four. Summary
The above procedure is from the DHT11 temperature and humidity sensor read a number of concurrent to the serial value of the program, to learn this kind of sensor program, we must remember, learn to understand the timing diagram, will be able to use, need to DHT11 the PDF of the small partners, please leave contact or private chat me 0.0
DHT11 of sensor Introduction