#include <stc15.h>
#include <intrins.h>
#include <onewire.h>
sbit R1=P3^0;
sbit R2=P3^1;
sbit R3=P3^2;
sbit R4=P3^3;
sbit C4=P3^4;
sbit C3=P3^5;
sbit C2=P4^2;
sbit C1=P4^4;
unsigned char a[12]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf,0xff};//0~9/-/灭
unsigned char b[8]={0};
unsigned int wendu,wen_max=30,wen_min=20,count_wendu=0,count_deng=0;
unsigned char n=5;
void BUZZ()//初始化
{
P2=(P2&0x1f)|0x80;
P0=0xff;
P2&=0x1f;
P2=(P2&0x1f)|0xa0;
P0=0x00;
P2&=0x1f;
P2=(P2&0x1f)|0xc0;
P0=0xff;
P2&=0x1f;
P2=(P2&0x1f)|0xe0;
P0=0xff;
P2&=0x1f;
}
void Delay30ms() //@11.0592MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 2;
j = 67;
k = 183;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void SMG_xuanze()//数码管 位选 段选
{
static int i=0;
P2=(P2&0x1f)|0xc0;
P0=0x01<<i;
P2&=0x1f;
P2=(P2&0x1f)|0xe0;
P0=a[b[i]];
P2&=0x1f;
i=(i+1)%8;
}
unsigned int wendu_duxie()//温度读写
{
unsigned int wendu;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(1200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
EA=0;
wendu=Read_DS18B20();
wendu |=(Read_DS18B20()<<8);
wendu=wendu*6.25;
EA=1;
return wendu;
}
void wendu_xianshi()//温度显示
{
b[0]=10;
b[1]=1;
b[2]=10;
b[3]=11;
b[4]=11;
b[5]=11;
b[6]=wendu/1000;
b[7]=wendu/100%10;
}
void wendu_shezhi()//温度设置界面
{
b[0]=10;
b[1]=wen_max/10%10;
b[2]=wen_max%10;
b[3]=11;
b[4]=11;
b[5]=10;
b[6]=wen_min/10%10;
b[7]=wen_min%10;
}
void Keys()//按键
{
C1=0;//第一列
C2=C3=C4=1;
R1=R2=R3=R4=1;
//=================
if(R1==0&n==0)
{
Delay30ms();Delay30ms();
if(R1==0)
{
while(R2==0);
wen_max=0*10;
n=1;
}
}
if(n==1)
{
if(R1==0)
{
Delay30ms();Delay30ms();
if(R1==0)
{
while(R1==0);
wen_max=wen_max+0;
n=2;
}
}
}
if(n==2)
{
if(R1==0)
{
Delay30ms();Delay30ms();
if(R1==0)
{
while(R1==0);
wen_min=0*10;
n=3;
}
}
}
if(n==3)
{
if(R1==0)
{
Delay30ms();Delay30ms();
if(R1==0)
{
while(R1==0);
wen_min=wen_min+0;
n=4;
}
}
}
//===================
if(R2==0&n==0)
{
Delay30ms();Delay30ms();
if(R2==0)
{
while(R2==0);
n=1;
wen_max=3*10;
}
}
if(n==1)
{
if(R2==0)
{
Delay30ms();Delay30ms();
if(R2==0)
{
while(R2==0);
n=2;
wen_max=wen_max+3;
}
}
}
if(n==2)
{
if(R2==0)
{
Delay30ms();Delay30ms();
if(R2==0)
{
while(R2==0);
n=3;
wen_min=3*10;
}
}
}
if(n==3)
{
if(R2==0)
{
Delay30ms();Delay30ms();
if(R2==0)
{
while(R2==0);
wen_min=wen_min+3;
n=4;
}
}
}
//============
if(R3==0&n==0)
{
Delay30ms();Delay30ms();
if(R3==0)
{
while(R3==0);
n=1;
wen_max=6*10;
}
}
if(n==1)
{
if(R3==0)
{
Delay30ms();Delay30ms();
if(R3==0)
{
while(R3==0);
n=2;
wen_max=wen_max+6;
}
}
}
if(n==2)
{
if(R3==0)
{
Delay30ms();Delay30ms();
if(R3==0)
{
while(R3==0);
n=3;
wen_min=6*10;
}
}
}
if(n==3)
{
if(R3==0)
{
Delay30ms();Delay30ms();
if(R3==0)
{
while(R3==0);
wen_min=wen_min+6;
n=4;
}
}
}
//=================
if(R4==0&n==0)
{
Delay30ms();Delay30ms();
if(R4==0)
{
while(R4==0);
n=1;
wen_max=9*10;
}
}
if(n==1)
{
if(R4==0)
{
Delay30ms();Delay30ms();
if(R4==0)
{
while(R4==0);
n=2;
wen_max=wen_max+9;
}
}
}
if(n==2)
{
if(R4==0)
{
Delay30ms();Delay30ms();
if(R4==0)
{
while(R4==0);
n=3;
wen_min=9*10;
}
}
}
if(n==3)
{
if(R4==0)
{
Delay30ms();Delay30ms();
if(R4==0)
{
while(R4==0);
wen_min=wen_min+9;
n=4;
}
}
}
//===============
C2=0;//第二列
C1=C3=C4=1;
R1=R2=R3=R4=1;
//=================
if(R1==0&n==0)
{
Delay30ms();Delay30ms();
if(R1==0)
{
while(R2==0);
n=1;
wen_max=1*10;
}
}
if(n==1)
{
if(R1==0)
{
Delay30ms();Delay30ms();
if(R1==0)
{
while(R1==0);
n=2;
wen_max=wen_max+1;
}
}
}
if(n==2)
{
if(R1==0)
{
Delay30ms();Delay30ms();
if(R1==0)
{
while(R1==0);
n=3;
wen_min=1*10;
}
}
}
if(n==3)
{
if(R1==0)
{
Delay30ms();Delay30ms();
if(R1==0)
{
while(R1==0);
wen_min=wen_min+1;
n=4;
}
}
}
//===================
if(R2==0&n==0)
{
Delay30ms();Delay30ms();
if(R2==0)
{
while(R2==0);
n=1;
wen_max=4*10;
}
}
if(n==1)
{
if(R2==0)
{
Delay30ms();Delay30ms();
if(R2==0)
{
while(R2==0);
n=2;
wen_max=wen_max+4;
}
}
}
if(n==2)
{
if(R2==0)
{
Delay30ms();Delay30ms();
if(R2==0)
{
while(R2==0);
n=3;
wen_min=4*10;
}
}
}
if(n==3)
{
if(R2==0)
{
Delay30ms();Delay30ms();
if(R2==0)
{
while(R2==0);
wen_min=wen_min+4;
n=4;
}
}
}
//============
if(R3==0&n==0)
{
Delay30ms();Delay30ms();
if(R3==0)
{
while(R3==0);
n=1;
wen_max=7*10;
}
}
if(n==1)
{
if(R3==0)
{
Delay30ms();Delay30ms();
if(R3==0)
{
while(R3==0);
n=2;
wen_max=wen_max+7;
}
}
}
if(n==2)
{
if(R3==0)
{
Delay30ms();Delay30ms();
if(R3==0)
{
while(R3==0);
n=3;
wen_min=7*10;
}
}
}
if(n==3)
{
if(R3==0)
{
Delay30ms();Delay30ms();
if(R3==0)
{
while(R3==0);
wen_min=wen_min+7;
n=4;
}
}
}
if(n==5)
{
if(R4==0)
{
Delay30ms();Delay30ms();
if(R4==0)
{
while(R4==0);
n=0;
}
}
}
if(n==0|n==1|n==2|n==3|n==4)
{
if(R4==0)
{
Delay30ms();Delay30ms();
if(R4==0)
{
while(R4==0);
n=5;
}
}
}
//============================
C3=0;//第三列
C2=C1=C4=1;
R1=R2=R3=R4=1;
//=================
if(R1==0&n==0)
{
Delay30ms();Delay30ms();
if(R1==0)
{
while(R2==0);
n=1;
wen_max=2*10;
}
}
if(n==1)
{
if(R1==0)
{
Delay30ms();Delay30ms();
if(R1==0)
{
while(R1==0);
n=2;
wen_max=wen_max+2;
}
}
}
if(n==2)
{
if(R1==0)
{
Delay30ms();Delay30ms();
if(R1==0)
{
while(R1==0);
n=3;
wen_min=2*10;
}
}
}
if(n==3)
{
if(R1==0)
{
Delay30ms();Delay30ms();
if(R1==0)
{
while(R1==0);
wen_min=wen_min+2;
n=4;
}
}
}
//===================
if(R2==0&n==0)
{
Delay30ms();Delay30ms();
if(R2==0)
{
while(R2==0);
n=1;
wen_max=5*10;
}
}
if(n==1)
{
if(R2==0)
{
Delay30ms();Delay30ms();
if(R2==0)
{
while(R2==0);
n=2;
wen_max=wen_max+5;
}
}
}
if(n==2)
{
if(R2==0)
{
Delay30ms();Delay30ms();
{
while(R2==0);
n=3;
wen_min=5*10;
}
}
}
if(n==3)
{
if(R2==0)
{
Delay30ms();Delay30ms();
if(R2==0)
{
while(R2==0);
wen_min=wen_min+5;
n=4;
}
}
}
//============
if(R3==0&n==0)
{
Delay30ms();Delay30ms();
if(R3==0)
{
while(R3==0);
n=1;
wen_max=8*10;
}
}
if(n==1)
{
if(R3==0)
{
Delay30ms();Delay30ms();
if(R3==0)
{
while(R3==0);
n=2;
wen_max=wen_max+8;
}
}
}
if(n==2)
{
if(R3==0)
{
Delay30ms();Delay30ms();
if(R3==0)
{
while(R3==0);
n=3;
wen_min=8*10;
}
}
}
if(n==3)
{
if(R3==0)
{
Delay30ms();Delay30ms();
if(R3==0)
{
while(R3==0);
wen_min=wen_min+8;
n=4;
}
}
}
if(n==0|n==1|n==2|n==3|n==4)
{
if(R4==0)
{
Delay30ms();Delay30ms();
if(R4==0)
{
while(R4==0);
wen_min=0;
wen_max=0;
n=0;
}
}
}
//=================
}
void Timer0Init(void) //1毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0xCD; //设置定时初值
TH0 = 0xD4; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0=1;
EA=1;
}
void ServiceTimer0() interrupt 1
{
if(n==5)
{
SMG_xuanze();
wendu_xianshi();
count_wendu++;
}
else
{
SMG_xuanze();
wendu_shezhi();
}
if(wen_max<wen_min)
{
P2=(P2&0x1f)|0x80;
P0=0xfd;
}
else if(wen_max>=wen_min)
{
P2=(P2&0x1f)|0x80;
P0=0xff;
}
//=====================
if(wendu/100<wen_min)
{
P2=(P2&0x1f)|0xa0;
P0=0x00;
P2&=0x1f;
count_deng++;
if(count_deng==400)
{
P2=(P2&0x1f)|0x80;
P0=0xfe;
}
if(count_deng>400)
{
P2=(P2&0x1f)|0x80;
P0=0xff;
}
if(count_deng==800)
count_deng=0;
}
if(wendu/100>=wen_min&&wendu/100<=wen_max)
{
P2=(P2&0x1f)|0xa0;
P0=0x00;
P2&=0x1f;
count_deng++;
if(count_deng==200)
{
P2=(P2&0x1f)|0x80;
P0=0xfe;
}
if(count_deng>200)
{
P2=(P2&0x1f)|0x80;
P0=0xff;
}
if(count_deng==400)
{
count_deng=0;
}
}
if(wendu/100>wen_max)
{
P2=(P2&0x1f)|0xa0;
P0=0x10;
P2&=0x1f;
count_deng++;
if(count_deng==100)
{
P2=(P2&0x1f)|0x80;
P0=0xfe;
}
if(count_deng>100)
{
P2=(P2&0x1f)|0x80;
P0=0xff;
}
if(count_deng==200)
{
count_deng=0;
}
}
}
void main()
{
BUZZ();
Timer0Init();
while(1)
{
Keys();
if(count_wendu%500==0)
wendu=wendu_duxie();//从底层读取温度显示
}
}