|
#include "reg52.h"
#include "lcd.h"
#include "temp.h"
#include "i2c.h"
sbit k3=P3^2; //設(shè)置溫度上下限
sbit k1=P3^1; //加
sbit k2=P3^0; //減
sbit led=P2^4; //報(bào)警指示燈
sbit beep=P1^5; //蜂鳴器報(bào)警
sbit relay=P1^1; // 設(shè)備加熱和繼電器
sbit moto=P1^0; // 電機(jī)散熱
char set_temph=0,a=0;//,set_templ=28; //設(shè)定溫度限默認(rèn)值
u16 temp_val; //檢測(cè)的實(shí)際溫度
u8 mode; //溫度模式
void delay(u16 i)
{
while(i--);
}
void Temp_DataPros()
{
char set_temph=a;//,set_templ=28; //設(shè)定溫度上下限默認(rèn)值
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); //顯示檢測(cè)的溫度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); //顯示設(shè)定的溫度值xx */
}
#define K1_MODE 1
#define K2_ADD 2
#define K3_DEC 3
//mode: 0-單次掃描 1-連續(xù)掃描
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) //溫度上限設(shè)置
{
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) //實(shí)際溫度高于上限值 報(bào)警 散熱
{
led=1;
moto=1; //1
relay=0;
sound();
}
else if(temp_val<(set_temph-1)*10) //實(shí)際溫度低于下限值 報(bào)警 加熱
{
led=1;
moto=0; //0
relay=1;
sound();
}
else //實(shí)際溫度在下限值和上限值之間 取消報(bào)警 取消加熱 取消散熱
{
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();
}
}
|
|