Skip to content

Commit fa681d5

Browse files
committed
+ reduced # micros calls 2->1 in inner loop. => footprint -18
detection of the zero/one is doen by measuring the whole bit LOW+HIGH + improved wait for acknowledge + merged two loopCounters into one. + layout + dht22_test sketch collects statistics (every 20 reads)
1 parent e4ad650 commit fa681d5

File tree

3 files changed

+90
-41
lines changed

3 files changed

+90
-41
lines changed

libraries/DHTlib/dht.cpp

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
//
22
// FILE: dht.cpp
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.14
4+
// VERSION: 0.1.15
55
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
66
// URL: http://arduino.cc/playground/Main/DHTLib
77
//
88
// HISTORY:
9+
// 0.1.15 reduced # micros calls 2->1 in inner loop.
910
// 0.1.14 replace digital read with faster (~3x) code => more robust low MHz machines.
1011
// 0.1.13 fix negative temperature
1112
// 0.1.12 support DHT33 and DHT44 initial version
@@ -44,8 +45,8 @@ int dht::read11(uint8_t pin)
4445
int rv = _readSensor(pin, DHTLIB_DHT11_WAKEUP);
4546
if (rv != DHTLIB_OK)
4647
{
47-
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
48-
temperature = DHTLIB_INVALID_VALUE; // invalid value
48+
humidity = DHTLIB_INVALID_VALUE;
49+
temperature = DHTLIB_INVALID_VALUE;
4950
return rv;
5051
}
5152

@@ -56,8 +57,10 @@ int dht::read11(uint8_t pin)
5657
// TEST CHECKSUM
5758
// bits[1] && bits[3] both 0
5859
uint8_t sum = bits[0] + bits[2];
59-
if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
60-
60+
if (bits[4] != sum)
61+
{
62+
return DHTLIB_ERROR_CHECKSUM;
63+
}
6164
return DHTLIB_OK;
6265
}
6366

@@ -111,61 +114,75 @@ int dht::_readSensor(uint8_t pin, uint8_t wakeupDelay)
111114
// replace digitalRead() with Direct Port Reads.
112115
// reduces footprint ~100 bytes => portability issue?
113116
// direct port read is about 3x faster
114-
uint8_t bit = digitalPinToBitMask(pin);
115-
uint8_t port = digitalPinToPort(pin);
117+
uint8_t bit = digitalPinToBitMask(pin);
118+
uint8_t port = digitalPinToPort(pin);
116119
volatile uint8_t *PIR = portInputRegister(port);
117120

118121
// EMPTY BUFFER
119122
for (uint8_t i = 0; i < 5; i++) bits[i] = 0;
120123

121124
// REQUEST SAMPLE
122125
pinMode(pin, OUTPUT);
123-
digitalWrite(pin, LOW); // T-be
126+
digitalWrite(pin, LOW); // T-be
124127
delay(wakeupDelay);
125-
digitalWrite(pin, HIGH); // T-go
126-
delayMicroseconds(40);
128+
digitalWrite(pin, HIGH); // T-go
127129
pinMode(pin, INPUT);
128130

131+
uint16_t loopCount = DHTLIB_TIMEOUT*2; // 200uSec max
132+
// while(digitalRead(pin) == HIGH)
133+
while ((*PIR & bit) != LOW )
134+
{
135+
if (--loopCount == 0) return DHTLIB_ERROR_TIMEOUT;
136+
}
137+
129138
// GET ACKNOWLEDGE or TIMEOUT
130-
uint16_t loopCntLOW = DHTLIB_TIMEOUT;
139+
loopCount = DHTLIB_TIMEOUT;
140+
// while(digitalRead(pin) == LOW)
131141
while ((*PIR & bit) == LOW ) // T-rel
132142
{
133-
if (--loopCntLOW == 0) return DHTLIB_ERROR_TIMEOUT;
143+
if (--loopCount == 0) return DHTLIB_ERROR_TIMEOUT;
134144
}
135145

136-
uint16_t loopCntHIGH = DHTLIB_TIMEOUT;
146+
loopCount = DHTLIB_TIMEOUT;
147+
// while(digitalRead(pin) == HIGH)
137148
while ((*PIR & bit) != LOW ) // T-reh
138149
{
139-
if (--loopCntHIGH == 0) return DHTLIB_ERROR_TIMEOUT;
150+
if (--loopCount == 0) return DHTLIB_ERROR_TIMEOUT;
140151
}
141152

