找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

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

arduino dht11庫文件arduino-DHT-master下載

[復制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:516597 發(fā)表于 2019-4-19 18:04 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式


單片機源程序如下:
  1. #include "DHT.h"

  2. void DHT::setup(uint8_t pin, DHT_MODEL_t model)
  3. {
  4.   DHT::pin = pin;
  5.   DHT::model = model;
  6.   DHT::resetTimer(); // Make sure we do read the sensor in the next readSensor()

  7.   if ( model == AUTO_DETECT) {
  8.     DHT::model = DHT22;
  9.     readSensor();
  10.     if ( error == ERROR_TIMEOUT ) {
  11.       DHT::model = DHT11;
  12.       // Warning: in case we auto detect a DHT11, you should wait at least 1000 msec
  13.       // before your first read request. Otherwise you will get a time out error.
  14.     }
  15.   }
  16. }

  17. void DHT::resetTimer()
  18. {
  19.   DHT::lastReadTime = millis() - 3000;
  20. }

  21. float DHT::getHumidity()
  22. {
  23.   readSensor();
  24.   return humidity;
  25. }

  26. float DHT::getTemperature()
  27. {
  28.   readSensor();
  29.   return temperature;
  30. }

  31. #ifndef OPTIMIZE_SRAM_SIZE

  32. const char* DHT::getStatusString()
  33. {
  34.   switch ( error ) {
  35.     case DHT::ERROR_TIMEOUT:
  36.       return "TIMEOUT";

  37.     case DHT::ERROR_CHECKSUM:
  38.       return "CHECKSUM";

  39.     default:
  40.       return "OK";
  41.   }
  42. }

  43. #else

  44. // At the expense of 26 bytes of extra PROGMEM, we save 11 bytes of
  45. // SRAM by using the following method:

  46. prog_char P_OK[]       PROGMEM = "OK";
  47. prog_char P_TIMEOUT[]  PROGMEM = "TIMEOUT";
  48. prog_char P_CHECKSUM[] PROGMEM = "CHECKSUM";

  49. const char *DHT::getStatusString() {
  50.   prog_char *c;
  51.   switch ( error ) {
  52.     case DHT::ERROR_CHECKSUM:
  53.       c = P_CHECKSUM; break;

  54.     case DHT::ERROR_TIMEOUT:
  55.       c = P_TIMEOUT; break;

  56.     default:
  57.       c = P_OK; break;
  58.   }

  59.   static char buffer[9];
  60.   strcpy_P(buffer, c);

  61.   return buffer;
  62. }

  63. #endif

  64. void DHT::readSensor()
  65. {
  66.   // Make sure we don't poll the sensor too often
  67.   // - Max sample rate DHT11 is 1 Hz   (duty cicle 1000 ms)
  68.   // - Max sample rate DHT22 is 0.5 Hz (duty cicle 2000 ms)
  69.   unsigned long startTime = millis();
  70.   if ( (unsigned long)(startTime - lastReadTime) < (model == DHT11 ? 999L : 1999L) ) {
  71.     return;
  72.   }
  73.   lastReadTime = startTime;

  74.   temperature = NAN;
  75.   humidity = NAN;

  76.   // Request sample

  77.   digitalWrite(pin, LOW); // Send start signal
  78.   pinMode(pin, OUTPUT);
  79.   if ( model == DHT11 ) {
  80.     delay(18);
  81.   }
  82.   else {
  83.     // This will fail for a DHT11 - that's how we can detect such a device
  84.     delayMicroseconds(800);
  85.   }

  86.   pinMode(pin, INPUT);
  87.   digitalWrite(pin, HIGH); // Switch bus to receive data

  88.   // We're going to read 83 edges:
  89.   // - First a FALLING, RISING, and FALLING edge for the start bit
  90.   // - Then 40 bits: RISING and then a FALLING edge per bit
  91.   // To keep our code simple, we accept any HIGH or LOW reading if it's max 85 usecs long

  92.   uint16_t rawHumidity = 0;
  93.   uint16_t rawTemperature = 0;
  94.   uint16_t data = 0;

  95.   for ( int8_t i = -3 ; i < 2 * 40; i++ ) {
  96.     byte age;
  97.     startTime = micros();

  98.     do {
  99.       age = (unsigned long)(micros() - startTime);
  100.       if ( age > 90 ) {
  101.         error = ERROR_TIMEOUT;
  102.         return;
  103.       }
  104.     }
  105.     while ( digitalRead(pin) == (i & 1) ? HIGH : LOW );

  106.     if ( i >= 0 && (i & 1) ) {
  107.       // Now we are being fed our 40 bits
  108.       data <<= 1;

  109.       // A zero max 30 usecs, a one at least 68 usecs.
  110.       if ( age > 30 ) {
  111.         data |= 1; // we got a one
  112.       }
  113.     }

  114.     switch ( i ) {
  115.       case 31:
  116.         rawHumidity = data;
  117.         break;
  118.       case 63:
  119.         rawTemperature = data;
  120.         data = 0;
  121.         break;
  122.     }
  123.   }

  124.   // Verify checksum

  125.   if ( (byte)(((byte)rawHumidity) + (rawHumidity >> 8) + ((byte)rawTemperature) + (rawTemperature >> 8)) != data ) {
  126.     error = ERROR_CHECKSUM;
  127.     return;
  128.   }

  129.   // Store readings

  130.   if ( model == DHT11 ) {
  131.     humidity = rawHumidity >> 8;
  132.     temperature = rawTemperature >> 8;
  133.   }
  134.   else {
  135.     humidity = rawHumidity * 0.1;

  136.     if ( rawTemperature & 0x8000 ) {
  137.       rawTemperature = -(int16_t)(rawTemperature & 0x7FFF);
  138.     }
  139.     temperature = ((int16_t)rawTemperature) * 0.1;
  140.   }

  141.   error = ERROR_NONE;
  142. }

復制代碼

所有資料51hei提供下載:
arduino-DHT-master (1).zip (42.91 KB, 下載次數(shù): 167)


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

使用道具 舉報

沙發(fā)
ID:530524 發(fā)表于 2019-5-8 13:00 | 只看該作者
這個很有用
回復

使用道具 舉報

板凳
ID:13396 發(fā)表于 2020-12-25 19:57 | 只看該作者
謝謝分享,這個有用。
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

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

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

快速回復 返回頂部 返回列表