11//
22// FILE: dht.cpp
33// AUTHOR: Rob Tillaart
4- // VERSION: 0.1.09
4+ // VERSION: 0.1.10
55// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
66// URL: http://arduino.cc/playground/Main/DHTLib
77//
88// HISTORY:
9+ // 0.1.10 optimized faster WAKEUP + TIMEOUT
910// 0.1.09 optimize size: timeout check + use of mask
1011// 0.1.08 added formula for timeout based upon clockspeed
1112// 0.1.07 added support for DHT21
2425
2526#include " dht.h"
2627
27- // #define TIMEOUT 10000
28- // uint16_t for UNO, higher CPU speeds => exceed MAXINT.
29- // works for DUE
30- // 16 MHz => 10000
31- // 84 MHz => 52500
32- // 100MHz => 62500
33- #define TIMEOUT (F_CPU/1600 )
28+ // max timeout is 100usec.
29+ // For a 16Mhz proc that is max 1600 clock cycles
30+ // loops using TIMEOUT use at least 4 clock cycli
31+ // so 100 us takes max 400 loops
32+ // so by dividing F_CPU by 40000 we "fail" as fast as possible
33+ #define TIMEOUT (F_CPU/40000 )
3434
3535
3636// ///////////////////////////////////////////////////
4545int dht::read11 (uint8_t pin)
4646{
4747 // READ VALUES
48- int rv = read (pin);
48+ int rv = read (pin, DHTLIB_DHT11_WAKEUP );
4949 if (rv != DHTLIB_OK)
5050 {
5151 humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
@@ -71,7 +71,8 @@ int dht::read11(uint8_t pin)
7171// DHTLIB_ERROR_TIMEOUT
7272int dht::read21 (uint8_t pin)
7373{
74- return read22 (pin); // dataformat identical to DHT22
74+ // dataformat & wakeup identical to DHT22
75+ return read22 (pin);
7576}
7677
7778// return values:
@@ -81,7 +82,7 @@ int dht::read21(uint8_t pin)
8182int dht::read22 (uint8_t pin)
8283{
8384 // READ VALUES
84- int rv = read (pin);
85+ int rv = read (pin, DHTLIB_DHT22_WAKEUP );
8586 if (rv != DHTLIB_OK)
8687 {
8788 humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
@@ -116,44 +117,52 @@ int dht::read22(uint8_t pin)
116117// return values:
117118// DHTLIB_OK
118119// DHTLIB_ERROR_TIMEOUT
119- int dht::read (uint8_t pin)
120+ int dht::read (uint8_t pin, uint8_t wakeupDelay )
120121{
121122 // INIT BUFFERVAR TO RECEIVE DATA
122123 uint8_t mask = 128 ;
123124 uint8_t idx = 0 ;
124125
125126 // EMPTY BUFFER
126- for (uint8_t i= 0 ; i< 5 ; i++) bits[i] = 0 ;
127+ for (uint8_t i = 0 ; i < 5 ; i++) bits[i] = 0 ;
127128
128129 // REQUEST SAMPLE
129130 pinMode (pin, OUTPUT);
130131 digitalWrite (pin, LOW);
131- delay (20 );
132+ delay (wakeupDelay );
132133 digitalWrite (pin, HIGH);
133134 delayMicroseconds (40 );
134135 pinMode (pin, INPUT);
135136
136137 // GET ACKNOWLEDGE or TIMEOUT
137138 unsigned int loopCnt = TIMEOUT;
138139 while (digitalRead (pin) == LOW)
139- if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
140+ {
141+ if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
142+ }
140143
141144 loopCnt = TIMEOUT;
142145 while (digitalRead (pin) == HIGH)
143- if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
146+ {
147+ if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
148+ }
144149
145150 // READ THE OUTPUT - 40 BITS => 5 BYTES
146- for (uint8_t i= 0 ; i< 40 ; i++)
151+ for (uint8_t i = 0 ; i < 40 ; i++)
147152 {
148153 loopCnt = TIMEOUT;
149154 while (digitalRead (pin) == LOW)
150- if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
155+ {
156+ if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
157+ }
151158
152159 unsigned long t = micros ();
153160
154161 loopCnt = TIMEOUT;
155162 while (digitalRead (pin) == HIGH)
156- if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
163+ {
164+ if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
165+ }
157166
158167 if ((micros () - t) > 40 ) bits[idx] |= mask;
159168 mask >>= 1 ;
@@ -163,7 +172,6 @@ int dht::read(uint8_t pin)
163172 idx++;
164173 }
165174 }
166-
167175 return DHTLIB_OK;
168176}
169177//
0 commit comments