控制/MCU
实现功能:
1、对温度、湿度进行采集2、可以警报提示、修改温度、湿度上下限值3、lcd显示相关信息
原理图:
部分程序:
#include
#define uint unsigned int
#define uchar unsigned char
#include
typedef bit BOOL ;
sbit io = P1^1 ;//(口线定义)
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;//报警的温度和湿度
#include "eeprom52.h"
/ 延时程序 /
void delay(uchar ms)
{ // 延时子程序
uchar i ;
while(ms--)
{
for(i = 0 ; i<250;i++) ;
}
}
void delay1()//延时10us
{
uchar i;
i--;
i--;
i--;
i--;
i--;
i--;
}
void longdelay(uchar s) //长延时
{
while(s--)
{
delay(20) ;
}
}
/ 把数据保存到单片机内部eeprom中 /
void write_eeprom()
{
SectorErase(0x2000);
byte_write(0x2000, WenDu);
byte_write(0x2001, ShiDu);
byte_write(0x2060, a_a);
}
/* 把数据从单片机内部eeprom中读出来 /
void read_eeprom()
{
WenDu = byte_read(0x2000);
ShiDu = byte_read(0x2001);
a_a = byte_read(0x2060);
}
/ 开机自检eeprom初始化 ***/
void init_eeprom()
{
read_eeprom(); //先读
if(a_a != 2) //新的单片机初始单片机内问eeprom
{
WenDu = 30;
ShiDu = 80;
a_a = 2;
write_eeprom();
}
}
if( TH
{
bee=1; //停止报警
}
}
/ LCD模块 ******************/
BOOL lcd_bz()//测试LCD忙碌状态
rs = 0 ;
rw = 0 ;
ep = 0 ;
P0 = cmd ;
ep = 1 ;
ep = 0 ;
}
void write_addr(uchar addr)//写地址
{
write_cmd(addr|0x80) ;
}
void write_byte(uchar dat)//写字节
{
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);
}
data_byte<<=1;
data_byte|=temp;
}
return data_byte;
}
void receive()//接收数据
{
uchar T_H,T_L,R_H,R_L,check,num_check;
uchar count;
start();//开始信号
io=1;
if(!io)//读取DHT11响应信号
{
while((io)&&count++);
R_H=receive_byte();
R_L=receive_byte();
T_H=receive_byte();
T_L=receive_byte();
check=receive_byte();
io=0;//拉低延时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;
}
}
}
全部0条评论
快来发表一下你的评论吧 !