ÍøÉÏÓкܶàDHT11µÄ²âÊÔ£¬ÊÔÁËN¸ö³ÌÐò£¬×ÜÊDz»µÃÒªÁ죬¸÷ÖÖ±¨´í£¬×îºóÖÕÓÚÕÒµ½Ò»Ì׿ÉÓõĿ⡣
Ê×ÏÈÊÇDHT11.hÎļþ
#ifndef __DHT11_H__ #define __DHT11_H__ #include <Arduino.h> //DHT11 IOÉèÖà #define DHT11_DQ 2 #define DHT11_DQ_0 digitalWrite(DHT11_DQ,LOW) #define DHT11_DQ_1 digitalWrite(DHT11_DQ,HIGH) //º¯Êý»òÕß±äÁ¿ÉùÃ÷ extern void DHT11_Init(); extern unsigned char DHT11_Read_Byte(); extern void DHT11_Read(); extern unsigned char HUMI_Buffer_Int; extern unsigned char TEM_Buffer_Int; #endifÆä´ÎÊÇDHT11.cppÎļþ
#include "DHT11.h" //¶¨Òå±äÁ¿ unsigned char HUMI_Buffer_Int = 0; unsigned char TEM_Buffer_Int = 0; //**************************************************** //³õʼ»¯DHT11 //**************************************************** void DHT11_Init() { pinMode(DHT11_DQ,OUTPUT); DHT11_DQ_0; //ÀµÍ×ÜÏߣ¬·¢¿ªÊ¼Ðźţ» delay(30); //ÑÓʱҪ´óÓÚ 18ms£¬ÒÔ±ã DHT11 Äܼì²âµ½¿ªÊ¼Ðźţ» DHT11_DQ_1; delayMicroseconds(40); //µÈ´ý DHT11 ÏìÓ¦£» pinMode(DHT11_DQ,INPUT); while(digitalRead(DHT11_DQ) == HIGH); delayMicroseconds(80); //DHT11 ·¢³öÏìÓ¦£¬ÀµÍ×ÜÏß 80us£» if(digitalRead(DHT11_DQ) == LOW); delayMicroseconds(80); //DHT11 À¸ß×ÜÏß 80us ºó¿ªÊ¼·¢ËÍÊý¾Ý£» } //**************************************************** //¶ÁÒ»¸ö×Ö½ÚDHT11Êý¾Ý //**************************************************** unsigned char DHT11_Read_Byte() { unsigned char i,dat = 0; unsigned int j; pinMode(DHT11_DQ,INPUT); for( i=0; i<8; i++) { if(digitalRead(DHT11_DQ) == LOW) { while(digitalRead(DHT11_DQ) == LOW); //µÈ´ý 50us£» delayMicroseconds(30); //ÅжϸߵçƽµÄ³ÖÐøʱ¼ä£¬ÒÔÅж¨Êý¾ÝÊÇ¡®0¡¯»¹ÊÇ¡®1¡¯£» if(digitalRead(DHT11_DQ) == HIGH) dat |= (1<<(7-i)); //¸ßλÔÚÇ°£¬µÍλÔÚºó£» while(digitalRead(DHT11_DQ) == HIGH); //Êý¾Ý¡®1¡¯£¬µÈ´ýÏÂһλµÄ½ÓÊÕ£» } } return dat; } //**************************************************** //¶ÁÈ¡ÎÂʪ¶ÈÖµ£¬´æ·ÅÔÚTEM_BufferºÍHUMI_Buffer //**************************************************** void DHT11_Read() { DHT11_Init(); HUMI_Buffer_Int = DHT11_Read_Byte(); //¶Áȡʪ¶ÈµÄÕûÊýÖµ DHT11_Read_Byte(); //¶Áȡʪ¶ÈµÄСÊýÖµ TEM_Buffer_Int = DHT11_Read_Byte(); //¶ÁȡζȵÄÕûÊýÖµ DHT11_Read_Byte(); //¶ÁȡζȵÄСÊýÖµ DHT11_Read_Byte(); //¶ÁȡУÑéºÍ delayMicroseconds(50); //DHT11ÀµÍ×ÜÏß50us pinMode(DHT11_DQ,OUTPUT); DHT11_DQ_1; //ÊÍ·Å×ÜÏß }×îºóÊÇÖ÷³ÌÐòÎļþ
#include <Arduino.h> #include "DHT11.h" void setup() //Arduino³ÌÐò³õʼ»¯³ÌÐò·ÅÔÚÕâÀֻÔÚ¿ª»úʱºòÔËÐÐÒ»´Î { Serial.begin(9600); //ÉèÖÃͨѶµÄ²¨ÌØÂÊΪ9600 DHT11_Read(); //¶ÁÈ¡ÎÂʪ¶ÈÖµ delay(200); //µÈ´ý´«¸ÐÆ÷Îȶ¨ } void loop() //Arduino³ÌÐòµÄÖ÷³ÌÐò²¿·Ö£¬Ñ»·ÔËÐÐÄÚ²¿³ÌÐò { DHT11_Read(); //¶ÁÈ¡ÎÂʪ¶ÈÖµ Serial.print("HUMI = "); Serial.print(HUMI_Buffer_Int); Serial.println(" %RH"); Serial.print("TMEP = "); Serial.print(TEM_Buffer_Int); Serial.println(" C"); delay(1000); //ÑÓʱ1s }Èý¸öÎļþ±£´æÔÚͬһ¸öÎļþ¼Ð¼´¿É¡£
¶¥ 0 ²È 0
ÎÒµÄͬÀàÎÄÕÂ
²ÂÄãÔÚÕÒ
²é¿´ÆÀÂÛ
* ÒÔÉÏÓû§ÑÔÂÛÖ»´ú±íÆä¸öÈ˹۵㣬²»´ú±íCSDNÍøÕ¾µÄ¹Ûµã»òÁ¢³¡
¸öÈË×ÊÁÏ
u010435562
»ý·Ö£º840
²©Ö÷ÃèÊö
ÎÄÕÂËÑË÷
ÎÄÕ·ÖÀà
ÎÄÕ´浵
ÔĶÁÅÅÐÐ
¡¡