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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4485|回復: 0
收起左側

arduino溫濕度傳感器DHT11, 21, 22, 33 and 44庫文件 MEGA2560芯片

[復制鏈接]
ID:312559 發表于 2018-4-20 17:22 | 顯示全部樓層 |閱讀模式
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)

0.jpg

單片機源程序如下:
  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)


回復

使用道具 舉報

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

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 久久精彩 | 午夜在线影院 | 亚洲精品www久久久久久广东 | 国产精品久久久亚洲 | 亚洲高清在线观看 | 精品二区视频 | 色爱综合网 | 一级黄色片日本 | 精品产国自在拍 | 婷婷成人在线 | 午夜影院在线观看免费 | 在线a视频网站 | 成年人在线观看 | 韩国av电影网 | 日韩久久久久久 | 国产精品a免费一区久久电影 | 91国产精品| 国产精品18hdxxxⅹ在线 | 一区二区av| 91精品国产91久久久久久丝袜 | 日韩午夜电影在线观看 | 97国产精品 | 不卡一区 | 97久久精品午夜一区二区 | 亚洲大片一区 | 久久精品一区二区视频 | 成人免费激情视频 | 日韩中文视频 | 亚洲久草| 国产精品黄视频 | 自拍偷拍第1页 | 亚洲一区二区三区视频 | 亚洲人人舔人人 | 日韩在线一区二区三区 | 欧美日韩91| 一区二区福利视频 | 一区二区三区精品在线 | 亚洲成人一区 | 91精品国产日韩91久久久久久 | 在线一区视频 | 国产一区欧美 |