Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
DHT11.cpp
- Committer:
- s_inoue_mbed
- Date:
- 2014-09-10
- Revision:
- 8:160047ca45bf
- Parent:
- 7:50f5c8efd967
- Child:
- 9:056d1e9b428c
File content as of revision 8:160047ca45bf:
/*
* Library for the use of the DHT11, a temperature and humidity sensor
* Shigenori Inoue, September 10, 2014
*/
#include "DHT11.h"
// Constructor
DHT11::DHT11(PinName pin) : io(pin, PIN_INPUT, OpenDrain, 1), io_irq(pin)
{
io_irq.rise(this, &DHT11::pos_edge);
io_irq.fall(this, &DHT11::neg_edge);
io_irq.disable_irq();
init();
first_time = true;
}
// Destructor
DHT11::~DHT11(void) {}
// Reading the data bits from the DHT11
int DHT11::readData()
{
// Initialize
init();
// Checking the measurement frequency
if (t.read_ms() < 2000 & first_time == false) {
t.reset();
return TOO_FAST_READ;
}
// Checking the data bus
if (io == 0) {
io.input();
return BUS_BUSY;
}
// Sending start signal, low signal for 20 ms
io.output();
t.reset();
t.start();
do {
io = 0;
} while (t.read_ms() < 20);
io = 1;
io.input();
// Waiting for the start of the response signal
t.reset();
t.start();
do {
if (t.read_us() > 100) {
io.input();
return NOT_PRESENT;
}
} while (io == 1);
// Wainting for the start of the ready signal
t.reset();
t.start();
do {
if (t.read_us() > 100) {
io.input();
return NOT_READY;
}
} while (io == 0);
// Wainting for the end of the ready signal
t.reset();
t.start();
do {} while (io == 1);
// Starting the pulse width sensing
io_irq.enable_irq();
do {
wait_us(100);
if (wdt > 50) {
return WATCHDOG_ERR;
}
wdt++;
} while (eod == false);
// Calculating the check sum
chksum = ((data & 0xff00000000) >> 32)
+ ((data & 0x00ff000000) >> 24)
+ ((data & 0x0000ff0000) >> 16)
+ ((data & 0x000000ff00) >> 8);
if (chksum != (data & 0x00000000ff)) {
return CHKSUM_ERR;
} else {
t.reset();
first_time = false;
return OK;
}
}
// Extracting humidity data from the received data
int DHT11::readHumidity()
{
if (err == OK) {
return (data & 0xff00000000) >> 32;
} else {
return 0xffffffff;
}
}
// Extracting temperature data from the received data
int DHT11::readTemperature()
{
if (err == OK) {
return (data & 0x0000ff0000) >> 16;
} else {
return 0xffffffff;
}
}
// Initialization of variables
void DHT11::init(void)
{
t_pulse_us = 0;
data = 0;
chksum = 0;
cnt = 0;
wdt = 0;
err = OK;
eod = false;
}
void DHT11::pos_edge(void)
{
// Disabling the interruptions
io_irq.disable_irq();
// Initializing the Timer
t.reset();
// Enabling the interruptions
io_irq.enable_irq();
}
void DHT11::neg_edge(void)
{
// Disabling the interruptions
io_irq.disable_irq();
// Reading the positive pulse width
t_pulse_us = t.read_us();
// Detecting 0 if the pulse width ranges from 20 us to 30 us
if (20 <= t_pulse_us && t_pulse_us <= 30) {
// Shifting the data buffer and not adding 1 (because this bit is zero)
data = data << 1;
// Counting up the bits
cnt++;
}
// Detecting 1 if the pulse width ranges from 60 us to 80 us
else if (60 <= t_pulse_us && t_pulse_us <= 80) {
// Shifting the data buffer and adding 1 (because this bit is one)
data = data << 1;
data++;
// Counting up the bits
cnt++;
}
// Detecting the end of Data
if (cnt < 40) {
// Enabling the interruptions
io_irq.enable_irq();
} else {
eod = true;
}
}