Skip to content

Commit 9419315

Browse files
committed
Merge pull request #36 from Zirientis/master
Cause interrupts to be reenabled if a timeout occurs while waiting for the sensor
2 parents a1393fc + a2208eb commit 9419315

File tree

2 files changed

+57
-45
lines changed

2 files changed

+57
-45
lines changed

DHT.cpp

Lines changed: 46 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -140,62 +140,63 @@ boolean DHT::read(void) {
140140

141141
// Turn off interrupts temporarily because the next sections are timing critical
142142
// and we don't want any interruptions.
143-
noInterrupts();
143+
{
144+
InterruptLock lock;
144145

145-
// End the start signal by setting data line high for 40 microseconds.
146-
digitalWrite(_pin, HIGH);
147-
delayMicroseconds(40);
146+
// End the start signal by setting data line high for 40 microseconds.
147+
digitalWrite(_pin, HIGH);
148+
delayMicroseconds(40);
148149

149-
// Now start reading the data line to get the value from the DHT sensor.
150-
pinMode(_pin, INPUT);
151-
delayMicroseconds(10); // Delay a bit to let sensor pull data line low.
150+
// Now start reading the data line to get the value from the DHT sensor.
151+
pinMode(_pin, INPUT);
152+
delayMicroseconds(10); // Delay a bit to let sensor pull data line low.
152153

153-
// First expect a low signal for ~80 microseconds followed by a high signal
154-
// for ~80 microseconds again.
155-
if (expectPulse(LOW) == 0) {
156-
DEBUG_PRINTLN(F("Timeout waiting for start signal low pulse."));
157-
_lastresult = false;
158-
return _lastresult;
159-
}
160-
if (expectPulse(HIGH) == 0) {
161-
DEBUG_PRINTLN(F("Timeout waiting for start signal high pulse."));
162-
_lastresult = false;
163-
return _lastresult;
164-
}
165-
166-
// Now read the 40 bits sent by the sensor. Each bit is sent as a 50
167-
// microsecond low pulse followed by a variable length high pulse. If the
168-
// high pulse is ~28 microseconds then it's a 0 and if it's ~70 microseconds
169-
// then it's a 1. We measure the cycle count of the initial 50us low pulse
170-
// and use that to compare to the cycle count of the high pulse to determine
171-
// if the bit is a 0 (high state cycle count < low state cycle count), or a
172-
// 1 (high state cycle count > low state cycle count).
173-
for (int i=0; i<40; ++i) {
174-
uint32_t lowCycles = expectPulse(LOW);
175-
if (lowCycles == 0) {
176-
DEBUG_PRINTLN(F("Timeout waiting for bit low pulse."));
154+
// First expect a low signal for ~80 microseconds followed by a high signal
155+
// for ~80 microseconds again.
156+
if (expectPulse(LOW) == 0) {
157+
DEBUG_PRINTLN(F("Timeout waiting for start signal low pulse."));
177158
_lastresult = false;
178159
return _lastresult;
179160
}
180-
uint32_t highCycles = expectPulse(HIGH);
181-
if (highCycles == 0) {
182-
DEBUG_PRINTLN(F("Timeout waiting for bit high pulse."));
161+
if (expectPulse(HIGH) == 0) {
162+
DEBUG_PRINTLN(F("Timeout waiting for start signal high pulse."));
183163
_lastresult = false;
184164
return _lastresult;
185165
}
186-
data[i/8] <<= 1;
187-
// Now compare the low and high cycle times to see if the bit is a 0 or 1.
188-
if (highCycles > lowCycles) {
189-
// High cycles are greater than 50us low cycle count, must be a 1.
190-
data[i/8] |= 1;
166+
167+
// Now read the 40 bits sent by the sensor. Each bit is sent as a 50
168+
// microsecond low pulse followed by a variable length high pulse. If the
169+
// high pulse is ~28 microseconds then it's a 0 and if it's ~70 microseconds
170+
// then it's a 1. We measure the cycle count of the initial 50us low pulse
171+
// and use that to compare to the cycle count of the high pulse to determine
172+
// if the bit is a 0 (high state cycle count < low state cycle count), or a
173+
// 1 (high state cycle count > low state cycle count).
174+
for (int i=0; i<40; ++i) {
175+
uint32_t lowCycles = expectPulse(LOW);
176+
if (lowCycles == 0) {
177+
DEBUG_PRINTLN(F("Timeout waiting for bit low pulse."));
178+
_lastresult = false;
179+
return _lastresult;
180+
}
181+
uint32_t highCycles = expectPulse(HIGH);
182+
if (highCycles == 0) {
183+
DEBUG_PRINTLN(F("Timeout waiting for bit high pulse."));
184+
_lastresult = false;
185+
return _lastresult;
186+
}
187+
data[i/8] <<= 1;
188+
// Now compare the low and high cycle times to see if the bit is a 0 or 1.
189+
if (highCycles > lowCycles) {
190+
// High cycles are greater than 50us low cycle count, must be a 1.
191+
data[i/8] |= 1;
192+
}
193+
// Else high cycles are less than (or equal to, a weird case) the 50us low
194+
// cycle count so this must be a zero. Nothing needs to be changed in the
195+
// stored data.
191196
}
192-
// Else high cycles are less than (or equal to, a weird case) the 50us low
193-
// cycle count so this must be a zero. Nothing needs to be changed in the
194-
// stored data.
195-
}
197+
// Timing critical code is now complete.
196198

197-
// Re-enable interrupts, timing critical code is complete.
198-
interrupts();
199+
}
199200

200201
DEBUG_PRINTLN(F("Received:"));
201202
DEBUG_PRINT(data[0], HEX); DEBUG_PRINT(F(", "));

DHT.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,15 @@ class DHT {
5757

5858
};
5959

60+
class InterruptLock {
61+
public:
62+
InterruptLock() {
63+
noInterrupts();
64+
}
65+
~InterruptLock() {
66+
interrupts();
67+
}
68+
69+
};
70+
6071
#endif

0 commit comments

Comments
 (0)