DHT22 与 8051 接口

问题描述 投票:0回答:1

我尝试以 4 位模式将 dht22 传感器与 8051 微控制器连接起来。但是我无法获得正确的温度和湿度读数。有人请帮我写代码。

我正在获取温度和湿度读数 嗡嗡声= 2.88% 温度=1.0C 91

请帮助我获得正确的温度和代码读数。我已经包含了 dht22 传感器和 LCD 的代码。

我的密码是:

#include<reg51.h>
#include<stdio.h>
#include<string.h>
#include<intrins.h>
#include <stdlib.h>
#include "LCD16x2_4bit.h"

sbit DHT22=P2^1;        /* Connect DHT22 output Pin to P2.1 Pin */
int I_RH,D_RH,I_Temp,D_Temp,CheckSum; 

void timer_delay20us()
{
    _nop_();_nop_();_nop_();_nop_();_nop_();                                     
    _nop_();_nop_();_nop_();_nop_();_nop_();                                        
    _nop_();_nop_();_nop_();_nop_();_nop_();                                       
    _nop_();_nop_();_nop_();_nop_();_nop_();                                        
                                           
}

void Request()          /* Microcontroller send request */
{
    DHT22 = 0;      /* set to low pin */
    delay(1);   /* wait for 1ms */
    DHT22 = 1;      /* set to high pin */
}

void Response()         /* Receive response from DHT22 */
{
    while(DHT22==1);
    while(DHT22==0);
    while(DHT22==1);
}

int Receive_data()      /* Receive data */
{
    int q,c=0;  
    for (q=0; q<8; q++)
    {
        while(DHT22==0);/* check received bit 0 or 1 */
        timer_delay20us();    //delay 20us
        if(DHT22 == 1)  /* If high pulse is greater than 28us */
        c = (c<<1)|(0x01);/* Then its logic HIGH */
        else
        c = (c<<1);  /* otherwise its logic LOW */
        while(DHT22==1);                                     
    }
    return c;
}

void main()
    unsigned char dat[20];
    LCD_Init();     // initialize LCD 
    
    while(1)
    {       
        Request();  // send start pulse
        Response(); // receive response
        
        I_RH=Receive_data();    // store first eight bit in I_RH
        D_RH=Receive_data();    // store next eight bit in D_RH
        I_Temp=Receive_data();  // store next eight bit in I_Temp
        D_Temp=Receive_data();  // store next eight bit in D_Temp
        CheckSum=Receive_data();// store next eight bit in CheckSum

        if ((I_RH + D_RH + I_Temp + D_Temp) != CheckSum)
        {
            LCD_String_xy(0,0,"Error");
        }
        
        else
        {

            sprintf(dat,"Hum = %d.%d",I_RH,D_RH);
            LCD_String_xy(0,0,dat);
            LCD_String("%");
            sprintf(dat,"Temp = %d.%d",I_Temp,D_Temp);   
            LCD_String_xy(1,0,dat);
            LCD_Char(0xDF); // to print 0 (degree) in LCD 
            LCD_String("C");
            sprintf(dat,"%d   ",CheckSum);
            LCD_String_xy(1,13,dat);
        }       
        delay(3000);
    }

} 


/*  
   LCD16x2 4 bit 8051 interface
*/


#include<reg51.h>
#include<LCD16x2_4bit.h>

sfr LCD_Port=0x90;                          /* P1 port as data port */
sbit rs=P1^0;                               /* Register select pin */
sbit rw=P1^1;                               /* Read/Write pin */
sbit en=P1^2;                               /* Enable pin */

/* Function to provide delay Approx 1ms with 11.0592 Mhz crystal*/
void delay(unsigned int count)          
{
    int i,j;
    for(i=0;i<count;i++)
    for(j=0;j<112;j++);
}

void LCD_Command (char cmnd)    /* LCD16x2 command funtion */
{
    LCD_Port =(LCD_Port & 0x0F) | (cmnd & 0xF0); /* sending upper nibble */
    rs=0;                                           /* command reg. */
    rw=0;                                           /* Write operation */
    en=1; 
    delay(1);
    en=0;
    delay(2);

    LCD_Port = (LCD_Port & 0x0F) | (cmnd << 4);  /* sending lower nibble */
    en=1;                                           /* enable pulse */
    delay(1);
    en=0;
    delay(5);
}

void LCD_Char (char char_data)  /* LCD data write function */
{
    LCD_Port =(LCD_Port & 0x0F) | (char_data & 0xF0); /* sending upper nibble */    
    rs=1;                                               /*Data reg.*/
    rw=0;                                               /*Write operation*/
    en=1;                   
    delay(1);
    en=0;
    delay(2);

    LCD_Port = (LCD_Port & 0x0F) | (char_data << 4);  /* sending lower nibble */
    en=1;                                           /* enable pulse */
    delay(1);
    en=0;
    delay(5);
}

void LCD_String (char *str)         /* Send string to LCD function */
{
    int i;
    for(i=0;str[i]!=0;i++)              /* Send each char of string till the NULL */
    {
        LCD_Char (str[i]);                  /* Call LCD data write */
    }
}

void LCD_String_xy (char row, char pos, char *str)  /* Send string to LCD function */
{
    if (row == 0)
    LCD_Command((pos & 0x0F)|0x80); /* Command of first row and required position<16 */
    else if (row == 1)
    LCD_Command((pos & 0x0F)|0xC0); /* Command of first row and required position<16 */
    LCD_String(str);                                /* Call LCD string function */
}

void LCD_Init (void)                            /* LCD Initialize function */
{
    delay(20);                  /* LCD Power ON Initialization time >15ms */
    
    LCD_Command (0x02);             /* 4bit mode */
    LCD_Command (0x28);             /* Initialization of 16X2 LCD in 4bit mode */
    LCD_Command(0x0C);              /* Display ON Cursor OFF */
    LCD_Command (0x06);             /* Auto Increment cursor */
    LCD_Command (0x01);             /* clear display */
    delay(3);
    LCD_Command (0x80);             /* cursor at home position */
}

/*
 * LCD16x2 4 bit 8051 interface
*/

#ifndef LCD16x2_4bit
#define LCD16x2_4bit

#include<reg51.h>

void delay(unsigned int count);
void LCD_Command (char cmnd);
void LCD_Char(char char_data);
void LCD_String(char *str);
void LCD_str(unsigned char *, unsigned char);   
void LCD_String_xy (char row, char pos, char *str);
void LCD_Init (void);
void delay_ms(unsigned int);

#endif
c sensors lcd 8051
1个回答
1
投票

你的程序可以正常运行DHT11;并且该程序不适用于 DHT22。原因是DHT11和DHT22的数据格式不同。

如果我们不考虑符号位,真实温度等于

(I_Temp\*256 + D_Temp)/10
,真实湿度等于
(I_Temp\*256+D_Temp)/10
。稍作修改即可解决您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.