DHT.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. #ifndef DHT_H
  2. #define DHT_H
  3. #if ARDUINO >= 100
  4. #include "Arduino.h"
  5. #else
  6. #include "WProgram.h"
  7. #endif
  8. #include <Wire.h>
  9. // 8 MHz(ish) AVR ---------------------------------------------------------
  10. #if (F_CPU >= 7400000UL) && (F_CPU <= 9500000UL)
  11. #define COUNT 1
  12. // 16 MHz(ish) AVR --------------------------------------------------------
  13. #elif (F_CPU == 10000000L)
  14. #define COUNT 1
  15. #elif (F_CPU >= 15400000UL) && (F_CPU <= 19000000L)
  16. #define COUNT 2
  17. #elif (F_CPU == 20000000L)
  18. #define COUNT 2
  19. #elif (F_CPU == 40000000L)
  20. #define COUNT 4
  21. // 48MHz SAMD21J18A (Sodaq Explorer)
  22. #elif (F_CPU == 48000000UL)
  23. #define COUNT 5
  24. // 64MHz NRF52840
  25. #elif (F_CPU == 64000000UL)
  26. #define COUNT 6
  27. // 168MHz STM32F405 STM32F407
  28. #elif (F_CPU == 168000000L)
  29. #define COUNT 16
  30. #elif (F_CPU == 80000000L)
  31. #define COUNT 8
  32. #elif (F_CPU == 160000000L)
  33. #define COUNT 16
  34. #else
  35. #define COUNT 24
  36. //#error "CPU SPEED NOT SUPPORTED"
  37. #endif
  38. //RP2040
  39. #if defined(ARDUINO_ARCH_RP2040)
  40. #undef COUNT
  41. #define COUNT 10
  42. #endif
  43. /* DHT library
  44. MIT license
  45. written by Adafruit Industries
  46. */
  47. // how many timing transitions we need to keep track of. 2 * number bits + extra
  48. #define MAXTIMINGS 85
  49. #define SERIALPRINT Serial
  50. #define DEFAULT_IIC_ADDR 0x38
  51. #define RESET_REG_ADDR 0xba
  52. #define HUMIDITY_INDEX 0
  53. #define TEMPRATURE_INDEX 1
  54. #define DHT11 11
  55. #define DHT22 22
  56. #define DHT21 21
  57. #define AM2301 21
  58. #define DHT10 10
  59. #define DHT20 10
  60. class DHT {
  61. private:
  62. uint8_t data[6];
  63. uint8_t _pin, _type, _count;
  64. boolean read(void);
  65. unsigned long _lastreadtime;
  66. boolean firstreading;
  67. public:
  68. DHT(uint8_t pin, uint8_t type, uint8_t count = COUNT);
  69. DHT(uint8_t type);
  70. void begin(void);
  71. float readTemperature(bool S = false);
  72. float convertCtoF(float);
  73. float readHumidity(void);
  74. /** Common interface to get temp&humi value.support all DHT device.
  75. @return 0 for calibrated failed,1 for succeed.
  76. **/
  77. int readTempAndHumidity(float* data);
  78. // DHT10 digital interfaces(i2c),onlu for DHT10 .
  79. int i2cReadByte(uint8_t& byte);
  80. int i2cReadBytes(uint8_t* bytes, uint32_t len);
  81. int i2cWriteBytes(uint8_t* bytes, uint32_t len);
  82. int i2cWriteByte(uint8_t byte);
  83. /** Reset sensor.
  84. @return 0 for calibrated failed,1 for succeed.
  85. **/
  86. int DHT10Reset(void);
  87. /** Read status register.check the calibration flag - bit[3]: 1- calibrated ok ,0 - Not calibrated.
  88. @return 0 for calibrated failed,1 for succeed.
  89. **/
  90. int DHT10ReadStatus(void);
  91. /** Init sensor,send 0x08,0x00 to register 0xe1.
  92. @ return : 0 if success, non-zero if failed.
  93. **/
  94. int setSystemCfg(void);
  95. /** Read temp & humi result buf from sensor.
  96. total 6 bytes,the first byte for status register,other 5 bytes for temp & humidity data.
  97. @ return : 0 if success, non-zero if failed.
  98. **/
  99. int readTargetData(uint32_t* data);
  100. /** DHT10 Init function.
  101. Reset sensor and wait for calibration complete.
  102. @ return : 0 if success, non-zero if failed.
  103. **/
  104. int DHT10Init(void);
  105. };
  106. #endif