#include <reg52.h> //调用单片机头文件
#define uchar unsigned char //无符号字符型 宏定义 变量范围0~255
#define uint unsigned int //无符号整型 宏定义 变量范围0~65535
#include<intrins.h>
#include "eeprom52.h"
sbit beep = P1^4; //蜂鸣器IO口定义
uchar a_a;
bit flag_300ms ;
// 按键的IO变量的定义
uchar key_can; //按键值的变量
uchar menu_shudu = 20; //用来控制连加的速度
uchar menu_1; //菜单设计的变量
uint t_high = 30,t_low = 10; //温度报警参数
uint s_high = 30,s_low = 10; //湿度报警参数
uchar flag_w_bj_en,flag_s_bj_en;
bit flag_lj_en; //按键连加使能
bit flag_lj_3_en; //按键连3次连加后使能 加的数就越大了
uchar key_time,flag_value; //用做连加的中间变量
bit key_500ms ;
uchar flag_clock; //温度报警变量
uchar flag_fuzi; //用做菜单内的初始化的
/***************************************************************
* 名称 : delay_1ms()
* 功能 : 延时1ms函数
* 输入 : q
* 输出 : 无
****************************************************************/
void delay_1ms(uint q)
{
uint i,j;
for(i=0;i<q;i++)
for(j=0;j<120;j++);
}
sbit dht11=P1^5; //温度传感器IO口定义
uchar table_dht11[5]={1,2,3,4,5};
/******************把数据保存到单片机内部eeprom中******************/
void write_eeprom() //保存数据
{
SectorErase(0x2000);
byte_write(0x2000, t_high);
byte_write(0x2001, t_low);
byte_write(0x2002, s_high);
byte_write(0x2003, s_low);
byte_write(0x2055, a_a);
}
/******************把数据从单片机内部eeprom中读出来*****************/
void read_eeprom() //读出保存数据
{
t_high = byte_read(0x2000);
t_low = byte_read(0x2001);
s_high = byte_read(0x2002);
s_low = byte_read(0x2003);
a_a = byte_read(0x2055);
}
///**************开机自检eeprom初始化*****************/
void init_eeprom() ////开始初始化保存的数据
{
read_eeprom(); //读出保存数据
if(a_a != 2) //新的单片机初始单片机内问eeprom
{
t_high = 40,t_low = 10;
s_high = 95,s_low = 10;
a_a = 2;
write_eeprom(); //保存数据
}
}
#include "lcd1602.h"
//#include "menu.h"
/*************定时器0初始化程序***************/
void time_init()
{
EA = 1; //开总中断
TMOD = 0X01; //定时器0、定时器1工作方式1
ET0 = 1; //开定时器0中断
TR0 = 1; //允许定时器0定时
}
/********************独立按键程序*****************/
uchar key_can; //按键值
void key() //独立按键程序
{
static uchar key_new;
key_can = 20; //按键值还原
P3 |= 0xf0;
if((P3 & 0xf0) != 0xf0) //按键按下
{
delay_1ms(1); //按键消抖动
if(((P3 & 0xf0) != 0xf0) && (key_new == 1))
{ //确认是按键按下
key_new = 0;
switch(P3 & 0xf0)
{
case 0xd0: key_can = 3; break; //得到k1键值
case 0xb0: key_can = 2; break; //得到K2键值
case 0x70: key_can = 1; break; //得到k3键值
}
}
}
else
key_new = 1;
}
/****************按键显示函数***************/
void key_with()
{
if(key_can == 1) //设置键
{
menu_1 ++;
if(menu_1 > 4)
{
menu_1 = 0;
init_1602(); //lcd1602初始化
}
if(menu_1 == 1) //初始化显示
{
write_string(1,0," WH: WL: ");
write_string(2,0," SH: % SL: % ");
write_zifu(1,6,0xdf); //显示度
write_zifu(1,13,0xdf); //显示度
write_sfm2(1,4,t_high); //显示温度
write_sfm2(1,11,t_low); //显示温度
write_sfm2(2,4,s_high); //显示湿度
write_sfm2(2,11,s_low); //显示湿度
}
}
if(menu_1 == 1) //设置高温报警
{
if(key_can == 2)
{
t_high ++ ; //设置高温值加1
if(t_high > 99)
t_high = 99;
}
if(key_can == 3)
{
t_high -- ; //设置高温值减1
if(t_high <= t_low)
t_high = t_low + 1;
}
write_sfm2(1,4,t_high); //显示温度
write_com(0x80+3); //将光标移动到秒个位
write_com(0x0f); //显示光标并且闪烁
write_eeprom(); //保存数据
}
if(menu_1 == 2) //设置低温报警
{
if(key_can == 2)
{
t_low ++ ; //设置低温值加1
if(t_low >= t_high)
t_low = t_high - 1;
}
if(key_can == 3) //设置高温值减1
{
t_low -- ;
if(t_low <= 1)
t_low = 1;
}
write_sfm2(1,11,t_low); //显示湿度
write_com(0x80+10); //将光标移动到秒个位
write_com(0x0f); //显示光标并且闪烁
write_eeprom(); //保存数据
}
if(menu_1 == 3) //设置高湿报警
{
if(key_can == 2)
{
s_high ++ ; //设置高湿值加1
if(s_high > 99)
s_high = 99;
}
if(key_can == 3)
{
s_high -- ; //设置高湿值减1
if(s_high <= s_low)
s_high = s_low + 1;
}
write_sfm2(2,4,s_high); //显示湿度
write_com(0x80+0x40+3); //将光标移动到秒个位
write_com(0x0f); //显示光标并且闪烁
write_eeprom(); //保存数据
}
if(menu_1 == 4) //设置低湿报警
{
if(key_can == 2)
{
s_low ++ ; //设置低湿值加1
if(s_low >= s_high)
s_low = s_high - 1;
}
if(key_can == 3) //设置低湿值减1
{
s_low -- ;
if(s_low <= 1)
s_low = 1;
}
write_sfm2(2,11,s_low); //显示湿度
write_com(0x80+0x40+10); //将光标移动到秒个位
write_com(0x0f); //显示光标并且闪烁
write_eeprom(); //保存数据
}
}
/*****************读温湿度传感器程序****************/
void dst11()
{
uchar i,j; //读温湿度时不能打开中断
dht11 = 0;
delay_1ms(18);
dht11 = 1;
delay_uint(4); //50us
if(dht11 == 0) //判断是否响应 0 为响应
{
while(dht11 == 0);
while(dht11 == 1); //等待响应时间过完
for(i=0;i<5;i++)
{
for(j=0;j<8;j++)
{
table_dht11[i] <<= 1;
while(dht11 == 0);
delay_uint(4); //50us 数据1的高电平时间为110us 低为27us
if(dht11 == 1)
{
table_dht11[i] |= 0x01;
while(dht11 == 1);
}
}
}
}
dht11 = 1;
}
/****************报警函数***************/
void clock_h_l()
{
static uchar value,value1,value2;
if((table_dht11[0] <= s_low) || (table_dht11[0] >= s_high))
{
value1 ++; //消除湿度在边界时的干扰
if(value1 > 2)
flag_s_bj_en = 1; //湿度报警标志位
}
else if((table_dht11[0] > (s_low )) || (table_dht11[0] < (s_high)))
{
value1 ++; //消除湿度在边界时的干扰
if(value1 > 2)
flag_s_bj_en = 0; //湿度取消报警
}
else
value1 = 0;
if((table_dht11[2] <= t_low) || (table_dht11[2] >= t_high))
{
value2 ++; //消除温度在边界时的干扰
if(value2 > 2)
{
flag_w_bj_en = 1; //温度 报警标志位
}
}
else if((table_dht11[2] > (t_low )) || (table_dht11[2] > (t_high)))
{
value2 ++; //消除温度在边界时的干扰
if(value2 > 2)
{
flag_w_bj_en = 0; //温度取消报警
}
}else
value2 = 0;
if(menu_1 == 0)
{
value ++; //用做报警的
if(value > 5)
value = 0;
if((flag_s_bj_en == 1) && (flag_w_bj_en == 1))
{
if(value < 3)
write_string(1,0,"wen shi du arlam");
else
write_string(1,0," ");
}else if(flag_w_bj_en == 1)
{
if(value < 3)
write_string(1,0," wendu arlam ");
else
write_string(1,0," ");
}else if(flag_s_bj_en == 1)
{
if(value < 3)
write_string(1,0," shidu arlam ");
else
write_string(1,0," ");
}else
write_string(1,0," DHT11 Kz System ");
}
if((flag_s_bj_en == 1) || (flag_w_bj_en == 1))
beep = ~beep; //蜂鸣器报警
else
beep = 1;
}
/******************主程序**********************/
void main()
{
beep = 0; //开机叫一声
delay_1ms(150);
P0 = P1 = P2 = P3 = 0xff;
init_eeprom(); ////开始初始化保存的数据
time_init(); //定时器初始化
init_1602(); //lcd1602初始化
dst11(); //先读出温湿度的值
while(1)
{
if(flag_300ms == 1)
{
flag_300ms = 0;
if(beep == 1)
dst11(); //先读出温湿度的值
if(menu_1 == 0) //开始界面显示的内容
{
write_sfm2(2,10,table_dht11[0]);