/*****************************************************************
題目:籃球比賽計(jì)時(shí)記分系統(tǒng)
硬件:STC89C52RC、1602LCM液晶屏、六個(gè)按鍵
軟件:Keil C
*****************************************************************/
#include //頭文件
#define uchar unsigned char //宏定義
#define uint unsigned int
sbit S1=P3^2; //中斷0(比賽倒計(jì)時(shí)開(kāi)始/暫停)
sbit S2=P3^3; //中斷1(24s倒計(jì)時(shí)重新開(kāi)始)
sbit key="P2"^4; //連接S3、S4、S5、S6(矩陣按鍵)
sbit key_S3=P2^3; //連接S3
sbit key_S4=P2^2; //連接S4
sbit key_S5=P2^1; //連接S5
sbit key_S6=P2^0; //連接S6
sbit FM="P1"^5; //蜂鳴器接口
sbit EN="P1"^0;
sbit RS="P1"^1;
char sec,min,num,time,sec_24s;
uchar hpoint,rpoint;
uchar code table1[]={"H.T 000:000 R.T "};
//H.T(home team)代表主隊(duì),R.T(road team)代表客隊(duì),中間是比分
uchar code table2[]={"12:00 SEC-1 24"};
//SEC-X代表第X節(jié)比賽,左邊是單節(jié)比賽倒計(jì)時(shí),右邊是24秒倒計(jì)時(shí)
/***1ms延時(shí)子程序***/
void delay(int z)
{
int x;
uchar y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/***LCD寫指令***/
void write_com(uchar com)
{
RS=0;
P0=com;
delay(5);
EN=1;
delay(5);
EN=0;
}
/***LCD寫數(shù)據(jù)***/
void write_dat(uchar dat)
{
RS=1;
P0=dat;
delay(5);
EN=1;
delay(5);
EN=0;
}
/***初始化程序***/
void init(void)
{
uchar i;
hpoint=0; //H.T分?jǐn)?shù)初始化
rpoint=0; //R.T分?jǐn)?shù)初始化
time=0;
TMOD=0x10; //定時(shí)器1初始化
TL1=0x00;
TH1=0x4c;
EA=1; //開(kāi)總中斷
ET1=1; //開(kāi)定時(shí)器1
TR1=0; //定時(shí)器1不工作
EX0=1; //開(kāi)中斷0
EX1=1; //開(kāi)中斷1
IT0=1; //中斷0為邊沿觸發(fā)
IT1=1; //中斷1為邊沿觸發(fā)
EN=0;
sec=0;
min=12;
num=1;
sec_24s=24;
write_com(0x38); //LCD設(shè)置初始化
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80); //LCD顯示初始化
for(i=0;i<16;i++)
{
write_dat(table1[i]);
}
write_com(0x80+0x40);
for(i=0;i<16;i++)
{
write_dat(table2[i]);
}
}
/***LCD分?jǐn)?shù)更新***/
void point_lcd(uchar add,uchar dat)
{
write_com(0x80+add);
write_dat(0x30+dat/100);
write_dat(0x30+(dat%100)/10);
write_dat(0x30+dat%10);
}
/***按鍵檢測(cè)***/
void keyscan(void)
{
key=0;
if(key_S3==0) //S3按下H.T分?jǐn)?shù)加一
{
hpoint++;
point_lcd(0x04,hpoint); //分?jǐn)?shù)顯示更新
if(key_S3==0) //松手檢測(cè)
{
while(key_S3==0);
delay(20);
}
}
else if(key_S4==0) //S4按下H.T分?jǐn)?shù)減一
{
hpoint--;
point_lcd(0x04,hpoint);
if(key_S4==0)
{
while(key_S4==0);
delay(20);
}
}
else if(key_S5==0) //S5按下R.T分?jǐn)?shù)加一
{
rpoint++;
point_lcd(0x08,rpoint);
if(key_S5==0)
{
while(key_S5==0);
delay(20);
}
}
else if(key_S6==0) //S6按下R.T分?jǐn)?shù)減一
{
rpoint--;
point_lcd(0x08,rpoint);
if(key_S6==0)
{
while(key_S6==0);
delay(20);
}
}
}
/***比賽倒計(jì)時(shí)/24s倒計(jì)時(shí)(同步)***/
void counter_down(void)
{
uchar i;
if(time>=20) //每1s倒計(jì)時(shí)做減一操作
{
sec--;
sec_24s--;
write_com(0x80+0x4e); //24s倒計(jì)時(shí)顯示
write_dat(0x30+sec_24s/10);
write_dat(0x30+sec_24s%10);
if(sec_24s==0) //24s結(jié)束發(fā)出3s連續(xù)報(bào)警
{
FM="0";
delay(3000);
FM="1";
sec_24s=24;
}
if((sec==0)&&(min==0)) //檢測(cè)一節(jié)比賽是否結(jié)束
{
TR1=0; //定時(shí)器1暫停
write_com(0x80+0x44);
write_dat(0x30);
num++;
sec_24s=24; //24s計(jì)時(shí)復(fù)位
write_com(0x80+0x4e); //24s倒計(jì)時(shí)顯示
write_dat(0x30+sec_24s/10);
write_dat(0x30+sec_24s%10);
if(num<5) //每節(jié)結(jié)束蜂鳴器發(fā)出8s的間斷報(bào)警
{
for(i=80;i>0;i--)
{
FM="0";
delay(500);
FM="1";
delay(500);
}
}
if(num==5) //終場(chǎng)結(jié)束,蜂鳴器發(fā)出10s的連續(xù)警報(bào)聲
{
FM="0";
delay(10000);
num="1";
}
FM="1"; //蜂鳴器關(guān)閉
write_com(0x80+0x4b); //更新"SEC-?"
write_dat(0x30+num);
sec="0"; //倒計(jì)時(shí)復(fù)位
min="12";
}
if(sec==-1)
{
sec="59";
min--;
}
write_com(0x80+0x40); //更新倒計(jì)時(shí)顯示
write_dat(0x30+min/10);
write_dat(0x30+min%10);
write_com(0x80+0x43);
write_dat(0x30+sec/10);
write_dat(0x30+sec%10);
time=0;
}
}
/***主程序***/
void main()
{
init();
while(1)
{
keyscan(); //分?jǐn)?shù)按鍵檢測(cè)
}
}
/***S1按鍵中斷0***/
void exter0() interrupt 0 //比賽時(shí)間開(kāi)始/暫停
{
TR1=~TR1; //定時(shí)器1工作/暫停
if(TR1==1) //當(dāng)?shù)褂?jì)時(shí)工作時(shí),S1按下定時(shí)器立即停止工作
{
PT1=0;
}
else //倒計(jì)時(shí)不工作時(shí),S1按下倒計(jì)時(shí)立即工作
{
PT1=1;
}
if(S1==0) //松手檢測(cè)
{
while(S1==0)
{
counter_down();
}
delay(20);
}
}
/***S2按鍵中斷1***/
void exter1() interrupt 2 //24s倒計(jì)時(shí)重新開(kāi)始
{
sec_24s=24;
write_com(0x80+0x4e); //24s倒計(jì)時(shí)顯示
write_dat(0x30+sec_24s/10);
write_dat(0x30+sec_24s%10);
if(S2==0) //松手檢測(cè)
{
while(S2==0)
{
counter_down();
}
delay(20);
}
}
/***定時(shí)器1中斷***/
void timer1() interrupt 3 //定時(shí)器1中斷20次為1s
{
time++;
TL1=0x00;
TH1=0x4c;
counter_down(); //倒計(jì)時(shí)
}
?
評(píng)論
查看更多