#include "reg52.h"
#include "lcd.h"
#include "temp.h"
#include "i2c.h"
sbit k3=P3^2; //设置温度上下限
sbit k1=P3^1; //加
sbit k2=P3^0; //减
sbit led=P2^4; //报警指示灯
sbit beep=P1^5; //蜂鸣器报警
sbit relay=P1^1; // 设备加热和继电器
sbit moto=P1^0; // 电机散热
char set_temph=0,a=0;//,set_templ=28; //设定温度限默认值
u16 temp_val; //检测的实际温度
u8 mode; //温度模式
void delay(u16 i)
{
while(i--);
}
void Temp_DataPros()
{
char set_temph=a;//,set_templ=28; //设定温度上下限默认值
short temp;
u8 temp_buf[5];
temp=Ds18b20ReadTemp();
temp_val=temp;
if(temp<0)
{
temp=-temp;
LCD_Dispstring(3+5,0,"-");
}
else
{
LCD_Dispstring(3+5,0," ");
}
temp_buf[0]=temp/100+0x30;
temp_buf[1]=temp%100/10+0x30;
temp_buf[2]='.';
temp_buf[3]=temp%100%10+0x30;
temp_buf[4]='\0';
LCD_Dispstring(3+6,0,temp_buf); //显示检测的温度xx.x
set_temph=a;
temp_buf[0]=set_temph/10+0x30;
temp_buf[1]=set_temph%10+0x30;
temp_buf[2]='\0';
LCD_Dispstring(8,1,temp_buf); //显示设定的温度值xx */
}
#define K1_MODE 1
#define K2_ADD 2
#define K3_DEC 3
//mode: 0-单次扫描 1-连续扫描
u8 KEY_Scan(u8 mode)
{
static u8 key=1;
if(key&&(k1==0||k2==0||k3==0))
{
delay(1000); //消抖
key=0;
if(k3==0)
{
return K1_MODE;
}
else if(k1==0)
{
return K2_ADD;
}
else if(k2==0)
{
return K3_DEC;
}
}
else if(k1==1&&k2==1&&k3==1)
{
key=1;
}
if(mode)
{
key=1;
}
return 0;
}
void sound()
{
u8 i=50;
while(i--)
{
beep=!beep;
delay(10);
}
}
void KEY_Pros()
{
u8 key;
u8 temph_buf[3];
key=KEY_Scan(0);
if(key==K1_MODE) //模式选择
{
mode++;
LCD_Clear();
if(mode==1)
{
LCD_Dispstring(4,0,"SET: C");
}
else
{
mode=0;
LCD_Dispstring(3,0,"Temp: C");
LCD_Dispstring(4,1,"SET: C");
}
}
if(mode==1) //温度上限设置
{
switch(key)
{
case K2_ADD: //加
set_temph++;
if(set_temph>=80)set_temph=80;
break;
case K3_DEC: //减
set_temph--;
if(set_temph<=0)set_temph=0;
break;
}
a=set_temph;
temph_buf[0]=set_temph/10+0x30;
temph_buf[1]=set_temph%10+0x30;
temph_buf[2]='\0';
LCD_Dispstring(8,0,temph_buf);
At24c02Write(0,set_temph);
}
}
void TempData_Compare()
{
if(temp_val>(set_temph+1)*10) //实际温度高于上限值 报警 散热
{
led=1;
moto=1; //1
relay=0;
sound();
}
else if(temp_val<(set_temph-1)*10) //实际温度低于下限值 报警 加热
{
led=1;
moto=0; //0
relay=1;
sound();
}
else //实际温度在下限值和上限值之间 取消报警 取消加热 取消散热
{
moto=1;
led=0;
relay=1;
}
}
void kai_display()
{
if(At24c02Read(255)!=18)
{
At24c02Write(0,set_temph);
At24c02Write(255,18);
}
else
{
set_temph=At24c02Read(0);
}
LCD_Dispstring(3,0,"Temp: C");
LCD_Dispstring(4,1,"SET: C");
}
void main()
{
moto=0;
led=0;
relay=0;
LCD_Init();
kai_display();
while(1)
{
TempData_Compare();
if(mode==0)
Temp_DataPros();
KEY_Pros();
}
}
|