+ optimize size
+ timeout check
+ use of mask to minimize shifts
+ added time measurement in dht22_test
+ reindented code
This commit is contained in:
rob tillaart 2014-02-08 13:53:52 +01:00
parent 5be850301c
commit 616ee0c061
3 changed files with 127 additions and 118 deletions

View File

@ -6,6 +6,7 @@
// URL: http://arduino.cc/playground/Main/DHTLib
//
// HISTORY:
// 0.1.09 optimize size: timeout check + use of mask
// 0.1.08 added formula for timeout based upon clockspeed
// 0.1.07 added support for DHT21
// 0.1.06 minimize footprint (2012-12-27)
@ -26,6 +27,9 @@
// #define TIMEOUT 10000
// uint16_t for UNO, higher CPU speeds => exceed MAXINT.
// works for DUE
// 16 MHz => 10000
// 84 MHz => 52500
// 100MHz => 62500
#define TIMEOUT (F_CPU/1600)
@ -115,7 +119,7 @@ int dht::read22(uint8_t pin)
int dht::read(uint8_t pin)
{
// INIT BUFFERVAR TO RECEIVE DATA
uint8_t cnt = 7;
uint8_t mask = 128;
uint8_t idx = 0;
// EMPTY BUFFER
@ -129,36 +133,35 @@ int dht::read(uint8_t pin)
delayMicroseconds(40);
pinMode(pin, INPUT);
// TODO rewrite with miros()?
// GET ACKNOWLEDGE or TIMEOUT
unsigned int loopCnt = TIMEOUT;
while(digitalRead(pin) == LOW)
if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
loopCnt = TIMEOUT;
while(digitalRead(pin) == HIGH)
if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
// READ THE OUTPUT - 40 BITS => 5 BYTES
for (uint8_t i=0; i<40; i++)
{
loopCnt = TIMEOUT;
while(digitalRead(pin) == LOW)
if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
unsigned long t = micros();
loopCnt = TIMEOUT;
while(digitalRead(pin) == HIGH)
if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
if (cnt == 0) // next byte?
if ((micros() - t) > 40) bits[idx] |= mask;
mask >>= 1;
if (mask == 0) // next byte?
{
cnt = 7;
mask = 128;
idx++;
}
else cnt--;
}
return DHTLIB_OK;

View File

@ -1,7 +1,7 @@
//
// FILE: dht.h
// AUTHOR: Rob Tillaart
// VERSION: 0.1.08
// VERSION: 0.1.09
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
// URL: http://arduino.cc/playground/Main/DHTLib
//
@ -18,7 +18,7 @@
#include <Arduino.h>
#endif
#define DHT_LIB_VERSION "0.1.08"
#define DHT_LIB_VERSION "0.1.09"
#define DHTLIB_OK 0
#define DHTLIB_ERROR_CHECKSUM -1

View File

@ -1,8 +1,7 @@
//
// FILE: dht22_test.ino
// AUTHOR: Rob Tillaart
// VERSION: 0.1.00
// VERSION: 0.1.01
// PURPOSE: DHT library test sketch for DHT22 && Arduino
// URL:
//
@ -22,14 +21,18 @@ void setup()
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT_LIB_VERSION);
Serial.println();
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)\tTime (us)");
}
void loop()
{
// READ DATA
Serial.print("DHT22, \t");
uint32_t start = micros();
int chk = DHT.read22(DHT22_PIN);
uint32_t stop = micros();
switch (chk)
{
case DHTLIB_OK:
@ -48,7 +51,10 @@ void loop()
// DISPLAY DATA
Serial.print(DHT.humidity, 1);
Serial.print(",\t");
Serial.println(DHT.temperature, 1);
Serial.print(DHT.temperature, 1);
Serial.print(",\t");
Serial.print(stop - start);
Serial.println();
delay(2000);
}