您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 
 

151 行
3.4 KiB

  1. /*
  2. * htu21d.c:
  3. * Extend wiringPi with the HTU21D I2C humidity and Temperature
  4. * sensor. This is used in the Pi Weather station.
  5. * Copyright (c) 2016 Gordon Henderson
  6. ***********************************************************************
  7. * This file is part of wiringPi:
  8. * https://projects.drogon.net/raspberry-pi/wiringpi/
  9. *
  10. * wiringPi is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Lesser General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * wiringPi is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Lesser General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Lesser General Public
  21. * License along with wiringPi.
  22. * If not, see <http://www.gnu.org/licenses/>.
  23. ***********************************************************************
  24. */
  25. #include <unistd.h>
  26. #include <stdint.h>
  27. #include <stdio.h>
  28. #include <math.h>
  29. #include "wiringPi.h"
  30. #include "wiringPiI2C.h"
  31. #include "htu21d.h"
  32. #define DEBUG
  33. #undef FAKE_SENSOR
  34. #define I2C_ADDRESS 0x40
  35. int checksum (UNU uint8_t data [4])
  36. {
  37. return TRUE ;
  38. }
  39. /*
  40. * myAnalogRead:
  41. *********************************************************************************
  42. */
  43. static int myAnalogRead (struct wiringPiNodeStruct *node, int pin)
  44. {
  45. int chan = pin - node->pinBase ;
  46. int fd = node->fd ;
  47. uint8_t data [4] ;
  48. uint32_t sTemp, sHumid ;
  49. double fTemp, fHumid ;
  50. int cTemp, cHumid ;
  51. /**/ if (chan == 0) // Read Temperature
  52. {
  53. // Send read temperature command:
  54. data [0] = 0xF3 ;
  55. if (write (fd, data, 1) != 1)
  56. return -9999 ;
  57. // Wait then read the data
  58. delay (50) ;
  59. if (read (fd, data, 3) != 3)
  60. return -9998 ;
  61. if (!checksum (data))
  62. return -9997 ;
  63. // Do the calculation
  64. sTemp = (data [0] << 8) | data [1] ;
  65. fTemp = -48.85 + 175.72 * (double)sTemp / 63356.0 ;
  66. cTemp = (int)rint (((100.0 * fTemp) + 0.5) / 10.0) ;
  67. return cTemp ;
  68. }
  69. else if (chan == 1) // humidity
  70. {
  71. // Send read humidity command:
  72. data [0] = 0xF5 ;
  73. if (write (fd, data, 1) != 1)
  74. return -9999 ;
  75. // Wait then read the data
  76. delay (50) ;
  77. if (read (fd, data, 3) != 3)
  78. return -9998 ;
  79. if (!checksum (data))
  80. return -9997 ;
  81. sHumid = (data [0] << 8) | data [1] ;
  82. fHumid = -6.0 + 125.0 * (double)sHumid / 65536.0 ;
  83. cHumid = (int)rint (((100.0 * fHumid) + 0.5) / 10.0) ;
  84. return cHumid ;
  85. }
  86. else
  87. return -9999 ;
  88. }
  89. /*
  90. * htu21dSetup:
  91. * Create a new instance of a HTU21D I2C GPIO interface.
  92. * This chip has a fixed I2C address, so we are not providing any
  93. * allowance to change this.
  94. *********************************************************************************
  95. */
  96. int htu21dSetup (const int pinBase)
  97. {
  98. int fd ;
  99. struct wiringPiNodeStruct *node ;
  100. uint8_t data ;
  101. int status ;
  102. if ((fd = wiringPiI2CSetup (I2C_ADDRESS)) < 0)
  103. return FALSE ;
  104. node = wiringPiNewNode (pinBase, 2) ;
  105. node->fd = fd ;
  106. node->analogRead = myAnalogRead ;
  107. // Send a reset code to it:
  108. data = 0xFE ;
  109. if (write (fd, &data, 1) != 1)
  110. return FALSE ;
  111. delay (15) ;
  112. // Read the status register to check it's really there
  113. status = wiringPiI2CReadReg8 (fd, 0xE7) ;
  114. return (status == 0x02) ? TRUE : FALSE ;
  115. }