forked from nethoncho/Arduino-DHT22
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DHT22.cpp
243 lines (219 loc) · 6.22 KB
/
DHT22.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
/*
DHT22.cpp - DHT22 sensor library
Developed by Ben Adams - 2011
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Humidity and Temperature Sensor DHT22 info found at
http://www.sparkfun.com/products/10167
Version 0.4: 24-Jan-2011 by Ben Adams
Added return code constants to keywords.txt
Returns DHT_ERROR_CHECKSUM on check sum mismatch
Version 0.3: 17-Jan-2011 by Ben Adams
This version reads data
Needs check sum code added at the end of readData
Version 0.2: 16-Jan-2011 by Ben Adams
Changed coding style to match other Arduino libraries.
This version will not read data either!
Version 0.1: 10-Jan-2011 by Ben Adams nethoncho AT gmail.com
First Version is a skeleton. This version will not read data!
Code adapted from the following sources:
The Arduino OneWire lib
http://sheepdogguides.com/arduino/ar3ne1humDHT11.htm
*/
#include "DHT22.h"
#include "pins_arduino.h"
extern "C" {
#include "WConstants.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
}
#define DIRECT_READ(base, mask) (((*(base)) & (mask)) ? 1 : 0)
#define DIRECT_MODE_INPUT(base, mask) ((*(base+1)) &= ~(mask))
#define DIRECT_MODE_OUTPUT(base, mask) ((*(base+1)) |= (mask))
#define DIRECT_WRITE_LOW(base, mask) ((*(base+2)) &= ~(mask))
//#define DIRECT_WRITE_HIGH(base, mask) ((*(base+2)) |= (mask))
// This should be 40, but the sensor is adding an extra bit at the start
#define DHT22_DATA_BIT_COUNT 41
DHT22::DHT22(uint8_t pin)
{
_bitmask = digitalPinToBitMask(pin);
_baseReg = portInputRegister(digitalPinToPort(pin));
_lastReadTime = millis();
_lastHumidity = DHT22_ERROR_VALUE;
_lastTemperature = DHT22_ERROR_VALUE;
}
//
// Read the 40 bit data stream from the DHT 22
// Store the results in private member data to be read by public member functions
//
DHT22_ERROR_t DHT22::readData()
{
uint8_t bitmask = _bitmask;
volatile uint8_t *reg asm("r30") = _baseReg;
uint8_t retryCount;
uint8_t bitTimes[DHT22_DATA_BIT_COUNT];
int currentHumidity;
int currentTemperature;
uint8_t checkSum, csPart1, csPart2, csPart3, csPart4;
unsigned long currentTime;
int i;
currentHumidity = 0;
currentTemperature = 0;
checkSum = 0;
currentTime = millis();
for(i = 0; i < DHT22_DATA_BIT_COUNT; i++)
{
bitTimes[i] = 0;
}
if(currentTime - _lastReadTime < 2000)
{
// Caller needs to wait 2 seconds between each call to readData
return DHT_ERROR_TOOQUICK;
}
_lastReadTime = currentTime;
// Pin needs to start HIGH, wait until it is HIGH with a timeout
cli();
DIRECT_MODE_INPUT(reg, bitmask);
sei();
retryCount = 0;
do
{
if (retryCount > 125)
{
return DHT_BUS_HUNG;
}
retryCount++;
delayMicroseconds(2);
} while(!DIRECT_READ(reg, bitmask));
// Send the activate pulse
cli();
DIRECT_WRITE_LOW(reg, bitmask);
DIRECT_MODE_OUTPUT(reg, bitmask); // Output Low
sei();
delayMicroseconds(1100); // 1.1 ms
cli();
DIRECT_MODE_INPUT(reg, bitmask); // Switch back to input so pin can float
sei();
// Find the start of the ACK Pulse
retryCount = 0;
do
{
if (retryCount > 25) //(Spec is 20 to 40 us, 25*2 == 50 us)
{
return DHT_ERROR_NOT_PRESENT;
}
retryCount++;
delayMicroseconds(2);
} while(!DIRECT_READ(reg, bitmask));
// Find the end of the ACK Pulse
retryCount = 0;
do
{
if (retryCount > 50) //(Spec is 80 us, 50*2 == 100 us)
{
return DHT_ERROR_ACK_TOO_LONG;
}
retryCount++;
delayMicroseconds(2);
} while(DIRECT_READ(reg, bitmask));
// Read the 40 bit data stream
for(i = 0; i < DHT22_DATA_BIT_COUNT; i++)
{
// Find the start of the sync pulse
retryCount = 0;
do
{
if (retryCount > 35) //(Spec is 50 us, 35*2 == 70 us)
{
return DHT_ERROR_SYNC_TIMEOUT;
}
retryCount++;
delayMicroseconds(2);
} while(!DIRECT_READ(reg, bitmask));
// Measure the width of the data pulse
retryCount = 0;
do
{
if (retryCount > 50) //(Spec is 80 us, 50*2 == 100 us)
{
return DHT_ERROR_DATA_TIMEOUT;
}
retryCount++;
delayMicroseconds(2);
} while(DIRECT_READ(reg, bitmask));
bitTimes[i] = retryCount;
}
// Now bitTimes have the number of retries (us *2)
// that were needed to find the end of each data bit
// Spec: 0 is 26 to 28 us
// Spec: 1 is 70 us
// bitTimes[x] <= 11 is a 0
// bitTimes[x] > 11 is a 1
// Note: the bits are offset by one from the data sheet, not sure why
for(i = 0; i < 16; i++)
{
if(bitTimes[i + 1] > 11)
{
currentHumidity |= (1 << (15 - i));
}
}
for(i = 0; i < 16; i++)
{
if(bitTimes[i + 17] > 11)
{
currentTemperature |= (1 << (15 - i));
}
}
for(i = 0; i < 8; i++)
{
if(bitTimes[i + 33] > 11)
{
checkSum |= (1 << (7 - i));
}
}
_lastHumidity = (float(currentHumidity & 0x7FFF) / 10.0);
if(currentTemperature & 0x8000)
{
// Below zero, non standard way of encoding negative numbers!
currentTemperature &= 0x7FFF;
_lastTemperature = (float(currentTemperature) / 10.0) * -1.0;
}
else
{
_lastTemperature = float(currentTemperature) / 10.0;
}
csPart1 = currentHumidity >> 8;
csPart2 = currentHumidity & 0xFF;
csPart3 = currentTemperature >> 8;
csPart4 = currentTemperature & 0xFF;
if(checkSum == ((csPart1 + csPart2 + csPart3 + csPart4) & 0xFF))
{
return DHT_ERROR_NONE;
}
return DHT_ERROR_CHECKSUM;
}
float DHT22::getHumidity()
{
return _lastHumidity;
}
float DHT22::getTemperatureC()
{
return _lastTemperature;
}
//
// This is used when the millis clock rolls over to zero
//
void DHT22::clockReset()
{
_lastReadTime = millis();
}