GY-63_MS5611/libraries/DHTlib/dht.cpp

179 lines
4.2 KiB
C++
Raw Normal View History

2013-08-28 07:57:45 -04:00
//
// FILE: dht.cpp
// AUTHOR: Rob Tillaart
2014-06-01 03:48:06 -04:00
// VERSION: 0.1.10
2013-08-28 07:57:45 -04:00
// PURPOSE: DHT Temperature & Humidity Sensor library for Arduino
// URL: http://arduino.cc/playground/Main/DHTLib
//
// HISTORY:
2014-06-01 03:48:06 -04:00
// 0.1.10 optimized faster WAKEUP + TIMEOUT
// 0.1.09 optimize size: timeout check + use of mask
// 0.1.08 added formula for timeout based upon clockspeed
2013-08-28 07:57:45 -04:00
// 0.1.07 added support for DHT21
// 0.1.06 minimize footprint (2012-12-27)
// 0.1.05 fixed negative temperature bug (thanks to Roseman)
// 0.1.04 improved readability of code using DHTLIB_OK in code
// 0.1.03 added error values for temp and humidity when read failed
// 0.1.02 added error codes
// 0.1.01 added support for Arduino 1.0, fixed typos (31/12/2011)
// 0.1.0 by Rob Tillaart (01/04/2011)
//
// inspired by DHT11 library
//
// Released to the public domain
//
#include "dht.h"
2014-06-01 03:48:06 -04:00
// max timeout is 100usec.
// For a 16Mhz proc that is max 1600 clock cycles
// loops using TIMEOUT use at least 4 clock cycli
// so 100 us takes max 400 loops
// so by dividing F_CPU by 40000 we "fail" as fast as possible
#define TIMEOUT (F_CPU/40000)
2013-08-28 07:57:45 -04:00
/////////////////////////////////////////////////////
//
// PUBLIC
//
// return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
int dht::read11(uint8_t pin)
{
// READ VALUES
2014-06-01 03:48:06 -04:00
int rv = read(pin, DHTLIB_DHT11_WAKEUP);
if (rv != DHTLIB_OK)
2013-08-28 07:57:45 -04:00
{
humidity = DHTLIB_INVALID_VALUE; // invalid value, or is NaN prefered?
temperature = DHTLIB_INVALID_VALUE; // invalid value
return rv;
}
2013-08-28 07:57:45 -04:00
// CONVERT AND STORE
humidity = bits[0]; // bits[1] == 0;
temperature = bits[2]; // bits[3] == 0;
2013-08-28 07:57:45 -04:00
// TEST CHECKSUM
// bits[1] && bits[3] both 0
uint8_t sum = bits[0] + bits[2];
if (bits[4] != sum) return DHTLIB_ERROR_CHECKSUM;
2013-08-28 07:57:45 -04:00
return DHTLIB_OK;
2013-08-28 07:57:45 -04:00
}
// return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
int dht::read21(uint8_t pin)
{
2014-06-01 03:48:06 -04:00
// dataformat & wakeup identical to DHT22
return read22(pin);
2013-08-28 07:57:45 -04:00
}
// return values:
// DHTLIB_OK
// DHTLIB_ERROR_CHECKSUM
// DHTLIB_ERROR_TIMEOUT
int dht::read22(uint8_t pin)
{
// READ VALUES
2014-06-01 03:48:06 -04:00
int rv = read(pin, DHTLIB_DHT22_WAKEUP);
if (rv != DHTLIB_OK)
2013-08-28 07:57:45 -04:00
{
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;
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;
return DHTLIB_OK;
2013-08-28 07:57:45 -04:00
}
/////////////////////////////////////////////////////
//
// PRIVATE
//
// return values:
// DHTLIB_OK
// DHTLIB_ERROR_TIMEOUT
2014-06-01 03:48:06 -04:00
int dht::read(uint8_t pin, uint8_t wakeupDelay)
2013-08-28 07:57:45 -04:00
{
// INIT BUFFERVAR TO RECEIVE DATA
uint8_t mask = 128;
uint8_t idx = 0;
// EMPTY BUFFER
2014-06-01 03:48:06 -04:00
for (uint8_t i = 0; i < 5; i++) bits[i] = 0;
// REQUEST SAMPLE
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
2014-06-01 03:48:06 -04:00
delay(wakeupDelay);
digitalWrite(pin, HIGH);
delayMicroseconds(40);
pinMode(pin, INPUT);
// GET ACKNOWLEDGE or TIMEOUT
unsigned int loopCnt = TIMEOUT;
while(digitalRead(pin) == LOW)
2014-06-01 03:48:06 -04:00
{
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
}
loopCnt = TIMEOUT;
while(digitalRead(pin) == HIGH)
2014-06-01 03:48:06 -04:00
{
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
}
// READ THE OUTPUT - 40 BITS => 5 BYTES
2014-06-01 03:48:06 -04:00
for (uint8_t i = 0; i < 40; i++)
{
loopCnt = TIMEOUT;
while(digitalRead(pin) == LOW)
2014-06-01 03:48:06 -04:00
{
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
}
unsigned long t = micros();
loopCnt = TIMEOUT;
while(digitalRead(pin) == HIGH)
2014-06-01 03:48:06 -04:00
{
if (--loopCnt == 0) return DHTLIB_ERROR_TIMEOUT;
}
if ((micros() - t) > 40) bits[idx] |= mask;
mask >>= 1;
if (mask == 0) // next byte?
{
mask = 128;
idx++;
}
}
return DHTLIB_OK;
2013-08-28 07:57:45 -04:00
}
//
// END OF FILE
//