Adafruit_HDC1000.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*!
  2. * @file Adafruit_HDC1000.cpp
  3. *
  4. * @mainpage Adafruit HDC1000 Library
  5. *
  6. * @section intro_sec Introduction
  7. *
  8. * This is a library for the HDC1000 Humidity & Temp Sensor
  9. *
  10. * Designed specifically to work with the HDC1000 sensor from Adafruit
  11. * ----> https://www.adafruit.com/products/2635
  12. *
  13. * These sensors use I2C to communicate, 2 pins are required to
  14. * interface
  15. * Adafruit invests time and resources providing this open source code,
  16. * please support Adafruit and open-source hardware by purchasing
  17. * products from Adafruit!
  18. *
  19. * @section author Author
  20. *
  21. * Written by Limor Fried/Ladyada for Adafruit Industries.
  22. *
  23. * @section license License
  24. *
  25. * BSD license, all text above must be included in any redistribution
  26. */
  27. #include "Adafruit_HDC1000.h"
  28. Adafruit_HDC1000::Adafruit_HDC1000() {}
  29. /*!
  30. * @brief Starts I2C connection
  31. * @param addr I2C address of the HDC1000
  32. * @param wire The TwoWire master, defaults to &Wire
  33. * @return Returns true if successful
  34. */
  35. bool Adafruit_HDC1000::begin(uint8_t addr, TwoWire *wire) {
  36. if (i2c_dev) {
  37. delete i2c_dev; // remove old interface
  38. }
  39. i2c_dev = new Adafruit_I2CDevice(addr, wire);
  40. if (!i2c_dev->begin()) {
  41. return false;
  42. }
  43. reset();
  44. if (read16(HDC1000_MANUFID) != 0x5449)
  45. return false;
  46. uint16_t device_id = read16(HDC1000_DEVICEID);
  47. if ((device_id != 0x1000) && (device_id != 0x1050))
  48. return false;
  49. return true;
  50. }
  51. /*!
  52. * @brief Soft resets the HDC1000 over I2C
  53. */
  54. void Adafruit_HDC1000::reset(void) {
  55. // reset, and select 14 bit temp & humidity
  56. uint16_t config = HDC1000_CONFIG_RST | HDC1000_CONFIG_MODE |
  57. HDC1000_CONFIG_TRES_14 | HDC1000_CONFIG_HRES_14;
  58. writeConfig(config);
  59. }
  60. /*!
  61. * @brief Gets the temperature over I2C from the HDC1000
  62. * @return Returns the temperature
  63. */
  64. float Adafruit_HDC1000::readTemperature(void) {
  65. float temp = (read32(HDC1000_TEMP, 20) >> 16);
  66. temp /= 65536;
  67. temp *= 165;
  68. temp -= 40;
  69. return temp;
  70. }
  71. /*!
  72. * @brief Gets the humidity over I2C from the HDC1000
  73. * @return Returns the humidity
  74. */
  75. float Adafruit_HDC1000::readHumidity(void) {
  76. float hum = (read32(HDC1000_TEMP, 20) & 0xFFFF);
  77. hum /= 65536;
  78. hum *= 100;
  79. return hum;
  80. }
  81. /*!
  82. * @brief Resets, heats up, selects temperature and humidity,
  83. * then takes 1000 readings and tosses
  84. */
  85. void Adafruit_HDC1000::drySensor(void) {
  86. uint16_t origConfig = read16(0x2);
  87. // reset, heat up, and select 14 bit temp & humidity
  88. uint16_t newConfig = HDC1000_CONFIG_RST | HDC1000_CONFIG_HEAT |
  89. HDC1000_CONFIG_MODE | HDC1000_CONFIG_TRES_14 |
  90. HDC1000_CONFIG_HRES_14;
  91. writeConfig(newConfig);
  92. delay(15);
  93. // take 1000 readings & toss
  94. for (int i = 0; i < 1000; i++) {
  95. read32(HDC1000_TEMP, 20);
  96. delay(1);
  97. }
  98. origConfig |= HDC1000_CONFIG_RST;
  99. writeConfig(origConfig);
  100. delay(15);
  101. }
  102. /*!
  103. * @brief Writes config to HDC1000
  104. * @param config Configuration settings to be written
  105. */
  106. void Adafruit_HDC1000::writeConfig(uint16_t config) {
  107. Adafruit_BusIO_Register reg16 =
  108. Adafruit_BusIO_Register(i2c_dev, 0x02, 2, MSBFIRST);
  109. reg16.write(config);
  110. delay(15);
  111. }
  112. /*********************************************************************/
  113. /*!
  114. * @brief Reads 16 bits
  115. * @param addr I2C register address
  116. * @param delayms Delay after write and before read
  117. * @return Returns what was read
  118. */
  119. uint16_t Adafruit_HDC1000::read16(uint8_t addr, uint8_t delayms) {
  120. uint8_t buff[2];
  121. uint16_t val = 0;
  122. buff[0] = addr;
  123. i2c_dev->write(buff, 1);
  124. delay(delayms);
  125. i2c_dev->read(buff, 2);
  126. val = buff[0];
  127. val <<= 8;
  128. val |= buff[1];
  129. return val;
  130. }
  131. /*!
  132. * @brief Reads 32 bits
  133. * @param addr I2C register address
  134. * @param delayms Delay after write and before read
  135. * @return Returns what was read
  136. */
  137. uint32_t Adafruit_HDC1000::read32(uint8_t addr, uint8_t delayms) {
  138. uint8_t buff[4];
  139. uint32_t val = 0;
  140. buff[0] = addr;
  141. i2c_dev->write(buff, 1);
  142. delay(delayms);
  143. i2c_dev->read(buff, 4);
  144. val = buff[0];
  145. val <<= 8;
  146. val |= buff[1];
  147. val <<= 8;
  148. val |= buff[2];
  149. val <<= 8;
  150. val |= buff[3];
  151. return val;
  152. }