mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-09-19 16:46:11 -04:00
+ 0.1.09
+ optimize size + timeout check + use of mask to minimize shifts + added time measurement in dht22_test + reindented code
This commit is contained in:
parent
5be850301c
commit
616ee0c061
@ -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)
|
||||
@ -23,10 +24,13 @@
|
||||
|
||||
#include "dht.h"
|
||||
|
||||
// #define TIMEOUT 10000
|
||||
// #define TIMEOUT 10000
|
||||
// uint16_t for UNO, higher CPU speeds => exceed MAXINT.
|
||||
// works for DUE
|
||||
#define TIMEOUT (F_CPU/1600)
|
||||
// 16 MHz => 10000
|
||||
// 84 MHz => 52500
|
||||
// 100MHz => 62500
|
||||
#define TIMEOUT (F_CPU/1600)
|
||||
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
@ -40,25 +44,25 @@
|
||||
// DHTLIB_ERROR_TIMEOUT
|
||||
int dht::read11(uint8_t pin)
|
||||
{
|
||||
// READ VALUES
|
||||
int rv = read(pin);
|
||||
if (rv != DHTLIB_OK)
|
||||
// READ VALUES
|
||||
int rv = read(pin);
|
||||
if (rv != DHTLIB_OK)
|
||||
{
|
||||
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
|
||||
temperature = DHTLIB_INVALID_VALUE; // invalid value
|
||||
return rv;
|
||||
}
|
||||
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
|
||||
temperature = DHTLIB_INVALID_VALUE; // invalid value
|
||||
return rv;
|
||||
}
|
||||
|
||||
// CONVERT AND STORE
|
||||
humidity = bits[0]; // bits[1] == 0;
|
||||
temperature = bits[2]; // bits[3] == 0;
|
||||
// CONVERT AND STORE
|
||||
humidity = bits[0]; // bits[1] == 0;
|
||||
temperature = bits[2]; // bits[3] == 0;
|
||||
|
||||
// TEST CHECKSUM
|
||||
// bits[1] && bits[3] both 0
|
||||
uint8_t sum = bits[0] + bits[2];
|
||||
if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
|
||||
// TEST CHECKSUM
|
||||
// bits[1] && bits[3] both 0
|
||||
uint8_t sum = bits[0] + bits[2];
|
||||
if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
|
||||
|
||||
return DHTLIB_OK;
|
||||
return DHTLIB_OK;
|
||||
}
|
||||
|
||||
// return values:
|
||||
@ -67,7 +71,7 @@ int dht::read11(uint8_t pin)
|
||||
// DHTLIB_ERROR_TIMEOUT
|
||||
int dht::read21(uint8_t pin)
|
||||
{
|
||||
return read22(pin); // dataformat identical to DHT22
|
||||
return read22(pin); // dataformat identical to DHT22
|
||||
}
|
||||
|
||||
// return values:
|
||||
@ -76,32 +80,32 @@ int dht::read21(uint8_t pin)
|
||||
// DHTLIB_ERROR_TIMEOUT
|
||||
int dht::read22(uint8_t pin)
|
||||
{
|
||||
// READ VALUES
|
||||
int rv = read(pin);
|
||||
if (rv != DHTLIB_OK)
|
||||
// READ VALUES
|
||||
int rv = read(pin);
|
||||
if (rv != DHTLIB_OK)
|
||||
{
|
||||
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
|
||||
temperature = DHTLIB_INVALID_VALUE; // invalid value
|
||||
return rv; // propagate error value
|
||||
}
|
||||
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
|
||||
temperature = DHTLIB_INVALID_VALUE; // invalid value
|
||||
return rv; // propagate error value
|
||||
}
|
||||
|
||||
// CONVERT AND STORE
|
||||
humidity = word(bits[0], bits[1]) * 0.1;
|
||||
// CONVERT AND STORE
|
||||
humidity = word(bits[0], bits[1]) * 0.1;
|
||||
|
||||
if (bits[2] & 0x80) // negative temperature
|
||||
{
|
||||
temperature = -0.1 * word(bits[2] & 0x7F, bits[3]);
|
||||
}
|
||||
else
|
||||
{
|
||||
temperature = 0.1 * word(bits[2], bits[3]);
|
||||
}
|
||||
if (bits[2] & 0x80) // negative temperature
|
||||
{
|
||||
temperature = -0.1 * word(bits[2] & 0x7F, bits[3]);
|
||||
}
|
||||
else
|
||||
{
|
||||
temperature = 0.1 * word(bits[2], bits[3]);
|
||||
}
|
||||
|
||||
// TEST CHECKSUM
|
||||
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
|
||||
if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
|
||||
// TEST CHECKSUM
|
||||
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
|
||||
if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
|
||||
|
||||
return DHTLIB_OK;
|
||||
return DHTLIB_OK;
|
||||
}
|
||||
|
||||
/////////////////////////////////////////////////////
|
||||
@ -114,54 +118,53 @@ int dht::read22(uint8_t pin)
|
||||
// DHTLIB_ERROR_TIMEOUT
|
||||
int dht::read(uint8_t pin)
|
||||
{
|
||||
// INIT BUFFERVAR TO RECEIVE DATA
|
||||
uint8_t cnt = 7;
|
||||
uint8_t idx = 0;
|
||||
// INIT BUFFERVAR TO RECEIVE DATA
|
||||
uint8_t mask = 128;
|
||||
uint8_t idx = 0;
|
||||
|
||||
// EMPTY BUFFER
|
||||
for (uint8_t i=0; i< 5; i++) bits[i] = 0;
|
||||
// EMPTY BUFFER
|
||||
for (uint8_t i=0; i< 5; i++) bits[i] = 0;
|
||||
|
||||
// REQUEST SAMPLE
|
||||
pinMode(pin, OUTPUT);
|
||||
digitalWrite(pin, LOW);
|
||||
delay(20);
|
||||
digitalWrite(pin, HIGH);
|
||||
delayMicroseconds(40);
|
||||
pinMode(pin, INPUT);
|
||||
// REQUEST SAMPLE
|
||||
pinMode(pin, OUTPUT);
|
||||
digitalWrite(pin, LOW);
|
||||
delay(20);
|
||||
digitalWrite(pin, HIGH);
|
||||
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;
|
||||
// GET ACKNOWLEDGE or TIMEOUT
|
||||
unsigned int loopCnt = TIMEOUT;
|
||||
while(digitalRead(pin) == LOW)
|
||||
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
|
||||
|
||||
loopCnt = TIMEOUT;
|
||||
while(digitalRead(pin) == HIGH)
|
||||
if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;
|
||||
loopCnt = TIMEOUT;
|
||||
while(digitalRead(pin) == HIGH)
|
||||
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;
|
||||
// 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;
|
||||
|
||||
unsigned long t = micros();
|
||||
unsigned long t = micros();
|
||||
|
||||
loopCnt = TIMEOUT;
|
||||
while(digitalRead(pin) == HIGH)
|
||||
if (loopCnt-- == 0) return DHTLIB_ERROR_TIMEOUT;
|
||||
loopCnt = TIMEOUT;
|
||||
while(digitalRead(pin) == HIGH)
|
||||
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
|
||||
|
||||
if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
|
||||
if (cnt == 0) // next byte?
|
||||
{
|
||||
cnt = 7;
|
||||
idx++;
|
||||
}
|
||||
else cnt--;
|
||||
}
|
||||
if ((micros() - t) > 40) bits[idx] |= mask;
|
||||
mask >>= 1;
|
||||
if (mask == 0) // next byte?
|
||||
{
|
||||
mask = 128;
|
||||
idx++;
|
||||
}
|
||||
}
|
||||
|
||||
return DHTLIB_OK;
|
||||
return DHTLIB_OK;
|
||||
}
|
||||
//
|
||||
// END OF FILE
|
||||
|
@ -1,13 +1,13 @@
|
||||
//
|
||||
// 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
|
||||
//
|
||||
// HISTORY:
|
||||
// see dht.cpp file
|
||||
//
|
||||
//
|
||||
|
||||
#ifndef dht_h
|
||||
#define dht_h
|
||||
@ -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
|
||||
@ -28,16 +28,16 @@
|
||||
class dht
|
||||
{
|
||||
public:
|
||||
int read11(uint8_t pin);
|
||||
int read11(uint8_t pin);
|
||||
int read21(uint8_t pin);
|
||||
int read22(uint8_t pin);
|
||||
|
||||
double humidity;
|
||||
double temperature;
|
||||
|
||||
double humidity;
|
||||
double temperature;
|
||||
|
||||
private:
|
||||
uint8_t bits[5]; // buffer to receive data
|
||||
int read(uint8_t pin);
|
||||
uint8_t bits[5]; // buffer to receive data
|
||||
int read(uint8_t pin);
|
||||
};
|
||||
#endif
|
||||
//
|
||||
|
@ -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:
|
||||
//
|
||||
@ -17,40 +16,47 @@ dht DHT;
|
||||
|
||||
void setup()
|
||||
{
|
||||
Serial.begin(115200);
|
||||
Serial.println("DHT TEST PROGRAM ");
|
||||
Serial.print("LIBRARY VERSION: ");
|
||||
Serial.println(DHT_LIB_VERSION);
|
||||
Serial.println();
|
||||
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");
|
||||
Serial.begin(115200);
|
||||
Serial.println("DHT TEST PROGRAM ");
|
||||
Serial.print("LIBRARY VERSION: ");
|
||||
Serial.println(DHT_LIB_VERSION);
|
||||
Serial.println();
|
||||
Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)\tTime (us)");
|
||||
}
|
||||
|
||||
void loop()
|
||||
{
|
||||
// READ DATA
|
||||
Serial.print("DHT22, \t");
|
||||
int chk = DHT.read22(DHT22_PIN);
|
||||
switch (chk)
|
||||
{
|
||||
case DHTLIB_OK:
|
||||
Serial.print("OK,\t");
|
||||
break;
|
||||
case DHTLIB_ERROR_CHECKSUM:
|
||||
Serial.print("Checksum error,\t");
|
||||
break;
|
||||
case DHTLIB_ERROR_TIMEOUT:
|
||||
Serial.print("Time out error,\t");
|
||||
break;
|
||||
default:
|
||||
Serial.print("Unknown error,\t");
|
||||
break;
|
||||
}
|
||||
// DISPLAY DATA
|
||||
Serial.print(DHT.humidity, 1);
|
||||
Serial.print(",\t");
|
||||
Serial.println(DHT.temperature, 1);
|
||||
// READ DATA
|
||||
Serial.print("DHT22, \t");
|
||||
|
||||
delay(2000);
|
||||
uint32_t start = micros();
|
||||
int chk = DHT.read22(DHT22_PIN);
|
||||
uint32_t stop = micros();
|
||||
|
||||
switch (chk)
|
||||
{
|
||||
case DHTLIB_OK:
|
||||
Serial.print("OK,\t");
|
||||
break;
|
||||
case DHTLIB_ERROR_CHECKSUM:
|
||||
Serial.print("Checksum error,\t");
|
||||
break;
|
||||
case DHTLIB_ERROR_TIMEOUT:
|
||||
Serial.print("Time out error,\t");
|
||||
break;
|
||||
default:
|
||||
Serial.print("Unknown error,\t");
|
||||
break;
|
||||
}
|
||||
// DISPLAY DATA
|
||||
Serial.print(DHT.humidity, 1);
|
||||
Serial.print(",\t");
|
||||
Serial.print(DHT.temperature, 1);
|
||||
Serial.print(",\t");
|
||||
Serial.print(stop - start);
|
||||
Serial.println();
|
||||
|
||||
delay(2000);
|
||||
}
|
||||
//
|
||||
// END OF FILE
|
||||
|
Loading…
Reference in New Issue
Block a user