找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3780|回复: 0
打印 上一主题 下一主题
收起左侧

arduino温湿度传感器DHT11, 21, 22, 33 and 44库文件 MEGA2560芯片

[复制链接]
跳转到指定楼层
楼主
The DHT11, 21, 22, 33 and 44 are relative inexpensive sensors for measuring temperature and humidity.

This library can be used for reading both values from these DHT sensors.
The DHT11 only returns integers (e.g. 20) and does not support negative values.
The others are quite similar and provide one decimal digit (e.g. 20.2)
The hardware pins of the sensors and handshake are identical so ideal to combine in one lib.

The library (0.1.13 version) is confirmed to work on:

UNO (tested myself)
2009 (tested myself)
MEGA2560
DUE
attiny85 @8MHz
Digistump Digix @ 84 MHz

More information - http://playground.arduino.cc//Main/DHTLib -

Notes:
version 0.1.13 is the last stable version for both AVR and ARM

version 0.1.14 and up are not compatible anymore with pre 1.0 Arduino
version 0.1.14 and up have breaking changes wrt ARM based arduino's e.g DUE.
version 0.1.15 is stable version for AVR only
version 0.1.16 and 0.1.17 have breaking changes for DHT11
version 0.1.18 works again for DHT11 (avr only)
version 0.1.19 fixed masking bug DHT11 (avr only)
version 0.1.20 Reduce footprint (34 bytes) by using int8_t as error codes. (thanks to chaveiro)



