HDC1000.cpp 2.9 KB

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