久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4496|回復: 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 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 神马九九| 久久蜜桃精品 | 久久综合一区二区三区 | 亚洲最新在线 | 国产视频1区 | 日韩欧美二区 | 美女网站视频免费黄 | 精品一区二区三区91 | 久久久久综合 | 亚洲v区 | 一本一道久久a久久精品综合蜜臀 | 福利av在线| 在线免费观看成年人视频 | 一区二区三区四区av | 四虎影院久久 | 岛国毛片在线观看 | 一级全黄视频 | 国产精品久久久久久久久久 | 欧美精品一区二区三区在线播放 | 久久久一区二区三区 | 亚洲精品www | 亚洲一区国产精品 | 国产在线视频一区 | 午夜男人天堂 | 红色av社区| www狠狠爱com | 国产一区二区在线播放 | 三级黄色网址 | 亚洲精品日韩一区二区电影 | 久久6视频 | 国产色网站 | 国产不卡在线观看 | 亚洲电影一区二区三区 | 亚洲精品久久久一区二区三区 | 精品国模一区二区三区欧美 | 亚洲在线一区二区 | 一级片片 | 日韩高清一区 | 在线观看黄色电影 | 国产精品自拍视频 | 日韩不卡在线 |