#include<REG52.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int /************************變量定義************************************/ sbit sw1=P1^0; sbit sw2=P1^1; sbit sw3=P1^2; sbit sw4=P1^3; sbit sw5=P1^4; sbit sw6=P1^5; sbit sw7=P1^6; sbit sw8=P1^7; sbit start_button=P3^0; sbit props_button=P3^1; sbit snatch_button=P3^2; sbit answer_button=P3^3; sbit push_button=P3^4; sbit reduce_button=P3^5; sbit stop_button=P3^6; sbit BEEP=P3^7; uint flag=0; uint snatch_flag=1; uint answer_flag=0; uint a,b,c,d,x; ucharstate[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x71,0x76,0x00,0x79,0x54,0x5e};/**數據依次表示數字0--9和字母F、H,以及 End。**/ /************************函數聲明***********************************/ void initUart(); //初始化函數 void time(uint ucMs); //延時函數 void Play(); //蜂鳴器發聲函數 void Start(); //開始函數 void Stop(); //終止函數 void Props(); //道具函數 void Snatch(); //搶答置數函數 void Answer(); //回答置數函數 void Push(); //時間加1S函數 void Reduce(); //時間減1S函數 void DisPlay(); //顯示器顯示函數 void Keyscan(); //選手按鈕掃描函數 /**************************主函數**********************************/ void main() { x=30; initUart(); while(1) { if(!flag) { Keyscan(); } if(a!=10&&!flag) { for(;a!=12;) { Play(); } } if(!start_button&&a==10) { flag=1; Start(); } if(!snatch_button) { flag=0; Snatch(); } if(!answer_button) { flag=1; Answer(); } if(!push_button) { Push(); } if(!reduce_button) { Reduce(); } DisPlay(); } } /**************************初始化程序**********************************/ void initUart() { a=10,b=10,c=10,d=10; BEEP=0; P2=0xee; P0=state[10]; time(1); P2=0xed; P0=state[10]; time(1); P2=0xeb; P0=state[10]; time(1); P2=0xe7; P0=state[10]; time(1); } /**************************延時程序**********************************/ /******************delay_5us*********************/ void delay_5us(void) //延時5us { _nop_(); _nop_(); } /******************delay_50us********************/ void delay_50us(void) //延時50us { unsigned char i; for(i=0;i<4;i++) { delay_5us(); } } /******************延時100us********************/ void delay_100us(void) //延時100us { delay_50us(); delay_50us(); } /****************延時單位:ms******************/ voidtime(unsigned int ucMs) //延時1ms { unsigned char j; while(ucMs>0) { for(j=0;j<10;j++) delay_100us(); ucMs--; } } /********************蜂鳴器發聲程序**************************/ void Play() { uchar i; for(i=1;i<100;i++) { BEEP=~BEEP; DisPlay(); if(!stop_button) { Stop(); } if(!props_button&&snatch_flag==0&&answer_flag==1) { Props(); } elseif(snatch_flag==1&&answer_flag==0) { Keyscan(); } } for(i=1;i<150;i++) { DisPlay(); if(!stop_button) { Stop(); } if(!props_button&&snatch_flag==0&&answer_flag==1) { Props(); } elseif(snatch_flag==1&&answer_flag==0) { Keyscan(); } } BEEP=0; } /**********************搶答開始程序*****************************/ void Start() { uint i,j,y=x; for(i=0;i<=y&&a==10;i++) { c=x/10; d=x%10; x--; if(x<5) { Play(); } else { for(j=1;j<250&&a==10;j++) { DisPlay(); if(!stop_button) { Stop(); } if(!props_button&&snatch_flag==0&&answer_flag==1) { Props(); } elseif(snatch_flag==1&&answer_flag==0) { Keyscan(); } } } } } /********************************終止程序************************************/ void Stop() { a=12; b=13; c=14; d=15; flag=1; } /***************道具使用程序(回答時可以重置倒計時時間)**********************/ void Props() { x=x+16; c=x/10; d=x%10; while(!props_button) { DisPlay(); } } /**********************搶答置數程序***************************************/ void Snatch() { x=30; a=10; b=10; c=3; d=0; snatch_flag=1; answer_flag=0; } /**********************回答置數程序***************************************/ void Answer() { x=60; a=10; b=10; c=6; d=0; snatch_flag=0; answer_flag=1; } /**********************時間加1S程序***************************************/ void Push() { uint y; x++; y=x; c=y/10; d=y%10; if(x==99) { x=0; } while(!push_button) { DisPlay(); } } /**********************時間減1S程序***************************************/ void Reduce() { x=x-1; time(1); c=x/10; d=x%10; if(x==-1) { x=99; } while(!reduce_button) { DisPlay(); } } /**********************按鍵掃描程序***************************************/ void Keyscan() { if(sw1==0) { a=1; b=11; } if(sw2==0) { a=2; b=11; } if(sw3==0) { a=3; b=11; } if(sw4==0) { a=4; b=11; } if(sw5==0) { a=5; b=11; } if(sw6==0) { a=6; b=11; } if(sw7==0) { a=7; b=11; } if(sw8==0) { a=8; b=11; } } /*************************顯示器顯示程序************************************/ void DisPlay() { P2=0xee; P0=state[a]; time(1); P2=0xed; P0=state; time(1); P2=0xeb; P0=state[c]; time(1); P2=0xe7; P0=state[d]; time(1); }
|