加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
dht.cpp 2.41 KB
一键复制 编辑 原始数据 按行查看 历史
root 提交于 2021-03-18 16:05 . new projest
//
// FILE: dht22.cpp
// VERSION: 0.1.00
// PURPOSE: DHT22 Temperature & Humidity Sensor library for Arduino
//
// DATASHEET:
//
// HISTORY:
// 0.1.0 by Rob Tillaart (01/04/2011)
// inspired by DHT11 library
//
#include "dht.h"
#define TIMEOUT 10000
/////////////////////////////////////////////////////
//
// PUBLIC
//
// return values:
// 0 : OK
// -1 : checksum error
// -2 : timeout
int dht::read11(uint8_t pin)
{
// READ VALUES
int rv = read(pin);
if (rv != 0) return rv;
// CONVERT AND STORE
humidity = bits[0]; // bit[1] == 0;
temperature = bits[2]; // bits[3] == 0;
// TEST CHECKSUM
uint8_t sum = bits[0] + bits[2]; // bits[1] && bits[3] both 0
if (bits[4] != sum) return -1;
return 0;
}
// return values:
// 0 : OK
// -1 : checksum error
// -2 : timeout
int dht::read22(uint8_t pin)
{
// READ VALUES
int rv = read(pin);
if (rv != 0) return rv;
// CONVERT AND STORE
humidity = word(bits[0], bits[1]) * 0.1;
int sign = 1;
if (bits[2] & 0x80) // negative temperature
{
bits[2] = bits[2] & 0x7F;
sign = -1;
}
temperature = sign * word(bits[2], bits[3]) * 0.1;
// TEST CHECKSUM
uint8_t sum = bits[0] + bits[1] + bits[2] + bits[3];
if (bits[4] != sum) return -1;
return 0;
}
/////////////////////////////////////////////////////
//
// PRIVATE
//
// return values:
// 0 : OK
// -2 : timeout
int dht::read(uint8_t pin)
{
// INIT BUFFERVAR TO RECEIVE DATA
uint8_t cnt = 7;
uint8_t idx = 0;
// EMPTY BUFFER
for (int 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);
// GET ACKNOWLEDGE or TIMEOUT
unsigned int loopCnt = TIMEOUT;
while(digitalRead(pin) == LOW)
if (loopCnt-- == 0) return -2;
loopCnt = TIMEOUT;
while(digitalRead(pin) == HIGH)
if (loopCnt-- == 0) return -2;
// READ THE OUTPUT - 40 BITS => 5 BYTES
for (int i=0; i<40; i++)
{
loopCnt = TIMEOUT;
while(digitalRead(pin) == LOW)
if (loopCnt-- == 0) return -2;
unsigned long t = micros();
loopCnt = TIMEOUT;
while(digitalRead(pin) == HIGH)
if (loopCnt-- == 0) return -2;
if ((micros() - t) > 40) bits[idx] |= (1 << cnt);
if (cnt == 0) // next byte?
{
cnt = 7;
idx++;
}
else cnt--;
}
return 0;
}
//
// END OF FILE
//
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化