|
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
uchar code tab[] ={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92,
0x82,0xf8,0x80,0x90,0xff}; //0,12,3,4,5,6,7,8,9,關(guān)顯示
uchar b,d,t; //定義變量
uchar fen=10,miao=0; //定時初始時間變量
uchar flag; //標(biāo)志位
uchar temp; //矩形鍵盤鍵值
sbit beep =P1^7 ; //蜂鳴器
void delay(uint z) //延時函數(shù)
{
uint x,y;
for(x=z;x>0;x--)
for( y= 110;y>0;y--);
}
void dispaly() //定時時間顯示
{
uchar miaoge,fenge,miaoshi,fenshi;
miaoge = miao%10;
P2 = 0x80;;
P0 =tab[miaoge];
delay(1);
miaoshi = miao/10;
P2 =0x40;
P0 =tab[miaoshi];
delay(1);
fenge = fen%10;
P2=0x20;
P0 =tab[fenge];
delay(1);
fenshi =fen/10;
P2 =0x10;
P0 =tab[fenshi];
delay(1);
P2 =0x00;
}
void dispaly1(char a) //甲隊比分顯示
{
uchar ge1,shi1;
b=a;
ge1=b%10;
P2=0x02;
P0=tab[ge1];
delay(1);
shi1=b/10;
P2=0x01;
P0=tab[shi1];
delay(1);
P2=0x00;
}
void dispaly2(char c) //乙隊比分顯示
{
uchar ge2,shi2;
d=c;
ge2 =d%10;
P2=0x08;
P0=tab[ge2];
delay(1);
shi2 =d/10;
P2 = 0x04;
P0 =tab[shi2];
delay(1);
P2 =0x80;
}
void keyscan() //矩陣鍵盤掃描控制
{
char a,c,e,f;
dispaly1(a);
dispaly2(c);
P3 =0xfe;
temp=P3;
temp =temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp =P3;
temp =temp&0xf0;
while(temp!=0xf0)
{
temp =P3;
if(temp==0xee)
{
delay(5);
if(temp == 0xee)
{
a++; //甲隊比分加1
if(a>=100)
a=99;
dispaly1(a);
}
}
if(temp==0xde)
{
delay(5);
if(temp==0xde)
{
a=a+2; //甲隊比分加2
if(a>=100)
a=99;
dispaly1(a);
}
}
if(temp==0xbe)
{
delay(5);
if(temp==0xbe)
{
a=a+3; //甲隊比分加3
if(a>=100)
a=99;
dispaly1(a);
}
}
if(temp ==0x7e)
{
delay(5);
if(temp ==0x7e)
{
a--; //甲隊比分減1
if(a<= -1)
a=0;
dispaly1(a);
}
}
while(temp!= 0xf0) //松手檢測
{
temp =P3;
temp=temp&0xf0;
dispaly1(a);
}
}
}
P3=0xfd;
temp=P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp =P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp =P3;
if(temp==0xed)
{
delay(5);
if(temp ==0xed)
{
c++; //乙隊比分加1
if(c>=100)
c=99;
dispaly2(c);
}
}
if(temp==0xdd)
{
delay(5);
if(temp==0xdd)
{
c= c+2; //乙隊比分加2
if(c>=100)
c=99;
dispaly2(c);
}
}
if(temp==0xbd)
{
delay(5);
if(temp ==0xbd)
{
c=c+3; //乙隊比分3
if(c>=100)
c=99;
dispaly2(c);
}
}
if(temp ==0x7d)
{
delay(5);
if(temp ==0x7d)
{
c--; //乙隊比分減1
if(c <= -1)
c=0;
dispaly2(c);
}
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
dispaly2(c);
}
}
}
P3=0xfb;
temp=P3;
temp =temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp =P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
temp =P3;
if(temp==0xeb)
{
delay(5);
if(temp ==0xeb)
{
a=0; //雙方比分清零
dispaly1(a);
c=0;
dispaly2(c);
}
}
if(temp == 0xdb)
{
delay(5);
if(temp ==0xdb)
{
e=a;
f=c;
a=f;
dispaly1(a); //雙方比分切換
c=e;
dispaly2(c);
}
}
if(flag!=1) //避免誤操作,只有在時間停止的情況下才能加/減定時時間
{
if(temp==0xbb)
{
delay(5);
if(temp==0xbb)
{
fen++; // 定時時間加1
if(fen==99)
fen=0;
}
}
if(temp ==0x7b)
{
delay(5);
if(temp==0x7b)
{
fen--; //定時時間減1
if(fen== -1)
fen=99;
}
}
}
while(temp!=0xf0)
{
temp=P3;
temp =temp&0xf0;
dispaly2(c);
}
}
}
P3=0xf7;
temp =P3;
temp=temp&0xf0;
while(temp!=0xf0)
{
delay(5);
temp=P3;
temp =temp&0xf0;
while(temp!=0xf0)
{
temp=P3;
if(temp==0xe7)
{
delay(5);
if(temp==0xe7)
{
TR0=1; //比賽開始計時按鍵
flag=1;
}
}
if(temp==0xd7)
{
delay(5);
if(temp==0xd7)
{
TR0=0; //比賽暫停計時按鍵
flag=0;
}
}
if(temp==0xb7)
{
delay(5);
if(temp==0xb7)
{
fen=0; //比賽時間清零
miao=0;
}
}
while(temp!=0xf0)
{
temp=P3;
temp=temp&0xf0;
dispaly2(c);
}
}
}
}
void timer0(void)interrupt 1 //T0中斷服務(wù)
{
TH0=0x4c; //50ms延時初值
TL0=0x00;
t++;
if(t==20) //50ms走20次剛好1S,1s時間到,減1操作
{
t=0;
miao--;
if(miao == -1)
{
fen--;
miao=59; //當(dāng)59s減完,分減1
}
if(fen == -1)
{
fen=0; //分清零
miao=0;
beep=0; //時間停止蜂鳴器響
}
}
}
void init() //T0中斷初始化
{
TMOD=0x01;
TH0= (65536-50000)/256;
TL0= (65536-50000)%256;
ET0=1;
EA=1;
TR0=0;
}
void main() //主程序
{
P2=0xfe;
init();
while(1)
{
dispaly();
keyscan();
}
}
圖看我頭像 希望對你有幫助 |
|