+ finalized support for DHT21

+ made TIMEOUT depends on clock ==> F_CPU
This commit is contained in:
Rob Tillaart 2013-09-29 12:35:28 +02:00
parent 9593136bda
commit c7c5bdf661
2 changed files with 5 additions and 26 deletions

View File

@ -22,7 +22,7 @@
#include "dht.h"
#define TIMEOUT 10000
#define TIMEOUT (F_CPU/1600) // unsigned int in code, for higher CPU speeds this might exceed MAXINT.
/////////////////////////////////////////////////////
//
@ -62,29 +62,7 @@ int dht::read11(uint8_t pin)
// DHTLIB_ERROR_TIMEOUT
int dht::read21(uint8_t pin)
{
// 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
}
// CONVERT AND STORE
humidity = bits[0] + bits[1] * 0.00390625; // == 1/256.0
temperature = (bits[2] & 0x7F) + bits[3] * 0.00390625;
if (bits[2] & 0x80) // negative temperature
{
temperature = -temperature;
}
// 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 read22(pin); // dataformat identical to DHT22
}
// return values:

View File

@ -13,7 +13,8 @@
dht DHT;
#define DHT11_PIN 4
#define DHT22_PIN 5
#define DHT21_PIN 5
#define DHT22_PIN 6
void setup()
{
@ -55,7 +56,7 @@ void loop()
// READ DATA
Serial.print("DHT21, \t");
int chk = DHT.read21(DHT21_PIN);
chk = DHT.read21(DHT21_PIN);
switch (chk)
{
case DHTLIB_OK: