极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 9728|回复: 0

多个DHT11的读书数问题

[复制链接]
发表于 2013-4-9 00:15:40 | 显示全部楼层 |阅读模式
求教:我打算用两个DHT11同时测温,程序如下,但始终显示同一个传感器读数,请问怎么改程序会显示两个DHT11的读数,非常感谢您的回答!
#include <dht11.h>
#include<LiquidCrystal.h>
#define DHT11_PIN 1
#define DHT11_PIN 2
byte read_dht11_dat()
{
        byte i = 0;
        byte result=0;
        for(i=0; i< 8; i++){
               
               
             while(!(PINC & _BV(DHT11_PIN)));  // wait for 50us
             delayMicroseconds(30);
               
             if(PINC & _BV(DHT11_PIN))
             result |=(1<<(7-i));
             while((PINC & _BV(DHT11_PIN)));  // wait '1' finish
         
               
        }
        return result;
}
LiquidCrystal lcd(8,9,4,5,6,7);
void setup()
{
  Serial.begin(9600);
  lcd.begin(16,2);
   DDRC |= _BV(DHT11_PIN);
   PORTC |= _BV(DHT11_PIN);
}
dht11 DHT11;
void loop()
{
        byte dht11_dat[5];
        byte dht11_in;
        byte i;
        // start condition
        // 1. pull-down i/o pin from 18ms
        PORTC &= ~_BV(DHT11_PIN);
        delay(18);
        PORTC |= _BV(DHT11_PIN);
        delayMicroseconds(40);
       
        DDRC &= ~_BV(DHT11_PIN);
        delayMicroseconds(40);
       
        dht11_in = PINC & _BV(DHT11_PIN);
       
        if(dht11_in){
                Serial.println("dht11 start condition 1 not met");
                return;
                         }
        delayMicroseconds(80);
       
        dht11_in = PINC & _BV(DHT11_PIN);
       
        if(!dht11_in){
                Serial.println("dht11 start condition 2 not met");
                return;
                           }
        delayMicroseconds(80);
        // now ready for data reception
        for (i=0; i<5; i++)
                dht11_dat[i] = read_dht11_dat();
               
        DDRC |= _BV(DHT11_PIN);
        PORTC |= _BV(DHT11_PIN);
       
        byte dht11_check_sum = dht11_dat[0]+dht11_dat[1]+dht11_dat[2]+dht11_dat[3];
        // check check_sum
        if(dht11_dat[4]!= dht11_check_sum)
        {
                Serial.println("DHT11 checksum error");
        }
   
     lcd.setCursor(0,0);
     lcd.print("H");
     lcd.print(":");
     lcd.print(dht11_dat[0], DEC);
     lcd.print(".");
     lcd.print(dht11_dat[1], DEC);
     lcd.print("%");
     lcd.setCursor(0,1);
      lcd.print("T");
     lcd.print(":");
     lcd.print(dht11_dat[2], DEC);
     lcd.print(".");
     lcd.print(dht11_dat[3], DEC);
     lcd.print("C ");
     lcd.setCursor(9,0);
     lcd.print(dht11_dat[0], DEC);
     lcd.print(".");
     lcd.print(dht11_dat[1], DEC);
     lcd.print("%");
     lcd.setCursor(9,1);
     lcd.print(dht11_dat[2], DEC);
     lcd.print(".");
     lcd.print(dht11_dat[3], DEC);
     lcd.print("C");
    delay(1000);
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-4 10:47 , Processed in 0.036266 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表