- #include <reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit m1=P3^1; //主持人按键答对加分,按一下加一分
- sbit m2=P3^2; //答错减分
- sbit k1=P1^0;
- sbit k2=P1^1;
- sbit k3=P1^2;
- sbit k4=P1^3;
- sbit k5=P1^4;
- sbit k6=P1^5;
- sbit k7=P1^6;
- sbit k8=P1^7; //选手按键
- sbit k=P3^0; //主持人按键开始
- sbit beep=P3^7; //蜂鸣器
- uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77}; //0-9代码
- uchar shiwei,gewei,xuanshou,count,i,second;
- uchar score1,score2,score3,score4,score5,score6,score7,score8;//选手1~8的分数
- uint t,m,n,a,a1,a2,a3,a4,a5,a6,a7,a8;
- void Timer()
- {
- TMOD|=0x01;
- TH0=0xd8; //初值55536,计数10000次,每次1US,总计10ms
- TL0=0xf0;
- IE=0x82; //这里是中断优先级控制EA=1(开总中断),ET0=1(定时器0允许中断),这里用定时器0来定时
- TR0=1;
- }
- void tim(void) interrupt 1 using 1 //为定时中断TR0
- {
- TH0=0xd8; //重新赋值
- TL0=0xf0;
- count++;
- if(count==100) //100*10ms=1秒
- {
- count=0;
- second--; //秒减1
- }
- }
- void delay(uint z) //延时函数
- {
- uint x,y;
- for(x=z;x>0;x--)
- for(y=10;y>0;y--);
- }
- void display (uchar shiwei,gewei,xuanshou) //显示函数
- {
- P2=0xfe; //打开显示时间十位的位选
- P0=shiwei; //显示十位
- delay(5);
- P2=0xfd; //个位位选
- P0=gewei;
- delay(5);
- P2=0xf7; //选手位选
- P0=xuanshou;
- delay(5);
- }
- void dis(void)
- {
- shiwei=tab[second/10];
- gewei=tab[second%10];
- xuanshou=tab[i];
- display(shiwei,gewei,xuanshou);
- }
- void init() //初始化函数
- {
- P2=0x04;
- P0=0x3f;
- t=0;
- beep=0;
- i=0;
- second=29;
- score1=60;
- score2=60;
- score3=60;
- score4=60;
- score5=60;
- score6=60;
- score7=60;
- score8=60;
- a1=1;
- a2=1;
- a3=1;
- a4=1;
- a5=1;
- a6=1;
- a7=1;
- a8=1;
- }
- void main()
- {
- init();
- if(k==0) //主持人按下开始键
- {
- Timer();
- delay(10);
- while(!k);
- while(1)
- {
- dis();
- m=0;
- n=0;
- if(second==10){beep=1; delay(500); beep=0;}
- if(second==0) {TR0=0; n=1;
- if(k==0)
- {beep=1; delay(500); beep=0; second=29; TR0=1; m=1;}
- }
- if(n==0)
- { if(k1==0) {delay(5);if(k1==0);while(!k1);t=1;}
- else if(k2==0) {delay(5);if(k2==0);while(!k2);t=2;}
- else if(k3==0) {delay(5);if(k3==0);while(!k2);t=3;}
- else if(k4==0) {delay(5);if(k4==0);while(!k2);t=4;}
- else if(k5==0) {delay(5);if(k5==0);while(!k2);t=5;}
- else if(k6==0) {delay(5);if(k6==0);while(!k2);t=6;}
- else if(k7==0) {delay(5);if(k7==0);while(!k2);t=7;}
- else if(k8==0) {delay(5);if(k8==0);while(!k2);t=8;}
- else if(k==0) {delay(5);if(k==0);while(!k);t=0;i=0;second=29;beep=1;delay(500);beep=0;TR0=1;}
- }
- while(t)
- { if(m==0) //开关,保证程序只执行一次
- { switch(t)
- {
- case 1:
- { TR0=0;i=1;second=0;beep=1;delay(500);beep=0;
- a1=0;a2=1;a3=1;a4=1;a5=1;a6=1;a7=1;a8=1;m=1;break;
- }
- case 2:
- { TR0=0;i=2;second=0;beep=1;delay(500);beep=0;
- a2=0;a1=1;a3=1;a4=1;a5=1;a6=1;a7=1;a8=1;m=1;break;
- }
- case 3:
- { TR0=0;i=3;second=0;beep=1;delay(500);beep=0;
- a3=0;a1=1;a2=1;a4=1;a5=1;a6=1;a7=1;a8=1;m=1;break;
- }
- case 4:
- { TR0=0;i=4;second=0;beep=1;delay(500);beep=0;
- a4=0;a1=1;a2=1;a3=1;a5=1;a6=1;a7=1;a8=1;m=1;break;
- }
- case 5:
- { TR0=0;i=5;second=0;beep=1;delay(500);beep=0;
- a5=0;a1=1;a2=1;a3=1;a4=1;a6=1;a7=1;a8=1;m=1;break;
- }
- case 6:
- { TR0=0;i=6;second=0;beep=1;delay(500);beep=0;
- a6=0;a1=1;a2=1;a3=1;a4=1;a5=1;a7=1;a8=1;m=1;break;
- }
- case 7:
- { TR0=0;i=7;second=0;beep=1;delay(500);beep=0;
- a7=0;a1=1;a2=1;a3=1;a4=1;a5=1;a6=1;a8=1;m=1;break;
- }
- case 8:
- { TR0=0;i=8;second=0;beep=1;delay(500);beep=0;
- a8=0;a1=1;a2=1;a3=1;a4=1;a5=1;a6=1;a7=1;m=1;break;
- }
- }
- }
- if(a1==0) //选手1的分数
- {
- second=score1;
- dis();
- if(m1==0) {delay(5);if(m1==0);while(!m1);beep=1;delay(100);beep=0; score1+=1;if(score1==99)score1=60;}
- else if(m2==0) {delay(5);if(m2==0);while(!m2);beep=1;delay(100); beep=0;score1-=1;if(score1==0)score1=60;}
- dis();
- }
- else if(a2==0)
- {
- second=score2;
- dis();
- if(m1==0) {delay(5);if(m1==0);while(!m1);beep=1;delay(100);beep=0; score2+=1;if(score2==99)score2=60;}
- else if(m2==0) {delay(5);if(m2==0);while(!m2);beep=1;delay(100); beep=0;score2-=1;if(score2==0)score2=60;}
- dis();
- }
- else if(a3==0)
- {
- second=score3;
- dis();
- if(m1==0) {delay(5);if(m1==0);while(!m1);beep=1;delay(100);beep=0; score3+=1;if(score3==99)score3=60;}
- else if(m2==0) {delay(5);if(m2==0);while(!m2);beep=1;delay(100); beep=0;score3-=1;if(score3==0)score3=60;}
- second=score3;
- dis();
- }
- else if(a4==0)
- {
- second=score4;
- dis();
- if(m1==0) {delay(5);if(m1==0);while(!m1);beep=1;delay(100);beep=0; score4+=1;if(score4==99)score4=60;}
- else if(m2==0) {delay(5);if(m2==0);while(!m2);beep=1;delay(100); beep=0;score4-=1;if(score4==0)score4=60;}
- second=score4;
- dis();
- }
- else if(a5==0)
- {
- second=score5;
- dis();
- if(m1==0) {delay(5);if(m1==0);while(!m1);beep=1;delay(100);beep=0; score5+=1;if(score5==99)score5=60;}
- else if(m2==0) {delay(5);if(m2==0);while(!m2);beep=1;delay(100); beep=0;score5-=1;if(score5==0)score5=60;}
- second=score5;
- dis();
- }
- else if(a6==0)
- {
- second=score6;
- dis();
- if(m1==0) {delay(5);if(m1==0);while(!m1);beep=1;delay(100);beep=0; score6+=1;if(score6==99)score6=60;}
- else if(m2==0) {delay(5);if(m2==0);while(!m2);beep=1;delay(100); beep=0;score6-=1;if(score6==0)score6=60;}
- second=score6;
- dis();
- }
- else if(a7==0)
- {
- second=score7;
- dis();
- if(m1==0) {delay(5);if(m1==0);while(!m1);beep=1;delay(100);beep=0; score7+=1;if(score7==99)score7=60;}
- else if(m2==0) {delay(5);if(m2==0);while(!m2);beep=1;delay(100); beep=0;score7-=1;if(score7==0)score7=60;}
- second=score7;
- dis();
- }
- else if(a8==0)
- {
- second=score8;
- dis();
- if(m1==0) {delay(5);if(m1==0);while(!m1);beep=1;delay(100);beep=0; score8+=1; if(score8==99)score8=60;}
- else if(m2==0) {delay(5);if(m2==0);while(!m2);beep=1;delay(100); beep=0;score8-=1; if(score8==0)score8=60;}
- second=score8;
- dis();
- }
- if(k==0){i=0;t=0;second=29;beep=1;delay(500);beep=0;TR0=1;}
- while(!k);
- }
- }
- }
- }
复制代码 |