153+
uint8_t state = LOW;
154+
uint8_t pstate = LOW;
155+
loopCount = DHTLIB_TIMEOUT;
156+
uint32_t t1 = micros();
157+
142158
// READ THE OUTPUT - 40 BITS => 5 BYTES
143-
for (uint8_t i = 40; i != 0; i--)
159+
for (uint8_t i = 40; i != 0; )
144160
{
145-
loopCntLOW = DHTLIB_TIMEOUT;
146-
while ((*PIR & bit) == LOW )
147-
{
148-
if (--loopCntLOW == 0) return DHTLIB_ERROR_TIMEOUT;
149-
}
150-
151-
uint32_t t = micros();
152-
153-
loopCntHIGH = DHTLIB_TIMEOUT;
154-
while ((*PIR & bit) != LOW )
155-
{
156-
if (--loopCntHIGH == 0) return DHTLIB_ERROR_TIMEOUT;
157-
}
158-
159-
if ((micros() - t) > 40)
160-
{
161-
bits[idx] |= mask;
162-
}
163-
mask >>= 1;
164-
if (mask == 0) // next byte?
161+
// WAIT FOR FALLING EDGE
162+
state = (*PIR & bit);
163+
if (state == LOW && pstate != LOW)
165164
{
166-
mask = 128;
167-
idx++;
165+
uint32_t t2 = micros();
166+
if ((t2-t1) > 100 ) // long -> one
167+
{
168+
bits[idx] |= mask;
169+
}
170+
mask >>= 1;
171+
if (mask == 0) // next byte
172+
{
173+
mask = 128;
174+
idx++;
175+
}
176+
// next bit
177+
--i;
178+
// update time admin
179+
t1 = t2;
180+
// reset timeout flag
181+
loopCount = DHTLIB_TIMEOUT;
168182
}
183+
pstate = state;
184+
// Check timeout
185+
if (--loopCount == 0) return DHTLIB_ERROR_TIMEOUT;
169186
}
170187
pinMode(pin, OUTPUT);
171188
digitalWrite(pin, HIGH);

libraries/DHTlib/dht.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//
22
// FILE: dht.h
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.14
4+
// VERSION: 0.1.15
55
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
66
// URL: http://arduino.cc/playground/Main/DHTLib
77
//
@@ -18,7 +18,7 @@
1818
#include <Arduino.h>
1919
#endif
2020

21-
#define DHT_LIB_VERSION "0.1.14"
21+
#define DHT_LIB_VERSION "0.1.15"
2222

2323
#define DHTLIB_OK 0
2424
#define DHTLIB_ERROR_CHECKSUM -1

libraries/DHTlib/examples/dht22_test/dht22_test.ino

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
//
22
// FILE: dht22_test.ino
33
// AUTHOR: Rob Tillaart
4-
// VERSION: 0.1.01
4+
// VERSION: 0.1.02
55
// PURPOSE: DHT library test sketch for DHT22 && Arduino
66
// URL:
7+
// HISTORY:
8+
// 0.1.02 added counters for error-regression testing.
9+
// 0.1.01
10+
// 0.1.00 initial version
711
//
812
// Released to the public domain
913
//
@@ -14,10 +18,19 @@ dht DHT;
1418

1519
#define DHT22_PIN 5
1620

21+
struct
22+
{
23+
uint32_t total;
24+
uint32_t ok;
25+
uint32_t crc_error;
26+
uint32_t time_out;
27+
uint32_t unknown;
28+
} stat = { 0,0,0,0,0 };
29+
1730
void setup()
1831
{
1932
Serial.begin(115200);
20-
Serial.println("DHT TEST PROGRAM ");
33+
Serial.println("dht22_test.ino");
2134
Serial.print("LIBRARY VERSION: ");
2235
Serial.println(DHT_LIB_VERSION);
2336
Serial.println();
@@ -33,18 +46,23 @@ void loop()
3346
int chk = DHT.read22(DHT22_PIN);
3447
uint32_t stop = micros();
3548

49+
stat.total++;
3650
switch (chk)
3751
{
3852
case DHTLIB_OK:
53+
stat.ok++;
3954
Serial.print("OK,\t");
4055
break;
4156
case DHTLIB_ERROR_CHECKSUM:
57+
stat.crc_error++;
4258
Serial.print("Checksum error,\t");
4359
break;
4460
case DHTLIB_ERROR_TIMEOUT:
61+
stat.time_out++;
4562
Serial.print("Time out error,\t");
4663
break;
4764
default:
65+
stat.unknown++;
4866
Serial.print("Unknown error,\t");
4967
break;
5068
}
@@ -56,6 +74,20 @@ void loop()
5674
Serial.print(stop - start);
5775
Serial.println();
5876

77+
if (stat.total % 20 == 0)
78+
{
79+
Serial.println("\nTOT\tOK\tCRC\tTO\tUNK");
80+
Serial.print(stat.total);
81+
Serial.print("\t");
82+
Serial.print(stat.ok);
83+
Serial.print("\t");
84+
Serial.print(stat.crc_error);
85+
Serial.print("\t");
86+
Serial.print(stat.time_out);
87+
Serial.print("\t");
88+
Serial.print(stat.unknown);
89+
Serial.println("\n");
90+
}
5991
delay(2000);
6092
}
6193
//

0 commit comments

Comments
 (0)