arduino DHT11 温湿度传感器

dht.cpp


#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;  
}  


dht.h

  
#ifndef dht_h  
#define dht_h  
  
#if ARDUINO < 100  
#include <WProgram.h>  
#else  
#include <Arduino.h>  
#endif  
  
#define DHT_LIB_VERSION "0.1.01"  
  
class dht  
{  
public:  
    int read11(uint8_t pin);  
    int read22(uint8_t pin);  
    double humidity;  
    double temperature;  
  
private:  
    uint8_t bits[5];  // buffer to receive data  
    int read(uint8_t pin);  
};  
#endif  
//  
// END OF FILE  
//  



主要调用文件

#include "dht\dht.h"

#define DHT11_PIN 13
dht DHT;
void setup()
{
	Serial.begin(115200);
	Serial.println("DHT TEST PROGRAM ");
	Serial.print("LIBRARY VERSION: ");
	Serial.println(DHT_LIB_VERSION);
	Serial.println();
	Serial.println("Type,\tstatus,\tHumidity (%),\tTemperature (C)");

}

void loop()
{
	// READ DATA  
 
	int chk = DHT.read11(DHT11_PIN);
	switch (chk)
	{
	case 0:  Serial.print(""); break;
	case -1: Serial.print("Checksum error,\t"); break;
	case -2: Serial.print("Time out error,\t"); break;
	default: Serial.print("Unknown error,\t"); break;
	}
	// DISPLAT DATA  
	Serial.print("humidity:");
	Serial.print(DHT.humidity, 1);
	Serial.print(",\t");
	Serial.print("temperature:");
	Serial.println(DHT.temperature, 1);

	delay(1000);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值