main.c
#include<stc15f2k60s2.h>
#include<onewire.h>
unsigned char code smgduan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,~0x40,0xff};
unsigned char smgbuffer[8];
unsigned char smgcom,keytt;
unsigned int temptt,wendu,LEDtt;
unsigned char qujian;
unsigned char Tmin=20;
unsigned char Tmax=30;
unsigned char TTT;
unsigned char canshu[5]; unsigned char canshui;
bit mode,querenflag,LEDflag;
void display();
void keyscan();
void Timer1Init(void)
{
AUXR |= 0x40;
TMOD &= 0x0F;
TL1 = 0x20;
TH1 = 0xD1;
TF1 = 0;
TR1 = 1;
ET1=1;
EA=1;
}
void Timer1() interrupt 3
{
unsigned char P0_keeper,P2_keeper;
P0_keeper=P0; P2_keeper=P2; P2=P2&0x1f;
display();
keytt++; if(keytt==10) {keytt=0; keyscan();}
temptt++;
if(qujian==0) {LEDtt++; if(LEDtt==800) {LEDtt=0; LEDflag=~LEDflag;}}
else if(qujian==1) {LEDtt++; if(LEDtt==400) {LEDtt=0; LEDflag=~LEDflag;}}
else if(qujian==2) {LEDtt++; if(LEDtt==200) {LEDtt=0; LEDflag=~LEDflag;}}
P0=P0_keeper; P2=P2_keeper;
}
void display()
{
P0=0xff; P2=P2&0x1f|0xc0; P2=P2&0x1f;
P0=0xff; P2=P2&0x1f|0xe0; P2=P2&0x1f;
P0=1<<smgcom; P2=P2&0x1f|0xc0; P2=P2&0x1f;
P0=smgduan[smgbuffer[smgcom++]]; P2=P2&0x1f|0xe0; P2=P2&0x1f;
if(smgcom==8) smgcom=0;
}
void keyscan()
{
static unsigned char keystat;
unsigned char key_x=0,key_y=0,key_z=0;
P4=0x00; P3=0x0f;
if(!P30) key_x=3;
else if(!P31) key_x=2;
else if(!P32) key_x=1;
else if(!P33) key_x=0;
P4=0xff; P3=0xf0;
if(!P34) key_y=4;
else if(!P35) key_y=3;
else if(!P42) key_y=2;
else if(!P44) key_y=1;
key_z=key_x+key_y*4;
switch(keystat)
{
case 0:if(key_z!=0) keystat=1; break;
case 1:if(key_z==0) keystat=0;
else
{
keystat=2;
switch(key_z)
{
case 4: canshui++; canshu[canshui]=9; break;
case 5: canshui++; canshu[canshui]=6; break;
case 6: canshui++; canshu[canshui]=3; break;
case 7: canshui++; canshu[canshui]=0; break;
case 9: canshui++; canshu[canshui]=7; break;
case 10:canshui++; canshu[canshui]=4; break;
case 11:canshui++; canshu[canshui]=1; break;
case 13:canshui++; canshu[canshui]=8; break;
case 14:canshui++; canshu[canshui]=5; break;
case 15:canshui++; canshu[canshui]=2; break;
case 8: if(querenflag==1) mode=0;
else if(mode==0) mode=1; break;
case 12:canshui--; break;
}
}break;
case 2:if(key_z==0) keystat=0; break;
}
}
void xianshi()
{
if(mode==0)
{
smgbuffer[0]=smgbuffer[2]=10;
smgbuffer[1]=qujian;
smgbuffer[3]=smgbuffer[4]=smgbuffer[5]=11;
smgbuffer[6]=wendu/1000;
smgbuffer[7]=wendu%1000/100;
}
else if(mode==1)
{
smgbuffer[0]=smgbuffer[5]=10;
smgbuffer[3]=smgbuffer[4]=11;
if(canshui==0)
{
smgbuffer[1]=smgbuffer[2]=smgbuffer[6]=smgbuffer[7]=11;
}
else if(canshui==1)
{
smgbuffer[1]=canshu[1];
smgbuffer[2]=smgbuffer[6]=smgbuffer[7]=11;
}
else if(canshui==2)
{
smgbuffer[1]=canshu[1]; smgbuffer[2]=canshu[2];
smgbuffer[6]=smgbuffer[7]=11;
}
else if(canshui==3)
{
smgbuffer[1]=canshu[1]; smgbuffer[2]=canshu[2];
smgbuffer[6]=canshu[3]; smgbuffer[7]=11;
}
else if(canshui==4)
{
smgbuffer[1]=canshu[1]; smgbuffer[2]=canshu[2];
smgbuffer[6]=canshu[3]; smgbuffer[7]=canshu[4];
}
if(canshui==5) {canshui=4;}
}
}
void wenduwending()
{
unsigned char Ti;
for(Ti=0;Ti<70;Ti++)
{
wendu=tempget()*100+0.5;
}
}
void wenduhuoqu()
{
if(temptt==500)
{
temptt=0;
wendu=tempget()*100+0.5;
}
TTT=10*(wendu/1000)+wendu%1000/100;
}
void canshuzhengque()
{
if(canshui==4)
{
Tmin=canshu[3]*10+canshu[4]; Tmax=canshu[1]*10+canshu[2];
if(Tmin<=Tmax)querenflag=1;
}
}
void qujianpanduan()
{
if(TTT<Tmin)qujian=0;
else if(TTT>=Tmin&&TTT<=Tmax) qujian=1;
else if(TTT>Tmax) qujian=2;
}
void LED()
{
if(mode==0)
{
if(LEDflag==0) {P00=0; P2=P2&0x1f|0x80; P2=P2&0x1f;}
else if(LEDflag==1){P00=1; P2=P2&0x1f|0x80; P2=P2&0x1f;}
}
}
void main()
{
P0=0x00; P2=P2&0x1f|0xa0; P2=P2&0x1f;
P0=0xff; P2=P2&0x1f|0x80; P2=P2&0x1f;
wenduwending();
Timer1Init();
while(1)
{
wenduhuoqu();
qujianpanduan();
canshuzhengque();
xianshi();
LED();
}
}
onewire.c
#include "reg52.h"
sbit DQ = P1^4;
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
void Delay_OneWire(unsigned int t)
{
t=t*12;
while(t--);
}
unsigned char tempget()
{
unsigned char temp;
unsigned char low,high;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();
temp=(high<<8|low)*0.0625;
return temp;
}