大神帮忙看看DHT11程序哪错了
时间:10-02
整理:3721RD
点击:
#include <reg52.h>
#include <intrins.h>
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
#define lcd_data P0
sbit rs=P2^6;
sbit rw=P2^5;
sbit e=P2^7;
sbit DHT=P3^2;
uchar str1[] = {"Humi:"};
uchar temp,humi;
void delay();
void delay10us();
void delay1ms();
void delay10ms();
void Lcd_Init();
void Lcd_write_com();
void Lcd_write_data();
void Lcd_Display();
uchar DHT11_Init();
uchar DHT11_Read_Bit();
uchar DHT11_Read_Byte();
uchar DHT11_Read_Data();
void delay(uint i)
{
while(i--);
}
void delay10us(void)
{
unsigned char a,b;
for(b=1;b>0;b--)
for(a=2;a>0;a--);
}
void delay1ms(void)
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=142;b>0;b--)
for(a=2;a>0;a--);
}
void delay10ms(void)
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
}
void LCD_write_com(uchar com)
{
rs=0;
rw=0;
e=0;
P0=com;
delay(5);
e=1;
delay(5);
e=0;
}
void LCD_write_data(uchar dat)
{
rs=1;
rw=0;
e=0;
P0=dat;
delay(5);
e=1;
delay(5);
e=0;
}
void Lcd_Init()
{
LCD_write_com(0x38);
LCD_write_com(0x0c);
LCD_write_com(0x06);
LCD_write_com(0x01);
}
void LCD_Display()
{
uchar i;
LCD_write_com(0x00+0x80);
for(i=0;i<5;i++)
{
LCD_write_data(str1);
}
}
uchar DHT11_Init()
{
uchar flag;
DHT=1;
_nop_();
DHT=0;
delay10ms();
delay10ms();
DHT=1;
delay10us();
delay10us();
delay10us();
while(DHT&&flag++);
if(flag>=100)return 1;
else flag=0;
while(!DHT&&flag++);
if(flag>=100)return 1;
else return 0;
}
uchar DHT11_Read_Bit(void)
{
uchar flag;
while(DHT&&flag++);
flag=0;
while(!DHT&&flag++);
delay10us();
delay10us();
delay10us();
delay10us();
if(DHT)return 1;
else return 0;
}
uchar DHT11_Read_Byte(void)
{
uchar i,dat=0;
for (i=0;i<8;i++)
{
dat<<=1;
dat|=DHT11_Read_Bit();
}
return dat;
}
uchar DHT11_Read_Data(uchar *temp,uchar *humi)
{
uchar dat[5];
uchar i;
DHT11_Init();
if(DHT11_Init()==0)
{
for(i=0;i<5;i++)
{
dat=DHT11_Read_Byte();
}
if((dat[0]+dat[1]+dat[2]+dat[3])==dat[4])
{
*humi=dat[0];
*temp=dat[2];
}
}
else return 1;
return 0;
}
void datapros()
{
uint humi_buf[2];
DHT11_Read_Data(&temp,&humi);
humi_buf[0]=humi/10+0x30;
humi_buf[1]=humi%10+0x30;
LCD_write_com(0x06+0x80);
LCD_write_data(humi_buf[0]);
LCD_write_com(0x07+0x80);
LCD_write_data(humi_buf[1]);
LCD_write_com(0x08+0x80);
LCD_write_data('%');
}
void main()
{
Lcd_Init();
delay10us();
while(1)
{
datapros();
LCD_Display();
}
}
#include <intrins.h>
#include<string.h>
#define uchar unsigned char
#define uint unsigned int
#define lcd_data P0
sbit rs=P2^6;
sbit rw=P2^5;
sbit e=P2^7;
sbit DHT=P3^2;
uchar str1[] = {"Humi:"};
uchar temp,humi;
void delay();
void delay10us();
void delay1ms();
void delay10ms();
void Lcd_Init();
void Lcd_write_com();
void Lcd_write_data();
void Lcd_Display();
uchar DHT11_Init();
uchar DHT11_Read_Bit();
uchar DHT11_Read_Byte();
uchar DHT11_Read_Data();
void delay(uint i)
{
while(i--);
}
void delay10us(void)
{
unsigned char a,b;
for(b=1;b>0;b--)
for(a=2;a>0;a--);
}
void delay1ms(void)
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=142;b>0;b--)
for(a=2;a>0;a--);
}
void delay10ms(void)
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
}
void LCD_write_com(uchar com)
{
rs=0;
rw=0;
e=0;
P0=com;
delay(5);
e=1;
delay(5);
e=0;
}
void LCD_write_data(uchar dat)
{
rs=1;
rw=0;
e=0;
P0=dat;
delay(5);
e=1;
delay(5);
e=0;
}
void Lcd_Init()
{
LCD_write_com(0x38);
LCD_write_com(0x0c);
LCD_write_com(0x06);
LCD_write_com(0x01);
}
void LCD_Display()
{
uchar i;
LCD_write_com(0x00+0x80);
for(i=0;i<5;i++)
{
LCD_write_data(str1);
}
}
uchar DHT11_Init()
{
uchar flag;
DHT=1;
_nop_();
DHT=0;
delay10ms();
delay10ms();
DHT=1;
delay10us();
delay10us();
delay10us();
while(DHT&&flag++);
if(flag>=100)return 1;
else flag=0;
while(!DHT&&flag++);
if(flag>=100)return 1;
else return 0;
}
uchar DHT11_Read_Bit(void)
{
uchar flag;
while(DHT&&flag++);
flag=0;
while(!DHT&&flag++);
delay10us();
delay10us();
delay10us();
delay10us();
if(DHT)return 1;
else return 0;
}
uchar DHT11_Read_Byte(void)
{
uchar i,dat=0;
for (i=0;i<8;i++)
{
dat<<=1;
dat|=DHT11_Read_Bit();
}
return dat;
}
uchar DHT11_Read_Data(uchar *temp,uchar *humi)
{
uchar dat[5];
uchar i;
DHT11_Init();
if(DHT11_Init()==0)
{
for(i=0;i<5;i++)
{
dat=DHT11_Read_Byte();
}
if((dat[0]+dat[1]+dat[2]+dat[3])==dat[4])
{
*humi=dat[0];
*temp=dat[2];
}
}
else return 1;
return 0;
}
void datapros()
{
uint humi_buf[2];
DHT11_Read_Data(&temp,&humi);
humi_buf[0]=humi/10+0x30;
humi_buf[1]=humi%10+0x30;
LCD_write_com(0x06+0x80);
LCD_write_data(humi_buf[0]);
LCD_write_com(0x07+0x80);
LCD_write_data(humi_buf[1]);
LCD_write_com(0x08+0x80);
LCD_write_data('%');
}
void main()
{
Lcd_Init();
delay10us();
while(1)
{
datapros();
LCD_Display();
}
}