1
1
//
2
2
// FILE: dht.cpp
3
3
// AUTHOR: Rob Tillaart
4
- // VERSION: 0.1.09
4
+ // VERSION: 0.1.10
5
5
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
6
6
// URL: http://arduino.cc/playground/Main/DHTLib
7
7
//
8
8
// HISTORY:
9
+ // 0.1.10 optimized faster WAKEUP + TIMEOUT
9
10
// 0.1.09 optimize size: timeout check + use of mask
10
11
// 0.1.08 added formula for timeout based upon clockspeed
11
12
// 0.1.07 added support for DHT21
24
25
25
26
#include " dht.h"
26
27
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 )
34
34
35
35
36
36
// ///////////////////////////////////////////////////
45
45
int dht::read11 (uint8_t pin)
46
46
{
47
47
// READ VALUES
48
- int rv = read (pin);
48
+ int rv = read (pin, DHTLIB_DHT11_WAKEUP );
49
49
if (rv != DHTLIB_OK)
50
50
{
51
51
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
@@ -71,7 +71,8 @@ int dht::read11(uint8_t pin)
71
71
// DHTLIB_ERROR_TIMEOUT
72
72
int dht::read21 (uint8_t pin)
73
73
{
74
- return read22 (pin); // dataformat identical to DHT22
74
+ // dataformat & wakeup identical to DHT22
75
+ return read22 (pin);
75
76
}
76
77
77
78
// return values:
@@ -81,7 +82,7 @@ int dht::read21(uint8_t pin)
81
82
int dht::read22 (uint8_t pin)
82
83
{
83
84
// READ VALUES
84
- int rv = read (pin);
85
+ int rv = read (pin, DHTLIB_DHT22_WAKEUP );
85
86
if (rv != DHTLIB_OK)
86
87
{
87
88
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
@@ -116,44 +117,52 @@ int dht::read22(uint8_t pin)
116
117
// return values:
117
118
// DHTLIB_OK
118
119
// DHTLIB_ERROR_TIMEOUT
119
- int dht::read (uint8_t pin)
120
+ int dht::read (uint8_t pin, uint8_t wakeupDelay )
120
121
{
121
122
// INIT BUFFERVAR TO RECEIVE DATA
122
123
uint8_t mask = 128 ;
123
124
uint8_t idx = 0 ;
124
125
125
126
// 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 ;
127
128
128
129
// REQUEST SAMPLE
129
130
pinMode (pin, OUTPUT);
130
131
digitalWrite (pin, LOW);
131
- delay (20 );
132
+ delay (wakeupDelay );
132
133
digitalWrite (pin, HIGH);
133
134
delayMicroseconds (40 );
134
135
pinMode (pin, INPUT);
135
136
136
137
// GET ACKNOWLEDGE or TIMEOUT
137
138
unsigned int loopCnt = TIMEOUT;
138
139
while (digitalRead (pin) == LOW)
139
- if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
140
+ {
141
+ if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
142
+ }
140
143
141
144
loopCnt = TIMEOUT;
142
145
while (digitalRead (pin) == HIGH)
143
- if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
146
+ {
147
+ if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
148
+ }
144
149
145
150
// 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++)
147
152
{
148
153
loopCnt = TIMEOUT;
149
154
while (digitalRead (pin) == LOW)
150
- if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
155
+ {
156
+ if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
157
+ }
151
158
152
159
unsigned long t = micros ();
153
160
154
161
loopCnt = TIMEOUT;
155
162
while (digitalRead (pin) == HIGH)
156
- if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
163
+ {
164
+ if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
165
+ }
157
166
158
167
if ((micros () - t) > 40 ) bits[idx] |= mask;
159
168
mask >>= 1 ;
@@ -163,7 +172,6 @@ int dht::read(uint8_t pin)
163
172
idx++;
164
173
}
165
174
}
166
-
167
175
return DHTLIB_OK;
168
176
}
169
177
//
0 commit comments