-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
dht.cpp
261 lines (222 loc) · 6.38 KB
/
dht.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
//
// FILE: dht.cpp
// AUTHOR: Rob Tillaart
// VERSION: 0.1.36
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino, AVR optimized
// URL: https://github.com/RobTillaart/DHTlib
// http://arduino.cc/playground/Main/DHTLib
//
// inspired by DHT11 library
#include "dht.h"
#define DHTLIB_DHT11_WAKEUP 18
#define DHTLIB_DHT_WAKEUP 1
#define DHTLIB_DHT11_LEADING_ZEROS 1
#define DHTLIB_DHT_LEADING_ZEROS 6
// max timeout is 100 microseconds.
// For a 16 MHz processor 100 microseconds is 1600 clock cycles
// loops using DHTLIB_TIMEOUT use at least 4 clock cycles
// so 100 us takes max 400 loops
// so by dividing F_CPU by 40000 we "fail" as fast as possible
#ifndef F_CPU
#define DHTLIB_TIMEOUT 1000 // should be approx. clock/40000
#else
#define DHTLIB_TIMEOUT (F_CPU/40000)
#endif
/////////////////////////////////////////////////////
//
// PUBLIC
//
int8_t dht::read11(uint8_t pin)
{
// READ VALUES
if (_disableIRQ) noInterrupts();
int8_t result = _readSensor(pin, DHTLIB_DHT11_WAKEUP, DHTLIB_DHT11_LEADING_ZEROS);
if (_disableIRQ) interrupts();
// these bits are always zero, masking them reduces errors.
bits[0] &= 0x7F;
bits[2] &= 0x7F;
// CONVERT AND STORE
humidity = bits[0]; // bits[1] == 0;
temperature = bits[2]; // bits[3] == 0;
// TEST CHECKSUM
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
if (bits[4] != sum)
{
return DHTLIB_ERROR_CHECKSUM;
}
return result;
}
int8_t dht::read12(uint8_t pin)
{
// READ VALUES
if (_disableIRQ) noInterrupts();
int8_t result = _readSensor(pin, DHTLIB_DHT11_WAKEUP, DHTLIB_DHT11_LEADING_ZEROS);
if (_disableIRQ) interrupts();
// CONVERT AND STORE
humidity = bits[0] + bits[1] * 0.1;
temperature = bits[2] + (bits[3] & 0x7F) * 0.1;
if (bits[3] & 0x80) // negative temperature
{
temperature = -temperature;
}
// TEST CHECKSUM
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
if (bits[4] != sum)
{
return DHTLIB_ERROR_CHECKSUM;
}
return result;
}
int8_t dht::read(uint8_t pin)
{
// READ VALUES
if (_disableIRQ) noInterrupts();
int8_t result = _readSensor(pin, DHTLIB_DHT_WAKEUP, DHTLIB_DHT_LEADING_ZEROS);
if (_disableIRQ) interrupts();
// these bits are always zero, masking them reduces errors.
bits[0] &= 0x03;
bits[2] &= 0x83;
// CONVERT AND STORE
humidity = (bits[0] * 256 + bits[1]) * 0.1;
int16_t t = ((bits[2] & 0x7F) * 256 + bits[3]);
if (t == 0)
{
temperature = 0.0; // prevent -0.0;
}
else
{
temperature = t * 0.1;
if((bits[2] & 0x80) == 0x80 )
{
temperature = -temperature;
}
}
// HEXDUMP DEBUG
/*
Serial.println();
// CHECKSUM
if (_bits[4] < 0x10) Serial.print(0);
Serial.print(_bits[4], HEX);
Serial.print(" ");
// TEMPERATURE
if (_bits[2] < 0x10) Serial.print(0);
Serial.print(_bits[2], HEX);
if (_bits[3] < 0x10) Serial.print(0);
Serial.print(_bits[3], HEX);
Serial.print(" ");
Serial.print(temperature, 1);
Serial.print(" ");
// HUMIDITY
if (_bits[0] < 0x10) Serial.print(0);
Serial.print(_bits[0], HEX);
if (_bits[1] < 0x10) Serial.print(0);
Serial.print(_bits[1], HEX);
Serial.print(" ");
Serial.print(humidity, 1);
*/
// TEST CHECKSUM
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
if (bits[4] != sum)
{
return DHTLIB_ERROR_CHECKSUM;
}
return result;
}
/////////////////////////////////////////////////////
//
// PRIVATE
//
int8_t dht::_readSensor(uint8_t pin, uint8_t wakeupDelay, uint8_t leadingZeroBits)
{
// INIT BUFFERVAR TO RECEIVE DATA
uint8_t mask = 128;
uint8_t idx = 0;
uint8_t data = 0;
uint8_t state = LOW;
uint8_t pstate = LOW;
uint16_t zeroLoop = DHTLIB_TIMEOUT;
uint16_t delta = 0;
leadingZeroBits = 40 - leadingZeroBits; // reverse counting...
// replace digitalRead() with Direct Port Reads.
// reduces footprint ~100 bytes => portability issue?
// direct port read is about 3x faster
uint8_t bit = digitalPinToBitMask(pin);
uint8_t port = digitalPinToPort(pin);
volatile uint8_t *PIR = portInputRegister(port);
// REQUEST SAMPLE
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW); // T-be
if (wakeupDelay > 8) delay(wakeupDelay);
else delayMicroseconds(wakeupDelay * 1000UL);
digitalWrite(pin, HIGH); // T-go
pinMode(pin, INPUT);
uint16_t loopCount = DHTLIB_TIMEOUT * 2; // 200 uSec max
// while(digitalRead(pin) == HIGH)
while ((*PIR & bit) != LOW )
{
if (--loopCount == 0)
{
return DHTLIB_ERROR_CONNECT;
}
}
// GET ACKNOWLEDGE or TIMEOUT
loopCount = DHTLIB_TIMEOUT;
// while(digitalRead(pin) == LOW)
while ((*PIR & bit) == LOW ) // T-rel
{
if (--loopCount == 0)
{
return DHTLIB_ERROR_ACK_L;
}
}
loopCount = DHTLIB_TIMEOUT;
// while(digitalRead(pin) == HIGH)
while ((*PIR & bit) != LOW ) // T-reh
{
if (--loopCount == 0)
{
return DHTLIB_ERROR_ACK_H;
}
}
loopCount = DHTLIB_TIMEOUT;
// READ THE OUTPUT - 40 BITS => 5 BYTES
for (uint8_t i = 40; i != 0; )
{
// WAIT FOR FALLING EDGE
state = (*PIR & bit);
if (state == LOW && pstate != LOW)
{
if (i > leadingZeroBits) // DHT22 first 6 bits are all zero !! DHT11 only 1
{
zeroLoop = min(zeroLoop, loopCount);
delta = (DHTLIB_TIMEOUT - zeroLoop)/4;
}
else if ( loopCount <= (zeroLoop - delta) ) // long -> one
{
data |= mask;
}
mask >>= 1;
if (mask == 0) // next byte
{
mask = 128;
bits[idx] = data;
idx++;
data = 0;
}
// next bit
--i;
// reset timeout flag
loopCount = DHTLIB_TIMEOUT;
}
pstate = state;
// Check timeout
if (--loopCount == 0)
{
return DHTLIB_ERROR_TIMEOUT;
}
}
// pinMode(pin, OUTPUT);
// digitalWrite(pin, HIGH);
return DHTLIB_OK;
}
// -- END OF FILE --