11//
22// FILE: dht.cpp
33// AUTHOR: Rob Tillaart
4- // VERSION: 0.1.11
4+ // VERSION: 0.1.13
55// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
66// URL: http://arduino.cc/playground/Main/DHTLib
77//
88// HISTORY:
9+ // 0.1.13 fix negative temperature
10+ // 0.1.12 support DHT33 and DHT44 initial version
911// 0.1.11 renamed DHTLIB_TIMEOUT
1012// 0.1.10 optimized faster WAKEUP + TIMEOUT
1113// 0.1.09 optimize size: timeout check + use of mask
3840int dht::read11 (uint8_t pin)
3941{
4042 // READ VALUES
41- int rv = read (pin, DHTLIB_DHT11_WAKEUP);
43+ int rv = _readSensor (pin, DHTLIB_DHT11_WAKEUP);
4244 if (rv != DHTLIB_OK)
4345 {
4446 humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
@@ -58,24 +60,15 @@ int dht::read11(uint8_t pin)
5860 return DHTLIB_OK;
5961}
6062
61- // return values:
62- // DHTLIB_OK
63- // DHTLIB_ERROR_CHECKSUM
64- // DHTLIB_ERROR_TIMEOUT
65- int dht::read21 (uint8_t pin)
66- {
67- // dataformat & wakeup identical to DHT22
68- return read22 (pin);
69- }
7063
7164// return values:
7265// DHTLIB_OK
7366// DHTLIB_ERROR_CHECKSUM
7467// DHTLIB_ERROR_TIMEOUT
75- int dht::read22 (uint8_t pin)
68+ int dht::read (uint8_t pin)
7669{
7770 // READ VALUES
78- int rv = read (pin, DHTLIB_DHT22_WAKEUP );
71+ int rv = _readSensor (pin, DHTLIB_DHT_WAKEUP );
7972 if (rv != DHTLIB_OK)
8073 {
8174 humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
@@ -85,20 +78,18 @@ int dht::read22(uint8_t pin)
8578
8679 // CONVERT AND STORE
8780 humidity = word (bits[0 ], bits[1 ]) * 0.1 ;
88-
89- if (bits[2 ] & 0x80 ) // negative temperature
90- {
91- temperature = -0.1 * word (bits[2 ] & 0x7F , bits[3 ]);
92- }
93- else
81+ temperature = word (bits[2 ] & 0x7F , bits[3 ]) * 0.1 ;
82+ if (bits[2 ] & 0x80 ) // negative temperature
9483 {
95- temperature = 0.1 * word (bits[ 2 ], bits[ 3 ]) ;
84+ temperature = -temperature ;
9685 }
9786
9887 // TEST CHECKSUM
9988 uint8_t sum = bits[0 ] + bits[1 ] + bits[2 ] + bits[3 ];
100- if (bits[4 ] != sum) return DHTLIB_ERROR_CHECKSUM;
101-
89+ if (bits[4 ] != sum)
90+ {
91+ return DHTLIB_ERROR_CHECKSUM;
92+ }
10293 return DHTLIB_OK;
10394}
10495
@@ -110,7 +101,7 @@ int dht::read22(uint8_t pin)
110101// return values:
111102// DHTLIB_OK
112103// DHTLIB_ERROR_TIMEOUT
113- int dht::read (uint8_t pin, uint8_t wakeupDelay)
104+ int dht::_readSensor (uint8_t pin, uint8_t wakeupDelay)
114105{
115106 // INIT BUFFERVAR TO RECEIVE DATA
116107 uint8_t mask = 128 ;
@@ -128,7 +119,7 @@ int dht::read(uint8_t pin, uint8_t wakeupDelay)
128119 pinMode (pin, INPUT);
129120
130121 // GET ACKNOWLEDGE or TIMEOUT
131- unsigned int loopCnt = DHTLIB_TIMEOUT;
122+ uint16_t loopCnt = DHTLIB_TIMEOUT;
132123 while (digitalRead (pin) == LOW)
133124 {
134125 if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
@@ -141,30 +132,36 @@ int dht::read(uint8_t pin, uint8_t wakeupDelay)
141132 }
142133
143134 // READ THE OUTPUT - 40 BITS => 5 BYTES
144- for (uint8_t i = 0 ; i < 40 ; i++ )
135+ for (uint8_t i = 40 ; i != 0 ; i-- )
145136 {
146137 loopCnt = DHTLIB_TIMEOUT;
147138 while (digitalRead (pin) == LOW)
148139 {
149140 if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
150141 }
151142
152- unsigned long t = micros ();
143+ uint32_t t = micros ();
153144
154145 loopCnt = DHTLIB_TIMEOUT;
155146 while (digitalRead (pin) == HIGH)
156147 {
157148 if (--loopCnt == 0 ) return DHTLIB_ERROR_TIMEOUT;
158149 }
159150
160- if ((micros () - t) > 40 ) bits[idx] |= mask;
151+ if ((micros () - t) > 40 )
152+ {
153+ bits[idx] |= mask;
154+ }
161155 mask >>= 1 ;
162156 if (mask == 0 ) // next byte?
163157 {
164158 mask = 128 ;
165159 idx++;
166160 }
167161 }
162+ pinMode (pin, OUTPUT);
163+ digitalWrite (pin, HIGH);
164+
168165 return DHTLIB_OK;
169166}
170167//
0 commit comments