单片机源程序如下:
  1. //
  2. //    FILE: dht.cpp
  3. //  AUTHOR: Rob Tillaart
  4. // VERSION: 0.1.20
  5. // PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
  6. //     URL: http://arduino.cc/playground/Main/DHTLib
  7. //
  8. // HISTORY:
  9. // 0.1.20 Reduce footprint (34 bytes) by using int8_t as error codes.
  10. //        (thanks to chaveiro)
  11. // 0.1.19 masking error for DHT11 - FIXED (thanks Richard for noticing)
  12. // 0.1.18 version 1.16/17 broke the DHT11 - FIXED
  13. // 0.1.17 replaced micros() with adaptive loopcount
  14. //        removed DHTLIB_INVALID_VALUE
  15. //        added  DHTLIB_ERROR_CONNECT
  16. //        added  DHTLIB_ERROR_ACK_L  DHTLIB_ERROR_ACK_H
  17. // 0.1.16 masking unused bits (less errors); refactored bits[]
  18. // 0.1.15 reduced # micros calls 2->1 in inner loop.
  19. // 0.1.14 replace digital read with faster (~3x) code
  20. //        => more robust low MHz machines.
  21. //
  22. // 0.1.13 fix negative temperature
  23. // 0.1.12 support DHT33 and DHT44 initial version
  24. // 0.1.11 renamed DHTLIB_TIMEOUT
  25. // 0.1.10 optimized faster WAKEUP + TIMEOUT
  26. // 0.1.09 optimize size: timeout check + use of mask
  27. // 0.1.08 added formula for timeout based upon clockspeed
  28. // 0.1.07 added support for DHT21
  29. // 0.1.06 minimize footprint (2012-12-27)
  30. // 0.1.05 fixed negative temperature bug (thanks to Roseman)
  31. // 0.1.04 improved readability of code using DHTLIB_OK in code
  32. // 0.1.03 added error values for temp and humidity when read failed
  33. // 0.1.02 added error codes
  34. // 0.1.01 added support for Arduino 1.0, fixed typos (31/12/2011)
  35. // 0.1.00 by Rob Tillaart (01/04/2011)
  36. //
  37. // inspired by DHT11 library
  38. //
  39. // Released to the public domain
  40. //

  41. #include "dht.h"

  42. /////////////////////////////////////////////////////
  43. //
  44. // PUBLIC
  45. //

  46. int8_t dht::read11(uint8_t pin)
  47. {
  48.     // READ VALUES
  49.     int8_t result = _readSensor(pin, DHTLIB_DHT11_WAKEUP, DHTLIB_DHT11_LEADING_ZEROS);

  50.     // these bits are always zero, masking them reduces errors.
  51.     bits[0] &= 0x7F;
  52.     bits[2] &= 0x7F;

  53.     // CONVERT AND STORE
  54.     humidity    = bits[0];  // bits[1] == 0;
  55.     temperature = bits[2];  // bits[3] == 0;

  56.     // TEST CHECKSUM
  57.     // bits[1] && bits[3] both 0
  58.     uint8_t sum = bits[0] + bits[2];
  59.     if (bits[4] != sum)
  60.     {
  61.         return DHTLIB_ERROR_CHECKSUM;
  62.     }
  63.     return result;
  64. }

  65. int8_t dht::read(uint8_t pin)
  66. {
  67.     // READ VALUES
  68.     int8_t result = _readSensor(pin, DHTLIB_DHT_WAKEUP, DHTLIB_DHT_LEADING_ZEROS);

  69.     // these bits are always zero, masking them reduces errors.
  70.     bits[0] &= 0x03;
  71.     bits[2] &= 0x83;

  72.     // CONVERT AND STORE
  73.     humidity = word(bits[0], bits[1]) * 0.1;
  74.     temperature = word(bits[2] & 0x7F, bits[3]) * 0.1;
  75.     if (bits[2] & 0x80)  // negative temperature
  76.     {
  77.         temperature = -temperature;
  78.     }

  79.     // TEST CHECKSUM
  80.     uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
  81.     if (bits[4] != sum)
  82.     {
  83.         return DHTLIB_ERROR_CHECKSUM;
  84.     }
  85.     return result;
  86. }

  87. /////////////////////////////////////////////////////
  88. //
  89. // PRIVATE
  90. //

  91. int8_t dht::_readSensor(uint8_t pin, uint8_t wakeupDelay, uint8_t leadingZeroBits)
  92. {
  93.     // INIT BUFFERVAR TO RECEIVE DATA
  94.     uint8_t mask = 128;
  95.     uint8_t idx = 0;

  96.     uint8_t data = 0;
  97.     uint8_t state = LOW;
  98.     uint8_t pstate = LOW;
  99.     uint16_t zeroLoop = DHTLIB_TIMEOUT;
  100.     uint16_t delta = 0;

  101.     leadingZeroBits = 40 - leadingZeroBits; // reverse counting...

  102.     // replace digitalRead() with Direct Port Reads.
  103.     // reduces footprint ~100 bytes => portability issue?
  104.     // direct port read is about 3x faster
  105.     uint8_t bit = digitalPinToBitMask(pin);
  106.     uint8_t port = digitalPinToPort(pin);
  107.     volatile uint8_t *PIR = portInputRegister(port);

  108.     // REQUEST SAMPLE
  109.     pinMode(pin, OUTPUT);
  110.     digitalWrite(pin, LOW); // T-be
  111.     delay(wakeupDelay);
  112.     digitalWrite(pin, HIGH); // T-go
  113.     pinMode(pin, INPUT);

  114.     uint16_t loopCount = DHTLIB_TIMEOUT * 2;  // 200uSec max
  115.     // while(digitalRead(pin) == HIGH)
  116.     while ((*PIR & bit) != LOW )
  117.     {
  118.         if (--loopCount == 0) return DHTLIB_ERROR_CONNECT;
  119.     }

  120.     // GET ACKNOWLEDGE or TIMEOUT
  121.     loopCount = DHTLIB_TIMEOUT;
  122.     // while(digitalRead(pin) == LOW)
  123.     while ((*PIR & bit) == LOW )  // T-rel
  124.     {
  125.         if (--loopCount == 0) return DHTLIB_ERROR_ACK_L;
  126.     }

  127.     loopCount = DHTLIB_TIMEOUT;
  128.     // while(digitalRead(pin) == HIGH)
  129.     while ((*PIR & bit) != LOW )  // T-reh
  130.     {
  131.         if (--loopCount == 0) return DHTLIB_ERROR_ACK_H;
  132.     }

  133.     loopCount = DHTLIB_TIMEOUT;

  134.     // READ THE OUTPUT - 40 BITS => 5 BYTES
  135.     for (uint8_t i = 40; i != 0; )
  136.     {
  137.         // WAIT FOR FALLING EDGE
  138.         state = (*PIR & bit);
  139.         if (state == LOW && pstate != LOW)
  140.         {
  141.             if (i > leadingZeroBits) // DHT22 first 6 bits are all zero !!   DHT11 only 1
  142.             {
  143.                 zeroLoop = min(zeroLoop, loopCount);
  144.                 delta = (DHTLIB_TIMEOUT - zeroLoop)/4;
  145.             }
  146.             else if ( loopCount <= (zeroLoop - delta) ) // long -> one
  147.             {
  148.                 data |= mask;
  149.             }
  150.             mask >>= 1;
  151.             if (mask == 0)   // next byte
  152.             {
  153.                 mask = 128;
  154.                 bits[idx] = data;
  155.                 idx++;
  156.                 data = 0;
  157.             }
  158.             // next bit
  159.             --i;

  160.             // reset timeout flag
  161.             loopCount = DHTLIB_TIMEOUT;
  162.         }
  163.         pstate = state;
  164.         // Check timeout
  165.         if (--loopCount == 0)
  166.         {
  167.             return DHTLIB_ERROR_TIMEOUT;
  168.         }

  169.     }
  170.     pinMode(pin, OUTPUT);
  171.     digitalWrite(pin, HIGH);

  172.     return DHTLIB_OK;
  173. }
  174. //
  175. // END OF FILE
  176. //
复制代码


全部资料51hei下载地址:
DHT-lib.zip (10.73 KB, 下载次数: 31)


分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

Powered by 单片机教程网

快速回复 返回顶部 返回列表