#include "stc15.h"
#define FOSC 11059200L
#define T1MS (65536-FOSC/12/1000)
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
#define LY 1
#define CY 2
#define AY 3
#define UY 4
#define ledon 1
#define ledoff 0
sbit beep=P0^6;
sbit jdq=P0^4;
u8 u8led=0x00;
u8 code t_display[]={
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46};
u8 code T_COM[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
u8 LED8[8];
u8 display_index;
bit B_1ms;
void hc573(u8 x);
u8 bit1(u8 x,u8 i);
u8 bit0(u8 x,u8 i);
u8 bitT(u8 x,u8 i);
u8 bitR(u8 x,u8 i);
void LEDdisp(void);
void LEDsta(u8 pos, u8 sta);
void delay1ms(int nms);
long s4time=0;
u8 s4timeflag=0;
u8 s4key=0;
u8 s4timeflag2=0;
u16 s4time2=0;
u8 s4cnt=0;
void main()
{
long a=0;
hc573(UY);
beep=0;
hc573(0);
hc573(0);
u8led=0x00;
P0=~u8led;
hc573(LY);
hc573(0);
AUXR &= 0xdf;
TMOD = 0x00;
TL1 = T1MS;
TH1 = T1MS >> 8;
TR1 = 1;
ET1 = 1;
EA = 1;
while(1)
{
if(P30==0)
{
delay1ms(10);
if(P30==0)
{
s4key=1;
s4timeflag=1;
if(s4cnt==0)
{
s4cnt=1;
s4time2=0;
s4timeflag2=1;
}
else if(s4cnt==1)
{
if(s4time2<250)
{
s4cnt=2;
}
}
while(P30==0)
{
if(s4time>1000)
{
s4cnt=0;
s4time2=0;
s4timeflag2=0;
s4key = 2;
delay1ms(200);
a=a+1;
if(a>9999999)a=0;
LED8[7]=10;
LED8[6]=a/1000000;
LED8[5]=(a % 1000000) / 100000;
LED8[4]=(a % 100000) / 10000;
LED8[3]=(a % 10000) / 1000;
LED8[2]=(a % 1000) / 100;
LED8[1]=(a % 100) / 10;
LED8[0]=(a % 10) / 1;
}
}
s4timeflag=0;
s4time=0;
}
}
if(s4key==1)
{
if(s4cnt==2)
{
s4key=0;
s4cnt=0;
a=a+10;
LED8[7]=12;
LED8[6]=a/1000000;
LED8[5]=(a % 1000000) / 100000;
LED8[4]=(a % 100000) / 10000;
LED8[3]=(a % 10000) / 1000;
LED8[2]=(a % 1000) / 100;
LED8[1]=(a % 100) / 10;
LED8[0]=(a % 10) / 1;
}
else if(s4cnt==1 && s4time2>200)
{
s4key=0;
s4cnt=0;
s4time2=0;
s4timeflag2=0;
a=a+1;
LED8[7]=11;
LED8[6]=a/1000000;
LED8[5]=(a % 1000000) / 100000;
LED8[4]=(a % 100000) / 10000;
LED8[3]=(a % 10000) / 1000;
LED8[2]=(a % 1000) / 100;
LED8[1]=(a % 100) / 10;
LED8[0]=(a % 10) / 1;
}
}
else if(P31==0)
{
delay1ms(10);
if(P31==0)
{
u8led=bitT(u8led,1);
LEDdisp();
LED8[0]=2;
while(P31==0);
}
}
else if(P32==0)
{
delay1ms(10);
if(P32==0)
{
u8led=bitT(u8led,2);
LEDdisp();
LED8[0]=3;
while(P32==0);
}
}
else if(P33==0)
{
delay1ms(10);
if(P33==0)
{
u8led=bitT(u8led,3);
LEDdisp();
LED8[0]=4;
while(P33==0);
}
}
}
}
void hc573(u8 x)
{
if(x==0)P2=P2&0x1F;
else if(x==LY)P2= P2&0x1F | 0x80 ;
else if(x==CY)P2= P2&0x1F | 0xC0 ;
else if(x==AY)P2= P2&0x1F | 0xe0 ;
else if(x==UY)P2= P2&0x1F | 0xa0 ;
}
u8 bit1(u8 x,u8 i)
{
u8 a;
a=(0x01<<i)|x;
return a;
}
u8 bit0(u8 x,u8 i)
{
u8 a;
a=(~(0x01<<i))&x;
return a;
}
u8 bitT(u8 x,u8 i)
{
u8 a;
a=x^(1<<i);
return a;
}
u8 bitR(u8 x,u8 i)
{
u8 a;
a=(x>>i) &0x01;
return a;
}
void LEDsta(u8 pos, u8 sta)
{
if(sta==ledon)
{
u8led = bit1(u8led,pos);
}
else if(sta==ledoff)
{
u8led = bit0(u8led,pos);
}
}
void LEDdisp(void)
{
hc573(0);
P0=~u8led;
hc573(LY);
hc573(LY);
hc573(0);
}
void delay1ms(int nms)
{
unsigned char i, j;
for(;nms>0;nms--)
{
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
}
void DisplayScan(void)
{
hc573(0);
P0=T_COM[display_index];
hc573(CY);
hc573(CY);
hc573(0);
hc573(0);
P0=~t_display[LED8[display_index]];
hc573(AY);
hc573(AY);
hc573(0);
if(++display_index >= 8) display_index = 0;
}
void tm1_isr() interrupt 3
{
if(s4timeflag==1)
{
s4time++;
if(s4time==100000)s4time=1000;
}
if(s4timeflag2==1)
{
s4time2++;
if(s4time2>1000)
{
s4timeflag2=0;
s4time2=0;
}
}
DisplayScan();
B_1ms = 1;
}