mirror of
https://github.com/RobTillaart/Arduino.git
synced 2024-10-03 18:09:02 -04:00
+ 0.1.18 fixed support for DHT11 again
This commit is contained in:
parent
94a48c2283
commit
baa1e15833
@ -1,11 +1,12 @@
|
||||
//
|
||||
// FILE: dht.cpp
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.17
|
||||
// VERSION: 0.1.18
|
||||
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
|
||||
// URL: http://arduino.cc/playground/Main/DHTLib
|
||||
//
|
||||
// HISTORY:
|
||||
// 0.1.18 version 1.16/17 broke the DHT11 - FIXED
|
||||
// 0.1.17 replaced micros() with adaptive loopcount
|
||||
// removed DHTLIB_INVALID_VALUE
|
||||
// added DHTLIB_ERROR_CONNECT
|
||||
@ -43,11 +44,11 @@
|
||||
int dht::read11(uint8_t pin)
|
||||
{
|
||||
// READ VALUES
|
||||
int result = _readSensor(pin, DHTLIB_DHT11_WAKEUP);
|
||||
int result = _readSensor(pin, DHTLIB_DHT11_WAKEUP, DHTLIB_DHT11_LEADING_ZEROS);
|
||||
|
||||
// these bits are always zero, masking them reduces errors.
|
||||
bits[0] &= 0x03;
|
||||
bits[2] &= 0x83;
|
||||
bits[0] &= 0x3F;
|
||||
bits[2] &= 0x3F;
|
||||
|
||||
// CONVERT AND STORE
|
||||
humidity = bits[0]; // bits[1] == 0;
|
||||
@ -66,7 +67,7 @@ int dht::read11(uint8_t pin)
|
||||
int dht::read(uint8_t pin)
|
||||
{
|
||||
// READ VALUES
|
||||
int result = _readSensor(pin, DHTLIB_DHT_WAKEUP);
|
||||
int result = _readSensor(pin, DHTLIB_DHT_WAKEUP, DHTLIB_DHT_LEADING_ZEROS);
|
||||
|
||||
// these bits are always zero, masking them reduces errors.
|
||||
bits[0] &= 0x03;
|
||||
@ -94,12 +95,20 @@ int dht::read(uint8_t pin)
|
||||
// PRIVATE
|
||||
//
|
||||
|
||||
int dht::_readSensor(uint8_t pin, uint8_t wakeupDelay)
|
||||
int dht::_readSensor(uint8_t pin, uint8_t wakeupDelay, uint8_t leadingZeroBits)
|
||||
{
|
||||
// INIT BUFFERVAR TO RECEIVE DATA
|
||||
uint8_t mask = 128;
|
||||
uint8_t idx = 0;
|
||||
|
||||
uint8_t data = 0;
|
||||
uint8_t state = LOW;
|
||||
uint8_t pstate = LOW;
|
||||
uint16_t zeroLoop = DHTLIB_TIMEOUT;
|
||||
uint16_t delta = 0;
|
||||
|
||||
leadingZeroBits = 40 - leadingZeroBits; // reverse counting...
|
||||
|
||||
// replace digitalRead() with Direct Port Reads.
|
||||
// reduces footprint ~100 bytes => portability issue?
|
||||
// direct port read is about 3x faster
|
||||
@ -136,12 +145,7 @@ int dht::_readSensor(uint8_t pin, uint8_t wakeupDelay)
|
||||
if (--loopCount == 0) return DHTLIB_ERROR_ACK_H;
|
||||
}
|
||||
|
||||
uint8_t data = 0;
|
||||
uint8_t state = LOW;
|
||||
uint8_t pstate = LOW;
|
||||
loopCount = DHTLIB_TIMEOUT;
|
||||
uint16_t zeroLoop = DHTLIB_TIMEOUT;
|
||||
uint16_t delta = 0;
|
||||
|
||||
// READ THE OUTPUT - 40 BITS => 5 BYTES
|
||||
for (uint8_t i = 40; i != 0; )
|
||||
@ -150,7 +154,7 @@ int dht::_readSensor(uint8_t pin, uint8_t wakeupDelay)
|
||||
state = (*PIR & bit);
|
||||
if (state == LOW && pstate != LOW)
|
||||
{
|
||||
if (i > 34) // first 6 bits are all zero !!
|
||||
if (i > leadingZeroBits) // DHT22 first 6 bits are all zero !! DHT11 only 1
|
||||
{
|
||||
zeroLoop = min(zeroLoop, loopCount);
|
||||
delta = (DHTLIB_TIMEOUT - zeroLoop)/4;
|
||||
|
@ -1,7 +1,7 @@
|
||||
//
|
||||
// FILE: dht.h
|
||||
// AUTHOR: Rob Tillaart
|
||||
// VERSION: 0.1.17
|
||||
// VERSION: 0.1.18
|
||||
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
|
||||
// URL: http://arduino.cc/playground/Main/DHTLib
|
||||
//
|
||||
@ -19,7 +19,7 @@
|
||||
#include <Arduino.h>
|
||||
#endif
|
||||
|
||||
#define DHT_LIB_VERSION "0.1.17"
|
||||
#define DHT_LIB_VERSION "0.1.18"
|
||||
|
||||
#define DHTLIB_OK 0
|
||||
#define DHTLIB_ERROR_CHECKSUM -1
|
||||
@ -31,6 +31,9 @@
|
||||
#define DHTLIB_DHT11_WAKEUP 18
|
||||
#define DHTLIB_DHT_WAKEUP 1
|
||||
|
||||
#define DHTLIB_DHT11_LEADING_ZEROS 1
|
||||
#define DHTLIB_DHT_LEADING_ZEROS 6
|
||||
|
||||
// max timeout is 100 usec.
|
||||
// For a 16 Mhz proc 100 usec is 1600 clock cycles
|
||||
// loops using DHTLIB_TIMEOUT use at least 4 clock cycli
|
||||
@ -61,7 +64,7 @@ public:
|
||||
|
||||
private:
|
||||
uint8_t bits[5]; // buffer to receive data
|
||||
int _readSensor(uint8_t pin, uint8_t wakeupDelay);
|
||||
int _readSensor(uint8_t pin, uint8_t wakeupDelay, uint8_t leadingZeroBits);
|
||||
};
|
||||
#endif
|
||||
//
|
||||
|
@ -17,13 +17,12 @@ Digistump Digix @ 84 MHz
|
||||
More information - http://playground.arduino.cc//Main/DHTLib -
|
||||
|
||||
Notes:
|
||||
version 0.1.13 is the last stable version for AVR and ARM
|
||||
version 0.1.13 is the last stable version for both AVR and ARM
|
||||
|
||||
version 0.1.14 and up are not compatible anymore with pre 1.0 Arduino
|
||||
version 0.1.14 and up have breaking changes wrt ARM based arduino's e.g DUE.
|
||||
|
||||
version 0.1.15 is stable version for AVR only
|
||||
|
||||
version 0.1.16 and 0.1.17 have breaking changes for DHT11
|
||||
version 0.1.18 works again for DHT11 (avr only)
|
||||
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user