DHT.h 2.6 KB

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