单片机教程网

电脑版
提示:原网页已由神马搜索转码, 内容由www.51hei.com提供.
查看:8130|回复:2
打印上一主题下一主题

arduino dht11库文件arduino-DHT-master下载

[复制链接]
跳转到指定楼层
楼主
ID:516597发表于 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, 下载次数: 165)


沙发
ID:530524发表于 2019-5-8 13:00|只看该作者
这个很有用
板凳
ID:13396发表于 2020-12-25 19:57|只看该作者
谢谢分享,这个有用。

手机版|小黑屋|51黑电子论坛|51黑电子论坛6群QQ管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网