作者:佚名 来源:网络
#include <REGX51.H>
#include <keyboardscan.H>
#include "DS18B20.H"
sbit bit1=P0^0;
sbit bit2=P0^1;
sbit bit3=P0^2;
sbit bit4=P0^3;
sbit bit5=P0^4;
sbit bit6=P0^5;
sbit bit7=P0^6;
sbit bit8=P0^7;
sbit heat=P3^5;
sbit fan=P3^6;
sbit ring=P3^7;
unsigned char code Numbernomal[]={0x03,0x9F,0x25,0x0D,0x99,0x49,0x41,0x1F,
0x01,0x09,0x11,0xC1,0x63,0x85,0x61,0X03,0Xff,0x63}; //{0x03,0x9F,0x25,0x0D,0x99,0x49,0x41,0x1F,0x01,0x09};
unsigned char code Numberdecimal[]={0x02,0x9E,0x24,0x0C,0x98,0x48,0x40,0x1E,
0x00,0x08,0x10,0xC0,0x62,0x84,0x60,0x70};
unsigned char showdata[8]={16,16,16,16,5,2,0,0};
unsigned char count,count1=0,temp0=0,temp1=0,temp2=0,temp3=0;
unsigned char hts1=0,hts2=0,x1=0Xff,x2=0Xff,x3=0Xff,x4=0Xff;
unsigned int temperature,nowtem=0;
//==============================延时函数,1毫秒
void Delay1ms(unsigned char count)
{
unsigned char i,j;
for(i=0;i<count;i++)
for(j=0;j<120;j++);
}
//====================初始计数器==================================
void InitInterrupt(void) //initialize the interrupt
{
TMOD=0x01;
TH0=0x20;
TL0=0x18;
IT0=1;
ET0=1;
TR0=1;
EA=1;
}
Colect()interrupt 1 using 1 //读键盘
{
temp0=keyboardscan();
if(temp0==14)
{
x1=0Xff;
x2=0Xff;
x3=0Xff;
x4=0Xff;
temp1=0;
temp3=1;
}
if((temp0<14)&(temp3==1))
{
if(temp0<10)
{
if(temp1==1)
x2=x1;
if(temp1==2)
{
x3=x2;
x2=x1;
}
showdata[0]=temp0;
x1=Numbernomal[showdata[0]];
temp1++;
}
if(temp0==10)
{
count1=0;
while(Numbernomal[count1++]!=x1);
x1=Numberdecimal[count1-1];
}
if(temp1==3)
temp1=0;
}
if(temp0==15)
{
temp3=0;
x4=x3;
x3=x2;
x2=x1;
x1=0x63;
}
}
show()
{
P0=0xfe;
P3=Numbernomal[showdata[4]];
Delay1ms(5);
P0=0xfd;
P3=Numberdecimal[showdata[5]];
Delay1ms(5);
P0=0xfb;
P3=Numbernomal[showdata[6]];
Delay1ms(5);
P0=0xf7;
P3=0x63;
Delay1ms(5);
P3=x1;
P0=0x7f;
Delay1ms(5);
P3=x2;
P0=0xbf;
Delay1ms(5);
P0=0xdf;
P3=x3;
Delay1ms(5);
P0=0xef;
P3=x4;
Delay1ms(5);
P0=0xff;
}
Control()
{
nowtem=x4*100+x3*10+x2;
if(temperature<nowtem-20)
{
heat=1;
fan=0;
}
else heat=0;
if(temperature>nowtem-20)
{
heat=1;
Delay1ms(10);
heat=0;
fan=1;
Delay1ms(10);
fan=0;
if(hts1==201)
hts1=0;
}
if((temperature>nowtem)&(temperature<nowtem+20))
{
hts1++;
if(hts1%3==0)
{
fan=1;
Delay1ms(10);
fan=0;
}
if(hts1%6==0)
{
heat=1;
Delay1ms(10);
heat=0;
}
if(hts1==201)
hts1=0;
}
if(temperature>nowtem+20)
{
heat=0;
fan=1;
}
if(temperature>nowtem-20)
{
ring=1;
Delay1ms(20);
}
else ring=0;
}
main()
{
InitInterrupt() ;
while(1)
{
show();
Control();
temp2++;
if(temp2==20)
{
temperature=Read_Temperature();
temperature=temperature*0.625;
showdata[4]=temperature/100;
showdata[5]=(temperature/10)%10;
showdata[6]=temperature%10;
temp2=0;
TH0=0xff;
TL0=0xff;
}
}
}