Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.
 
 
 
 
 

166 linhas
3.9 KiB

  1. /*
  2. * maxdetect.c:
  3. * Driver for the MaxDetect series sensors
  4. *
  5. * Copyright (c) 2013 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 published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (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 License
  21. * along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
  22. ***********************************************************************
  23. */
  24. //#include <stdio.h>
  25. //#include <stdlib.h>
  26. //#include <unistd.h>
  27. #include <wiringPi.h>
  28. #include "maxdetect.h"
  29. #ifndef TRUE
  30. # define TRUE (1==1)
  31. # define FALSE (1==2)
  32. #endif
  33. /*
  34. * maxDetectLowHighWait:
  35. * Wait for a transition from high to low on the bus
  36. *********************************************************************************
  37. */
  38. static void maxDetectLowHighWait (const int pin)
  39. {
  40. unsigned int timeOut = millis () + 2000 ;
  41. while (digitalRead (pin) == HIGH)
  42. if (millis () > timeOut)
  43. return ;
  44. while (digitalRead (pin) == LOW)
  45. if (millis () > timeOut)
  46. return ;
  47. }
  48. /*
  49. * maxDetectClockByte:
  50. * Read in a single byte from the MaxDetect bus
  51. *********************************************************************************
  52. */
  53. static unsigned int maxDetectClockByte (const int pin)
  54. {
  55. unsigned int byte = 0 ;
  56. int bit ;
  57. for (bit = 0 ; bit < 8 ; ++bit)
  58. {
  59. maxDetectLowHighWait (pin) ;
  60. // bit starting now - we need to time it.
  61. delayMicroseconds (30) ;
  62. byte <<= 1 ;
  63. if (digitalRead (pin) == HIGH) // It's a 1
  64. byte |= 1 ;
  65. }
  66. return byte ;
  67. }
  68. /*
  69. * maxDetectRead:
  70. * Read in and return the 4 data bytes from the MaxDetect sensor.
  71. * Return TRUE/FALSE depending on the checksum validity
  72. *********************************************************************************
  73. */
  74. int maxDetectRead (const int pin, unsigned char buffer [4])
  75. {
  76. int i ;
  77. unsigned int checksum ;
  78. unsigned char localBuf [5] ;
  79. // Wake up the RHT03 by pulling the data line low, then high
  80. // Low for 10mS, high for 40uS.
  81. pinMode (pin, OUTPUT) ;
  82. digitalWrite (pin, 0) ; delay (10) ;
  83. digitalWrite (pin, 1) ; delayMicroseconds (40) ;
  84. pinMode (pin, INPUT) ;
  85. // Now wait for sensor to pull pin low
  86. maxDetectLowHighWait (pin) ;
  87. // and read in 5 bytes (40 bits)
  88. for (i = 0 ; i < 5 ; ++i)
  89. localBuf [i] = maxDetectClockByte (pin) ;
  90. checksum = 0 ;
  91. for (i = 0 ; i < 4 ; ++i)
  92. {
  93. buffer [i] = localBuf [i] ;
  94. checksum += localBuf [i] ;
  95. }
  96. checksum &= 0xFF ;
  97. return checksum == localBuf [4] ;
  98. }
  99. /*
  100. * readRHT03:
  101. * Read the Temperature & Humidity from an RHT03 sensor
  102. *********************************************************************************
  103. */
  104. int readRHT03 (const int pin, int *temp, int *rh)
  105. {
  106. static unsigned int nextTime = 0 ;
  107. static int lastTemp = 0 ;
  108. static int lastRh = 0 ;
  109. static int lastResult = TRUE ;
  110. unsigned char buffer [4] ;
  111. // Don't read more than once a second
  112. if (millis () < nextTime)
  113. {
  114. *temp = lastTemp ;
  115. *rh = lastRh ;
  116. return lastResult ;
  117. }
  118. lastResult = maxDetectRead (pin, buffer) ;
  119. if (lastResult)
  120. {
  121. *temp = lastTemp = (buffer [2] * 256 + buffer [3]) ;
  122. *rh = lastRh = (buffer [0] * 256 + buffer [1]) ;
  123. nextTime = millis () + 2000 ;
  124. return TRUE ;
  125. }
  126. else
  127. {
  128. return FALSE ;
  129. }
  130. }