源代码:
目录
主函数
#include "public.h"
#include "lcd1602.h"
#include "dht11.h"
#include <intrins.h>
sbit fft=P2^5;
void main()
{
u8 temp=0,humi=0;
u8 i=0;
u8 temp_buf[3],humi_buf[3];
lcd1602_init();
while(DHT11_Init())
{
lcd1602_show_string(0,0,"Error");
}
while(1)
{
i++;
if(i%200==0)
{
DHT11_Read_Data(&temp,&humi);
if(temp>=30)
{
temp_buf[0]=temp/10+0x30;
temp_buf[1]=temp%10+0x30;
temp_buf[2]='\0';
lcd1602_show_string(6,0,temp_buf);
humi_buf[0]=humi/10+0x30;
humi_buf[1]=humi%10+0x30;
humi_buf[2]='\0';
lcd1602_show_string(6,1,humi_buf);
lcd1602_show_string(0,0,"Temp: *C ZYJW");
lcd1602_show_string(0,1,"Humi: %RH DHS");
fft=0;
delay_ms(50);
fft=0;
delay_ms(1);
}
else
{
temp_buf[0]=temp/10+0x30;
temp_buf[1]=temp%10+0x30;
temp_buf[2]='\0';
lcd1602_show_string(6,0,temp_buf);
humi_buf[0]=humi/10+0x30;
humi_buf[1]=humi%10+0x30;
humi_buf[2]='\0';
lcd1602_show_string(6,1,humi_buf);
lcd1602_show_string(0,0,"Temp: *C ZYBN");
lcd1602_show_string(0,1,"Humi: %RH DHS");
delay_ms(1);
}
}
delay_ms(1);
}
}
DHT11传感器模块
#include "dht11.h"
#include "intrins.h"
/
u8 DHT11_Init(void)
{
DHT11_DQ=1;
DHT11_Rst();
return DHT11_Check();
}
void DHT11_Rst(void)
{
DHT11_DQ=1;
delay_10us(1);
DHT11_DQ=0;
delay_ms(25);
DHT11_DQ=1;
delay_10us(3);
}
u8 DHT11_Check(void)
{
u8 retry=0;
while (!DHT11_DQ&&retry<100)
{
retry++;
_nop_();
};
if(retry>=100)return 1;
else retry=0;
while (DHT11_DQ&&retry<100)
{
retry++;
_nop_();
};
if(retry>=100)return 1;
return 0;
}
u8 DHT11_Read_Byte(void)
{
u8 i,temp;
u8 data_byte=0;
u8 retry=0;
for(i=0;i<8;i++)
{
while (!DHT11_DQ&&retry<50)
{
retry++;
_nop_();
};
retry=0;
delay_10us(3);
temp=0;
if(DHT11_DQ==1)
temp=1;
while (DHT11_DQ&&retry<100)
{
retry++;
_nop_();
};
data_byte<<=1;
data_byte|=temp;
}
return data_byte;
}
u8 DHT11_Read_Data(u8 *temp,u8 *humi)
{
u8 buf[5];
u8 i;
DHT11_Rst();
if(DHT11_Check()==0)
{
for(i=0;i<5;i++)
{
buf[i]=DHT11_Read_Byte();
}
if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4])
{
*humi=buf[0];
*temp=buf[2];
}
}else return 1;
return 0;
}
//其中还包含数据发送段代码如下:
/*--------------------------------------------------------------------------
INTRINS.H
Intrinsic functions for C51.
Copyright (c) 1988-2010 Keil Elektronik GmbH and ARM Germany GmbH
All rights reserved.
--------------------------------------------------------------------------*/
#ifndef __INTRINS_H__
#define __INTRINS_H__
#pragma SAVE
#if defined (__CX2__)
#pragma FUNCTIONS(STATIC)
/* intrinsic functions are reentrant, but need static attribute */
#endif
extern void _nop_ (void);
extern bit _testbit_ (bit);
extern unsigned char _cror_ (unsigned char, unsigned char);
extern unsigned int _iror_ (unsigned int, unsigned char);
extern unsigned long _lror_ (unsigned long, unsigned char);
extern unsigned char _crol_ (unsigned char, unsigned char);
extern unsigned int _irol_ (unsigned int, unsigned char);
extern unsigned long _lrol_ (unsigned long, unsigned char);
extern unsigned char _chkfloat_(float);
#if defined (__CX2__)
extern int abs (int);
extern void _illop_ (void);
#endif
#if !defined (__CX2__)
extern void _push_ (unsigned char _sfr);
extern void _pop_ (unsigned char _sfr);
#endif
#pragma RESTORE
#endif
LCD显示模块
#include "lcd1602.h"
if (LCD1602_4OR8_DATA_INTERFACE==0)
void lcd1602_write_cmd(u8 cmd)
{
LCD1602_RS=0;
LCD1602_RW=0;
LCD1602_E=0;
LCD1602_DATAPORT=cmd;
delay_ms(1);
LCD1602_E=1;
delay_ms(1);
LCD1602_E=0;
}
else
void lcd1602_write_cmd(u8 cmd)
{
LCD1602_RS=0;
LCD1602_RW=0;
LCD1602_E=0;
LCD1602_DATAPORT=cmd;
delay_ms(1);
LCD1602_E=1;
delay_ms(1);
LCD1602_E=0;
LCD1602_DATAPORT=cmd<<4;
delay_ms(1);
LCD1602_E=1;
delay_ms(1);
LCD1602_E=0;
}
endif
if (LCD1602_4OR8_DATA_INTERFACE==0)
void lcd1602_write_data(u8 dat)
{
LCD1602_RS=1;
LCD1602_RW=0;
LCD1602_E=0;
LCD1602_DATAPORT=dat;
delay_ms(1);
LCD1602_E=1;
delay_ms(1);
LCD1602_E=0;
}
else
void lcd1602_write_data(u8 dat)
{
LCD1602_RS=1;
LCD1602_RW=0;
LCD1602_E=0;
LCD1602_DATAPORT=dat;
delay_ms(1);
LCD1602_E=1;
delay_ms(1);
LCD1602_E=0;
LCD1602_DATAPORT=dat<<4;
delay_ms(1);
LCD1602_E=1;
delay_ms(1);
LCD1602_E=0;
}
endif
if (LCD1602_4OR8_DATA_INTERFACE==0)
void lcd1602_init(void)
{
lcd1602_write_cmd(0x38);
lcd1602_write_cmd(0x0c);
lcd1602_write_cmd(0x06);
lcd1602_write_cmd(0x01);
}
else
void lcd1602_init(void)
{
lcd1602_write_cmd(0x28);
lcd1602_write_cmd(0x0c);
lcd1602_write_cmd(0x06);
lcd1602_write_cmd(0x01);
}
endif
void lcd1602_clear(void)
{
lcd1602_write_cmd(0x01);
}
void lcd1602_show_string(u8 x,u8 y,u8 *str)
{
u8 i=0;
if(y>1||x>15)return;
if(y<1)
{
while(*str!='\0')
{
if(i<16-x)
{
lcd1602_write_cmd(0x80+i+x);
}
else
{
lcd1602_write_cmd(0x40+0x80+i+x-16);
}
lcd1602_write_data(*str);
str++;
i++;
}
}
else
{
while(*str!='\0')
{
if(i<16-x)
{
lcd1602_write_cmd(0x80+0x40+i+x);
}
else
{
lcd1602_write_cmd(0x80+i+x-16);
}
lcd1602_write_data(*str);
str++;
i++;
}
}
}
Delay函数
#include "public.h"
void delay_10us(u16 ten_us)
{
while(ten_us--);
}
void delay_ms(u16 ms)
{
u16 i,j;
for(i=ms;i>0;i--)
for(j=110;j>0;j--);
}