實(shí)現(xiàn)功能:
1、對(duì)溫度、濕度進(jìn)行采集
2、可以警報(bào)提示、修改溫度、濕度上下限值
3、lcd顯示相關(guān)信息
原理圖:
部分程序:
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
#include <intrins.h>
typedef bit BOOL? ;
sbit io = P1^1 ;//(口線(xiàn)定義)
sbit bee=P1^0;
sbit rs = P2^5 ;
sbit rw = P2^6 ;
sbit ep = P2^7 ;
bit flag_300ms ;
sbit K1=P1^2;
sbit K2=P1^3;
sbit K3=P1^4;
uchar yemian=0;//(定義變量)
uchar flat=1;
uchar data_byte;
uchar RH,RL,TH,TL;
uchar WenDu,ShiDu;//報(bào)警的溫度和濕度
#include "eeprom52.h"
/****************************延時(shí)程序****************************/
void delay(uchar ms)
{?????? // 延時(shí)子程序
uchar i ;
while(ms--)
{
for(i = 0 ; i<250;i++) ;
}
}
void delay1()//延時(shí)10us
{
uchar i;
i--;
i--;
i--;
i--;
i--;
i--;
}
void longdelay(uchar s) //長(zhǎng)延時(shí)
{
while(s--)
{
delay(20) ;
}
}
/******************把數(shù)據(jù)保存到單片機(jī)內(nèi)部eeprom中******************/
void write_eeprom()
{
SectorErase(0x2000);
byte_write(0x2000, WenDu);
byte_write(0x2001, ShiDu);
byte_write(0x2060, a_a);
}
/******************把數(shù)據(jù)從單片機(jī)內(nèi)部eeprom中讀出來(lái)*****************/
void read_eeprom()
{
WenDu?? = byte_read(0x2000);
ShiDu = byte_read(0x2001);
a_a????? = byte_read(0x2060);
}
/**************開(kāi)機(jī)自檢eeprom初始化*****************/
void init_eeprom()
{
read_eeprom();?????????????? //先讀
if(a_a != 2)?????????????? //新的單片機(jī)初始單片機(jī)內(nèi)問(wèn)eeprom
{
WenDu?? = 30;
ShiDu?? = 80;
a_a = 2;
write_eeprom();
}
}
/********************報(bào)警程序部分*******************/
void warn()? //報(bào)警函數(shù)
{
if(TH>=WenDu)? //檢測(cè)的溫度高于設(shè)定溫度報(bào)警值
{
bee=0; //報(bào)警
}
if(RH>=ShiDu)? //檢測(cè)的濕度高于設(shè)定的濕度值
{
bee=0; //報(bào)警
}
if( TH<WenDu? &&? RH<ShiDu )
{
bee=1; //停止報(bào)警
}
}
/************************LCD模塊******************************************/
BOOL lcd_bz()//測(cè)試LCD忙碌狀態(tài)
{
BOOL result ;
rs = 0 ;
rw = 1 ;
ep = 1 ;
result = (BOOL)(P0 & 0x80) ;
ep = 0 ;
return result ;
}
void write_cmd(uchar cmd)// 寫(xiě)指令
{
while(lcd_bz()) ;
rs = 0 ;
rw = 0 ;
ep = 0 ;
P0 = cmd ;
ep = 1 ;
ep = 0 ;
}
void write_addr(uchar addr)//寫(xiě)地址
{
write_cmd(addr|0x80) ;
}
void write_byte(uchar dat)//寫(xiě)字節(jié)
{
while(lcd_bz()) ;
rs = 1 ;
rw = 0 ;
ep = 0 ;
P0 = dat ;
ep = 1 ;
ep = 0 ;
}
void lcd_init()// 初始化
{
write_cmd(0x38) ;
delay(1);
write_cmd(0x08) ;
delay(1);
write_cmd(0x01) ;
delay(1);
write_cmd(0x06) ;
delay(1);
write_cmd(0x0c) ;
delay(1);
}
void display(uchar addr, uchar q)//在某一地址上顯示一字節(jié)
{
delay(1) ;
write_addr(addr) ;
write_byte(q) ;
delay(1) ;
}
/*********************** DHT11測(cè)試模塊***************************************/
void start()//開(kāi)始信號(hào)
{
io=1;
delay1();
io=0;
delay(20);//>18ms
io=1;
delay1();//20-40us
delay1();
delay1();
delay1();
delay1();
}
uchar receive_byte()//接收一個(gè)字節(jié)
{
uchar i,temp,count;
for(i=0;i<8;i++)
{
temp=0;
delay1();delay1();delay1();delay1();
if(io==1)temp=1;
count=2;
while((io)&&count++);
if(count==1)break;
data_byte<<=1;
data_byte|=temp;
}
return data_byte;
}
void receive()//接收數(shù)據(jù)
{
uchar T_H,T_L,R_H,R_L,check,num_check;
uchar count;
start();//開(kāi)始信號(hào)
io=1;
if(!io)//讀取DHT11響應(yīng)信號(hào)
{
while((io)&&count++);
R_H=receive_byte();
R_L=receive_byte();
T_H=receive_byte();
T_L=receive_byte();
check=receive_byte();
io=0;//拉低延時(shí)50us
delay1();delay1();delay1();delay1();delay1();
io=1;
num_check=R_H+R_L+T_H+T_L;
if(num_check=check)
{
RH=R_H;
RL=R_L;
TH=T_H;
TL=T_L;
check=num_check;
}
}
}