#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar code tab1[]="A :";
uchar code tab2[]="B ";
uchar code tab3[]=" GAME OVER "; //????????????
uchar code tab4[]=" A B "; //????????????
sbit lcden=P2^7;
sbit lcdrw=P2^5; //1602?????3?????
sbit lcdrs=P2^6;
sbit key1=P1^0;
sbit key2=P1^1; //???????
sbit key3=P1^2;
sbit key4=P1^3;
sbit Bell=P2^7; //??????
uint keyflag,flag; //???λ
uchar i,num,count;
uchar sec,min,miao;
uchar Ascore,Bscore; //????÷?
/*******************???????****************************/
void delay(uint z) //??????????
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com) //?????????
{
lcdrs=0; //???????
P0=com; //??P0?????????
delay(5);
lcden=1; //E???????
delay(5); //??????????????????
lcden=0; //E??????????????????
}
void write_dat(uchar date) //?????????data
{
lcdrs=1; //????????
P0=date; //??P0????????
delay(5);
lcden=1; //E???????
delay(5); //???????????????????
lcden=0; //E??????????????????
}
void write_time(uchar add,uchar date) //д????亯??
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+add);
write_dat(0x30+shi);
write_dat(0x30+ge);
}
void write_miao(uchar add,uchar date) //д??24????
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_dat(0x30+shi);
write_dat(0x30+ge);
}
void write_Ascore(uchar add,uchar date) //д??A??÷????
{
uchar bai,shi,ge;
bai=date/100;
shi=date/10%10;
ge=date%10;
write_com(0x80+add);
write_dat(0x30+bai);
write_dat(0x30+shi);
write_dat(0x30+ge);
}
void write_Bscore(uchar add,uchar date) //д??B??÷????
{
uchar bai,shi,ge;
bai=date/100;
shi=date/10%10;
ge=date%10;
write_com(0x80+0x40+add);
write_dat(0x30+bai);
write_dat(0x30+shi);
write_dat(0x30+ge);
}
void write_score(uchar add,uchar date) //????????д??÷????
{
uchar bai,shi,ge;
bai=date/100;
shi=date/10%10;
ge=date%10;
write_com(0x80+0x40+add);
write_dat(0x30+bai);
write_dat(0x30+shi);
write_dat(0x30+ge);
}
void delay_Bell(uint z) //?????????????
{
uint x,y;
for(x=z;x>0;x--)
for(y=255;y>0;y--);
}
void init() //?????????
{
Bell=1;
keyflag=0;
flag=0;
min=12;
sec=0;
miao=25;
Ascore=0;
Bscore=0;
lcdrw=0;
lcden=0; //E????????????????????????????????E???????
write_com(0x38); //д??????????
write_com(0x0e); //??????????????????
write_com(0x06); //?????????????????????
write_com(0x01); //???????
write_com(0x80+0x00); //??????????λ??
for(num=0;num<16;num++)
{
write_dat(tab1[num]);
}
write_com(0x80+0x40); //???????53?????????????????
for(num=0;num<2;num++)
{
write_dat(tab2[num]);
}
write_Ascore(2,Ascore);
write_Bscore(2,Bscore);
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1; ET0=1;
TR0=1; //???????
}
void keyscan() //????????
{
if(key1==0)
{
delay(10);
if(key1==0)
{
keyflag++; //??????£????λ???
while(!key1);
if(keyflag==1)
{
TR0=0;
} //???ж?
if(keyflag==2)
{
TR0=1; keyflag=0;
}
}
}
if(key2==0) //???24??
{
delay(5);
if(key2==0)
{
while(!key2);
miao=25;
}
}
if(key3==0)
{
delay(5);
if(key3==0)
{
while(!key3);
Ascore++;
write_Ascore(2,Ascore);
} //д??A??÷?
}
if(key4==0)
{
delay(5);
if(key4==0)
{
while(!key4);
Bscore++;
write_Bscore(2,Bscore);
} //д??B??÷?
}
}
void main()
{
init();
while(1)
{
keyscan();
if(flag==0)
{
write_com(0x80+0x40+7); //д?????? FIR
write_dat('F');
write_dat('I');
write_dat('R');
}
if(flag==1)
{
write_com(0x80+0x40+7); //д?????? SEC
write_dat('S');
write_dat('E');
write_dat('C');
}
if(flag==2)
{
write_com(0x80+0x40+7); //д??????? THR
write_dat('T');
write_dat('H');
write_dat('R');
}
if(flag==3)
{
write_com(0x80+0x40+7); //д?????? FOU
write_dat('F');
write_dat('O');
write_dat('U');
}
if(flag==4) //?????????????GAME OVER??????????
{
TR0=0;
write_com(0x80);
for(i=0;i<16;i++)
{
write_dat(tab3[i]); //"GAME OVER"?????????
}
write_com(0x80+0x40); //???????53?????????????????
for(i=0;i<16;i++)
{
write_dat(tab4[i]);
}
write_score(5,Ascore); //????????д????????
write_score(11,Bscore);
}
if(miao>=4&&miao<=5) //????5??????????1??
{
Bell=0; delay_Bell(510);
Bell=1; delay_Bell(510);
}
}
}
void timer0() interrupt 1 //?????
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==18)
{
count=0;
sec--;
miao--;
if(miao==(-1)) //??24????????????4??
{
Bell=0;
delay_Bell(510); delay_Bell(510);
delay_Bell(510); delay_Bell(510);
Bell=1; }
if(miao==(-1))
{
miao=24;
}
if(sec==((-1)))
{
sec=59;min--;
if(min==(-1)) //?????(????)??????????????4??
{
Bell=0;
delay_Bell(510);delay_Bell(510);
delay_Bell(510);delay_Bell(510);
Bell=1;
min=11; flag++;
}
}
write_time(11,min);
write_time(14,sec);
write_miao(14,miao);
} //??????????
}
|