#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar code duan[]={192, 249, 164, 176, 153, 146, 130, 248, 128, 144,191,255};
uchar code xs[]={0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9};
uchar zeng,xiao,fuhao,h,l;
char hh,hl,temp;
uchar data wei_m[4];
uchar bdata dm,c;
sbit c7=c^7;
sbit DQ=P3^3;
sbit dm0=dm^0;
sbit k1=P1^7;//模式键
sbit k2=P1^5;//加
sbit k3=P1^6;//减
sbit beep=P3^7;
void de60us(uchar x)
{ uchar a,b;
for(a=x;a>0;a--){ b=8; while(b--); }
}
void dsplay(void)
{ uchar i;
wei_m[0]=duan[xs[xiao]];
wei_m[1]=duan[zeng%10]&0x7f;
wei_m[2]=duan[(zeng/10)%10];
wei_m[3]=duan[zeng/100];
if((zeng/100)==0) {wei_m[3]=duan[fuhao];}
P2=0x7f;
for(i=0;i<4;i++)
{ P0=wei_m[i]; de60us(80); P0=0xff; P2=(P2>>1)|0x80; }
}
void xieshi(void)
{ uchar i;
wei_m[0]=duan[temp%10];
wei_m[1]=duan[(temp/10)%10];
wei_m[2]=duan[temp/100];
if((temp/100)==0) {wei_m[2]=duan[fuhao];}
P2=0x7f;
for(i=0;i<4;i++){ P0=wei_m[i]; de60us(50); P0=0xff; P2=(P2>>1)|0x80; }
}
void reset(void)
{ DQ=1; _nop_(); DQ=0;
de60us(8); DQ=1; de60us(1);
while(DQ==1);
while(DQ==0);
_nop_();
}
void writ(uchar com)
{ uchar b;
for(b=0;b<8;b++)
{ dm=com;
DQ=0;
_nop_();
DQ=dm0;
de60us(1);
DQ=1;
com=dm>>1;
_nop_();
_nop_();
}
}
uchar read(void)
{ uchar a;
for(a=0;a<8;a++)
{ DQ=1; DQ=0; _nop_();
c=c>>1; DQ=1; _nop_();
_nop_();c7=DQ; de60us(1);
}
return(c);
}
void clxs(void)
{ fuhao=(h<<4)&0x80;
if(fuhao==0x80) { fuhao=10; l=(~l)+0x01;h=(~h);}
else {fuhao=11; }
zeng=(h<<4)|(l>>4);
xiao=l&0x0f;
dsplay();
}
void key(void)
{ uchar a=1;
do{ if(k1==0){de60us(250);if(k1==0){while(k1==0);de60us(100);a++;}}
switch(a){
case 1:{ wei_m[3]=0x89; fuhao=hh&0x80;
if(fuhao==0x80) { fuhao=10;temp=~hh+1; }
else { fuhao=11;temp=hh; }
xieshi();
if(k2==0) { de60us(250); if(k2==0) { while(k2==0); de60us(100); hh++;
if(hh>125){ hh=-55; }}}
if(k3==0){ de60us(250); if(k3==0) { while(k3==0); de60us(100); hh--;
if(hh<-55){ hh=125; }}}
};break;
case 2:{ wei_m[3]=0xc7; fuhao=hl&0x80;
if(fuhao==0x80) { fuhao=10; temp=~hl+1; }
else {fuhao=11;temp=hl;}
xieshi();
if(k2==0) { de60us(250); if(k2==0) { while(k2==0); de60us(100); hl++;
if(hl>125){ hl=-55; }}}
if(k3==0) { de60us(250); if(k3==0) { while(k3==0); de60us(100); hl--;
if(hl<-55){ hl=125; }}}
};break;
default :break;
}
}while(a<3);
}
void main(void)
{ uchar bj;
hh=27;hl=18;
while(1)
{ if(k1==0){de60us(250);if(k1==0){while(k1==0);key();}}
reset(); writ(0xcc); writ(0x44);
reset(); writ(0xcc); writ(0xbe);
l=read(); h=read();
bj=(h<<4)|(l>>4);
if((bj>hh)||(bj<hl)){beep=~beep;}
clxs();
}
}