標(biāo)題:
DHT11溫濕度arduino程序
[打印本頁]
作者:
小小許有瑾
時間:
2018-4-10 14:59
標(biāo)題:
DHT11溫濕度arduino程序
DHT11溫濕度檢測程序
0.jpg
(23.37 KB, 下載次數(shù): 47)
下載附件
2018-4-10 17:13 上傳
arduino單片機(jī)源程序如下:
#include "dht.h"
#define TIMEOUT 10000
#define pin D2
// return values:
// 0 : OK
// -1 : checksum error
// -2 : timeout
int dht::read11()
{
// READ VALUES
int rv = read();
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()
{
// READ VALUES
int rv = read();
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;
}
// return values:
// 0 : OK
// -2 : timeout
int dht::read()
{
// 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)
……………………
…………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
temperature.rar
(1.77 KB, 下載次數(shù): 24)
2018-4-10 14:58 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者:
碌碌無為
時間:
2018-4-12 15:41
看起來挺高大上的,過來學(xué)習(xí)一下。
作者:
zx360c
時間:
2018-4-24 20:00
樓主能不能單獨(dú)發(fā)一份???thanks。
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1