这是我做了两个星期的毕业设计,四路竞赛抢答器,
附件里面包含了仿真图可直接下载打开
还有程序哦,请各位大神进行指教吧
仿真原理图如下(proteus仿真工程文件可到本帖附件中下载):
抢答器的设计论文:
单片机源程序如下:
- #include<reg51.h>
- #define uchar unsigned char
- #define uint unsigned int
- char s;
- uchar num=0;
- char time=20; //抢答时间
- char datitime=30; //答题限时时间
- uint tt,t1; //T0,T1定时器定一秒时
- bit flag,s_flag=1,b_flag,fall_flag; //标志位
- bit K_startcountflag,K_timecountflag; //时间调整标志位
- sbit K0=P3^0;
- sbit beep=P3^7; //蜂鸣器输出口
- sbit rled=P3^1; //指示灯输出口
- sbit K1=P1^0;
- sbit K2=P1^1;
- sbit K3=P1^2;
- sbit K4=P1^3;
- sbit K_Time=P3^2; //答题计时键
- sbit K_startcount=P3^3; //开始抢答时间调整键
- sbit K_timecount=P3^4; //答题计时时间调整键
- void delay(uchar ms)
- {
- uchar y;
- for(;ms>0;ms--)
- for(y=120;y>0;y--);
- }
- uchar code tabledu[]={0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71
- };
- uchar code tablewe[]={0XFE,0XFD,0XFB,0XF7};
- void T0_Init(void)
- {
- TMOD=0X01;
- TH0=(65536-2000)/256;
- TL0=(65536-2000)%256;
- TH1=(65536-2000)/256;
- TL1=(65536-2000)%256;
- ET0=1;
- ET1=1;
- EA=1;
- P0=0;
- }
- void Key_Scan(void) //开始键扫描
- {
- if(K0==0)
- {
- delay(10);
- if(K0==0)
- {
- while(!K0);
- TR0=1;
- s=time;
- tt=0;
- flag=1;
- s_flag=1;
- b_flag=1;
- num=0;
- beep=1;
- rled=1;
- fall_flag=0; //清除违规标志位
- K_startcountflag=0;
- K_timecountflag=0;
- }
- }
- }
- void Scan(void) //四路热键扫描(哪个键先按下,哪个优先级最高)
- {
- if(K1==0)
- {
- delay(10);
- if(K1==0)
- {
- while(!K1);
- num=1; //数码管显示1号"1"
- TR0=0; //关闭定时器0,时间停止
- TR1=1; //打开定时器1,使扬声器响一声
- s_flag=0; //关闭开始键标志位,使再按其他三个键不会响应
- }
- }
- if(K2==0) //下面三个键的处理同上
- {
- delay(10);
- if(K2==0)
- {
- while(!K2);
- num=2;
- TR0=0;
- TR1=1;
- s_flag=0; //重要
- }
- }
- if(K3==0)
- {
- delay(10);
- if(K3==0)
- {
- while(!K3);
- num=3;
- TR0=0;
- TR1=1;
- s_flag=0;
- }
- }
- if(K4==0)
- {
- delay(10);
- if(K4==0)
- {
- while(!K4);
- num=4;
- TR0=0;
- TR1=1;
- s_flag=0;
- }
- }
- }
- void display(void)
- {
- if(flag==1) //开始键按下,开始计时抢答
- {
- if(num!=0) //如果有人抢答,则显示相应的几号
- {
- P0=tabledu[num]; //显示几号抢到了
- P2=tablewe[0];
- delay(2);
- P0=0;
- P2=0XFF;
- }
- else //否则没人抢答,则前面不显示几号
- {
- P0=0;
- P2=0XFF;
- }
- P0=tabledu[s/10]; //下面为显示抢答倒计时
- P2=tablewe[2];
- delay(2);
- P0=0;
- P2=0XFF;
- P0=tabledu[s%10];
- P2=tablewe[3];
- delay(2);
- P2=0XFF;
- P0=0;
- }
- else //如果开始键没有按下,则显示F FF(若有违规者,则显示违规号码及 FF)或时间调整
- {
- if(fall_flag==1) //违规显示
- {
- if(num!=0)
- {
- P0=tabledu[num]; //显示几号违规了
- P2=tablewe[0];
- delay(2);
- P0=0;
- P2=0XFF;
- P0=tabledu[15]; //下面显示 FF,表示违规了
- P2=tablewe[2];
- delay(2);
- P0=0; //消隐
- P2=0XFF;
- P0=tabledu[15];
- P2=tablewe[3];
- delay(2);
- P0=0;
- P2=0XFF;
- }
- else
- {
- P0=0;
- P2=0XFF;
- }
- }
- else //没有人违规才显示调整时间
- {
- if(K_startcountflag==1)
- {
- P0=0X6D; //第一位数码管显示"5(S)"(表示抢答时间调整)
- P2=tablewe[0];
- delay(2);
- P0=0;
- P2=0XFF;
- P0=tabledu[time/10]; //下面显示调整的抢答时间
- P2=tablewe[2];
- delay(2);
- P0=0;
- P2=0XFF;
- P0=tabledu[time%10];
- P2=tablewe[3];
- delay(2);
- P0=0;
- P2=0XFF;
- }
- else if(K_timecountflag==1)
- {
- P0=0X07; //第一位与第二位数码管合起来显示"T",表示答题时间调整
- P2=tablewe[0];
- delay(2);
- P0=0;
- P2=0XFF;
- P0=0X31;
- P2=tablewe[1];
- delay(2);
- P0=0;
- P2=0XFF;
- P0=tabledu[datitime/10];
- P2=tablewe[2];
- delay(2);
- P0=0;
- P2=0XFF;
- P0=tabledu[datitime%10];
- P2=tablewe[3];
- delay(2);
- P0=0;
- P2=0XFF;
- }
- else //否则显示F FF
- {
- P0=tabledu[15];
- P2=tablewe[0];
- delay(2);
- P0=0;
- P0=tabledu[15];
- P2=tablewe[2];
- delay(2);
- P0=0; //消隐
- P2=0XFF;
- P0=tabledu[15];
- P2=tablewe[3];
- delay(2);
- P0=0;
- P2=0XFF;
- }
- }
- }
- }
- void Time_Scan(void) //调整时间键扫描
- {
- if(K_startcount==0) //抢答时间调整
- {
- delay(10);
- if(K_startcount==0)
- {
- while(!K_startcount);
- time++;
- if(time==50)
- {
- time=20;
- }
- K_startcountflag=1; //将抢答时间标志位置1
- K_timecountflag=0; //同时关闭答题时间标志位
- }
- }
- if(K_timecount==0) //答题时间调整
- {
- delay(10);
- if(K_timecount==0)
- {
- while(!K_timecount);
- datitime++;
- if(datitime==60)
- {
- datitime=30;
- }
- K_timecountflag=1;
- K_startcountflag=0;
- }
- }
- }
- void main(void)
- {
- T0_Init();
- while(1)
- {
- Key_Scan(); //开始键扫描
- if((flag==0)&(s_flag==1)) //当开始键没按下及没有人违规时才可进行时间调整
- {
- Time_Scan();
- }
- if((flag==1)&(s_flag==0))//当开始键按下及有人抢答才进行开始回答计时倒计时
- {
- if(K_Time==0)
- {
- delay(10);
- if(K_Time==0)
- {
- while(!K_Time);
- s=datitime;
- TR0=1;
- tt=0;
- TR1=1;
- }
- }
- }
- if((flag==0)&(s_flag==1)) //违规
- {
- Scan();
- if(num!=0) //开始键没有按下时,有人按下了抢答器,则置违规标志位
- {
- fall_flag=1;
- rled=0;
- }
- }
- if((flag==1)&(s_flag==1)) //如果开始键按下且抢答键没有人按下,则进行八路抢答键扫描
- {
- Scan();
- }
- display(); //显示到数码管上
- }
- }
- void timer0(void) interrupt 1
- {
- TH0=(65536-2000)/256; //2ms
- TL0=(65536-2000)%256;
- if(b_flag) //开始(START)键按下,嘟一声(长1秒),表示开始抢答
- {
- beep=~beep;
- }
- else
- beep=1;
- if(s<5) //抢答时间快到报警,隔1秒响一声且红灯闪烁,响三声
- {
- if(s%2==0)
- {
- b_flag=1;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
四路竞赛抢答器编程程序.zip
(168.37 KB, 下载次数: 295)
|