|
#include <reg51.h>
#include <1602.h>
#include <DS18b20.h>
#include <intrins.h>
sbit speak=P1^5;
sbit keyadj=P3^3;//调整按键
sbit keyup=P3^4; //加
sbit keydown=P3^5;//减
uchar DisplayData[6];//定义数组存放温度值
int num,temp;
uchar almhigh=40,almlow=15; //报警的上下限初始值
void almspeak() //报警响0.5秒
{
uint i=500;
while(i--)
{
speak=~speak;
Delay1ms(1);
}
}
void Display()
{
uchar i;
num=Ds18b20ReadTemp(); //读取温度传感器的数据
temp=num*0.0625*100+0.5; //转换为十进制数值
DisplayData[0] = temp / 10000; //以下为数值的拆分
DisplayData[1] = temp % 10000 / 1000;
DisplayData[2] = temp % 1000 / 100 ;
DisplayData[3] ='.';
DisplayData[4] = temp % 100 / 10;
DisplayData[5] = temp % 10;
LCD1602_write_com(0x80);
LCD1602_write_word("tempreture is:");
LCD1602_write_com(0xc4);
for(i=0;i<6;i++)
{
if(i==3)LCD1602_write_data(DisplayData[i]);
else LCD1602_write_data(DisplayData[i]+48);
}
if((temp/100+0.5)>=almhigh||(temp/100+0.5)<=almlow)almspeak(); //温度超出上下限就报警
}
void Display_adj()
{
LCD1602_write_com(0x01); //清屏指令
LCD1602_write_com(0x80); //重新定位
LCD1602_write_word("adjust almtemp");
}
void disp2() //调整上下限温度的画面
{
LCD1602_write_com(0xc0);
LCD1602_write_word("high");
LCD1602_write_data(almhigh/10+48);
LCD1602_write_data(almhigh%10+48);
LCD1602_write_word(",low");
LCD1602_write_data(almlow/10+48);
LCD1602_write_data(almlow%10+48);
}
void disp3() //调整上限温度的画面
{
LCD1602_write_com(0xc0);
LCD1602_write_word("* almhigh=");
LCD1602_write_data(almhigh/10+48);
LCD1602_write_data(almhigh%10+48);
LCD1602_write_word(" *");
}
void disp4() //调整下限温度的画面
{
LCD1602_write_com(0xc0);
LCD1602_write_word("* almlow=");
LCD1602_write_data(almlow/10+48);
LCD1602_write_data(almlow%10+48);
LCD1602_write_word(" * ");
}
void keyscan() //按键检测
{
if(keyadj==0) //第一次检测到按键按下,进入调整界面
{
Delay1ms(500);
Display_adj();
while(keyadj==1); //按键没有按第二次就等待
if(keyadj==0)//第二次按下调整按键,进行上限调整
{
Delay1ms(500);
disp2();
while(keyadj==1)
{
disp3();
if(keyup==0){Delay1ms(500);almhigh++; disp3();}
if(keydown==0){Delay1ms(500);almhigh--;disp3();}
}
}
if(keyadj==0)//第三次按下调整按键,进行下限调整
{
Delay1ms(500);
while(keyadj==1)
{
disp4();
if(keyup==0){Delay1ms(500);almlow++;disp4();}
if(keydown==0){Delay1ms(500);almlow--;disp4();}
}
}
if(keyadj==0)//第四次按下调整按键,退出调整
{
Delay1ms(500);
while(keyadj==0);
LCD1602_write_com(0x01);//退出之前进行清屏
}
}
}
void main()
{
Init_LCD1602();
Ds18b20Init();
while(1)
{
Display();
keyscan();
}
}
|
|