找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 3510|回復(fù): 2
打印 上一主題 下一主題
收起左側(cè)

DHT11溫濕度arduino程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
DHT11溫濕度檢測(cè)程序

arduino單片機(jī)源程序如下:
  1. #include "dht.h"  
  2.   
  3. #define TIMEOUT 10000  
  4. #define pin     D2
  5. // return values:  
  6. //  0 : OK  
  7. // -1 : checksum error  
  8. // -2 : timeout  
  9. int dht::read11()  
  10. {  
  11.     // READ VALUES  
  12.    int rv = read();  
  13.     if (rv != 0) return rv;  

  14.     // CONVERT AND STORE  
  15.     humidity    = bits[0];  // bit[1] == 0;  
  16.     temperature = bits[2];  // bits[3] == 0;  
  17.   
  18.     // TEST CHECKSUM  
  19.    uint8_t sum = bits[0] + bits[2]; // bits[1] && bits[3] both 0  
  20.     if (bits[4] != sum) return -1;  

  21.     return 0;  
  22. }  

  23. // return values:  
  24. //  0 : OK  
  25. // -1 : checksum error  
  26. // -2 : timeout  
  27. int dht::read22()  
  28. {  
  29.     // READ VALUES  
  30.     int rv = read();  
  31.     if (rv != 0) return rv;  
  32.   
  33.     // CONVERT AND STORE  
  34.     humidity    = word(bits[0], bits[1]) * 0.1;  
  35.   
  36.     int sign = 1;  
  37.     if (bits[2] & 0x80) // negative temperature  
  38.     {  
  39.         bits[2] = bits[2] & 0x7F;  
  40.         sign = -1;  
  41.     }  
  42.     temperature = sign * word(bits[2], bits[3]) * 0.1;  
  43.   
  44.   // TEST CHECKSUM  
  45.     uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];  
  46.     if (bits[4] != sum) return -1;  

  47.     return 0;  
  48. }  

  49. // return values:  
  50. //  0 : OK  
  51. // -2 : timeout  
  52. int dht::read()  
  53. {  
  54.     // INIT BUFFERVAR TO RECEIVE DATA  
  55.     uint8_t cnt = 7;  
  56.     uint8_t idx = 0;  

  57.    // EMPTY BUFFER  
  58.    for (int i=0; i< 5; i++) bits[i] = 0;  
  59.   
  60.     // REQUEST SAMPLE  
  61.    pinMode(pin, OUTPUT);  
  62.    digitalWrite(pin, LOW);  
  63.    delay(20);  
  64.    digitalWrite(pin, HIGH);  
  65.    delayMicroseconds(40);  
  66.    pinMode(pin, INPUT);  
  67.   
  68.    // GET ACKNOWLEDGE or TIMEOUT  
  69.    unsigned int loopCnt = TIMEOUT;  
  70.    while(digitalRead(pin) == LOW)  
  71.       if (loopCnt-- == 0) return -2;  
  72.   
  73.     loopCnt = TIMEOUT;  
  74.     while(digitalRead(pin) == HIGH)  
  75.        if (loopCnt-- == 0) return -2;  
  76.   
  77.     // READ THE OUTPUT - 40 BITS => 5 BYTES  
  78.     for (int i=0; i<40; i++)  
  79.     {  
  80.         loopCnt = TIMEOUT;  
  81.         while(digitalRead(pin) == LOW)  
  82.             if (loopCnt-- == 0) return -2;  
  83.         unsigned long t = micros();  
  84.   
  85.         loopCnt = TIMEOUT;  
  86.         while(digitalRead(pin) == HIGH)  
  87. ……………………

  88. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
temperature.rar (1.77 KB, 下載次數(shù): 24)


分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:234938 發(fā)表于 2018-4-12 15:41 | 只看該作者
看起來挺高大上的,過來學(xué)習(xí)一下。
回復(fù)

使用道具 舉報(bào)

板凳
ID:315637 發(fā)表于 2018-4-24 20:00 | 只看該作者
樓主能不能單獨(dú)發(fā)一份???thanks。
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表