#include"ds18b20.h"
#include<math.h>
#include<absacc.h>
//************************************变量定义*******************************
uchar code str1[]="Set: 011.0 ";
uchar code str2[]="Cur: ";
uchar code str3[]="Che: ";
uchar code tem[8]={0x0c,0x12,0x12,0x0c,0x00,0x00,0x00,0x00};
uint val[10]=0; //定义查询温度存储的数组
uchar display_set[4]; //存储设置温度的数组
uchar data disdata[5];
uint tvalue; //当前温度值
uchar tflag; //温度的符号标志位
uint temp1=110; //设定温度的初始值
uint count=0;
uchar flag1=0;
uchar flag2=0;
uchar i=0;
uchar num=0;
uint set_temp;
uint aa;
//******************************位定义*******************************
sbit key_meu = P1^0;
sbit key_add = P1^1;
sbit key_dec = P1^2;
sbit key_ok = P1^3;
sbit key_ch = P1^4;
sbit A=P2^3;
sbit C=P2^4;
sbit HEAT=P2^5;
//******************************************************
// 温度查询
//********************************************************
void check()
{
char j=0;
EA=0;
wr_com(0x80);
display(str3);
display1(val[j],j);
while(1)
{
if(key_add == 0)
{
delay1ms(20);
if(key_add == 0)
{
while(!key_add);
j++;
if(j==10)
j=0;
display1(val[j],j);
}
}
if(key_dec == 0)
{
delay1ms(20);
if(key_dec == 0)
{
while(!key_dec);
j--;
if(j==-1)
j=9;
display1(val[j],j);
}
}
if(key_ok == 0)
{
delay1ms(20);
if(key_ok == 0)
{
while(!key_ok);
wr_com(0x80);
display(str1);
show_set_temp(temp1);
wr_com(0x8b);
wr_dat(0x00);
wr_com(0x8c);
wr_dat('C');
EA=1;
break;
}
}
}
}
//***********************************************************
// 温度设置
//***********************************************************
void set()
{
EA=0;
wr_com(0x8f);
wr_com(0x0f);//光标闪射
while(1)
{
if(key_add == 0)
{
delay1ms(20);
if(key_add == 0)
{
while(!key_add);
temp1+=10;
wr_com(0x0c);
show_set_temp(temp1);
wr_com(0x0f);
wr_com(0x8f);
}
}
if(key_dec == 0)
{
delay1ms(20);
if(key_dec == 0)
{
while(!key_dec);
temp1-=10;
wr_com(0x0c);
show_set_temp(temp1);
wr_com(0x0f);
wr_com(0x8f);
}
}
if(key_ok == 0)
{
delay1ms(20);
if(key_ok == 0)
{
while(!key_ok);
set_temp=temp1;
num=1;
wr_com(0x0c);
EA=1;
break;
}
}
}
}
//***********************************************************
// 按键扫描
//**********************************************************
void key_scan()
{
EA=0;
if(key_meu == 0)
{
delay1ms(20);
if(key_meu == 0)
{
while(!key_meu);
set();
}
}
if(key_ch == 0)
{
delay1ms(20);
if(key_ch == 0)
{
while(!key_ch);
check();
}
}
EA=1;
}
void init_timer()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
//***************************************************
// 温度控制
//****************************************************
void control_temp()
{
if(tvalue>set_temp)
{
if(flag2==0)
{
flag1=1;
HEAT=1;
A=1;
C=0;
}
}
else if(tvalue<set_temp)
{
if(flag1==0)
{
flag2=1;
A=0;
C=0;
HEAT=0;
}
}
else
{
HEAT=1;
A=0;
C=0;
flag1=0;
flag2=0;
}
}
//******************************************************
// 主函数
//******************************************************
void main()
{
read_temp();//读取温度
delay1ms(400);
read_temp();//读取温度
delay1ms(400);
read_temp();//读取温度
aa = tvalue;
HEAT=1;
A=0;
C=0;
init_play(); //在字库中创建圈圈符号,位置为0X00
write_new();
wr_com(0xcb);
wr_dat(0x00);
wr_com(0xcc);
wr_dat('C');
wr_com(0x8b);
wr_dat(0x00);
wr_com(0x8c);
wr_dat('C');
init_timer();
while(1)
{
loop: if(abs(read_temp()-aa)<50)
{
key_scan();
ds1820disp();//显示
val[i] = tvalue;
aa=tvalue;
if(num==1)
{
control_temp();
}
goto loop;
}
aa=tvalue;
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==5)
{ count=0;
i++;
if(i==10)
i=0;
}
}
- 1
- 2
前往页