HDC1000.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. HDC1000.h
  3. A library for HDC1000 1.0
  4. Copyright (c) 2015 seeed technology inc.
  5. Website : www.seeed.cc
  6. Author : Pillar Zuo (baozhu.zuo@seeed.cc)
  7. Create Time: April 2015
  8. Change Log :
  9. The MIT License (MIT)
  10. Permission is hereby granted, free of charge, to any person obtaining a copy
  11. of this software and associated documentation files (the "Software"), to deal
  12. in the Software without restriction, including without limitation the rights
  13. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  14. copies of the Software, and to permit persons to whom the Software is
  15. furnished to do so, subject to the following conditions:
  16. The above copyright notice and this permission notice shall be included in
  17. all copies or substantial portions of the Software.
  18. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  24. THE SOFTWARE.
  25. */
  26. #include "HDC1000.h"
  27. #include "Wire.h"
  28. #if defined(__avr__)
  29. #include <util/delay.h>
  30. #endif
  31. HDC1000::HDC1000(uint8_t address, int drdyn_pin) {
  32. _addr = address;
  33. _drdyn_pin = drdyn_pin;
  34. if (_drdyn_pin > -1) {
  35. pinMode(_drdyn_pin, INPUT);
  36. }
  37. }
  38. uint8_t HDC1000::begin(uint8_t reset, uint8_t mode, uint8_t resolution, uint8_t heater) {
  39. Wire.begin();
  40. uint8_t config = mode | resolution | heater | reset;
  41. setConfig(config);
  42. return config;
  43. }
  44. uint16_t HDC1000::readConfig(void) {
  45. setReadRegister(HDC1000_CONFIG);
  46. return read16();
  47. }
  48. void HDC1000::setReadRegister(uint8_t reg) {
  49. Wire.beginTransmission(_addr);
  50. Wire.write(reg);
  51. Wire.endTransmission();
  52. if (_drdyn_pin > -1) while (digitalRead(_drdyn_pin) == HIGH); //using DRDYn pin
  53. else {
  54. delay(20); //using 20ms delay instead
  55. }
  56. }
  57. void HDC1000::setConfig(uint8_t config) {
  58. Wire.beginTransmission(_addr);
  59. Wire.write(HDC1000_CONFIG); //accessing the configuration register
  60. Wire.write(config); //sending the config bits (MSB)
  61. Wire.write(0x00); //the last 8 bits must be zeroes
  62. Wire.endTransmission();
  63. }
  64. uint16_t HDC1000::read16() {
  65. uint8_t bytes = 2;
  66. uint16_t dest = 0;
  67. Wire.requestFrom(_addr, bytes);
  68. if (Wire.available() >= bytes) {
  69. dest = Wire.read() << 8;
  70. dest += Wire.read();
  71. }
  72. return dest;
  73. }
  74. uint16_t HDC1000::getRawTemp(void) {
  75. setReadRegister(HDC1000_TEMP);
  76. return read16();
  77. }
  78. uint16_t HDC1000::getRawHumi(void) {
  79. setReadRegister(HDC1000_HUMI);
  80. return read16();
  81. }
  82. double HDC1000::getTemp(void) {
  83. double temp = getRawTemp();
  84. return temp / 65536.0 * 165.0 - 40.0;
  85. }
  86. double HDC1000::getHumi(void) {
  87. double humi = getRawHumi();
  88. return humi / 65536.0 * 100.0;
  89. }
  90. boolean HDC1000::battery() {
  91. return (bitRead(readConfig(), 11));
  92. }