1) 抢答器同时供8名选手或8个代表队比赛,分别用8个按钮s1-s8表示; 2) 设置一个系统清除和抢答控制开关Key1和Key2。 3) 抢答器具有锁存与显示功能。即选手按下按钮,锁存相应编号,显示该选手编号至数码管,并且该编号保存至主持人按下清除键Key1清除为止。 4) 抢答器具有定时抢答功能,一次抢答设定的时间为30秒。主持人按下抢答控制开关Key2,抢答才能开始,启动定时器,并将倒数计时时间用数码管显示。若定时时间到,无人抢答,则该轮抢答结束,系统通过蜂鸣器报警,选手禁止抢答。 5) 选手在设定的时间内进行抢答,抢答有效,定时器停止工作,蜂鸣器报警,数码管显示选手编号及倒计时剩余时间,并保持到主持人将系统清除为止。 仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <reg52.h>
- //共阴极数码管编码表 0 - f显示
- unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,
- 0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71};
- //变量定义
- sbit start_stop = P3^6;
- sbit reset = P3^7;
- sbit LE =P3^0;
- sbit key1 = P1^0;
- sbit key2 = P1^1;
- sbit key3 = P1^2;
- sbit key4 = P1^3;
- sbit key5 = P1^4;
- sbit key6 = P1^5;
- sbit key7 = P1^6;
- sbit key8 = P1^7;
- sbit state = P3^4;
- sbit start=P3^1;
- bit start_stop_flag = 0;
- bit key1_flag = 0;
- bit key2_flag = 0;
- bit key3_flag = 0;
- bit key4_flag = 0;
- bit key5_flag = 0;
- bit key6_flag = 0;
- bit key7_flag = 0;
- bit key8_flag = 0;
- bit reset_flag = 0;
- bit action = 0;
- bit start_flag=0;
- unsigned char second = 15;
- unsigned char timer0_count = 0;
- unsigned char number = 0;
- unsigned char number_display = 0;
- //延时函数
- void delay(unsigned int z)
- {
- unsigned int x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void Play(unsigned char t)
- {
- unsigned char i;
- for(i=0;i<100;i++)
- {
- state=~state;
- delay(t);
- }
- state=0;
- }
- //数码管显示驱动函数
- void display(unsigned char number,unsigned char second)
- {
- unsigned char second_first,second_second;
- second_first = second / 10;
- second_second = second % 10;
-
- P2 = 0xfe;
- P0 = table[number];
- delay(1);
-
- P2 = 0xfb;
- P0 = table[second_first];
- delay(1);
-
- P2 = 0xf7;
- P0 = table[second_second];
- delay(1);
- }
- //开始键扫描函数
- void start_stop_keyscan()
- {
- if(start_stop == 0)
- {
- delay(8);
- if((start_stop == 0)&&(!start_stop_flag))
- {
- start_stop_flag = 1;
- action = 1;
- TR0 = 1;
- //state =0;
- Play(1);
- }
- }
- else
- {
- start_stop_flag = 0;
- }
- }
- //八位抢答键扫描函数
- unsigned char key_scan8()
- {
- if(key1 == 0)
- {
- delay(8);
- if((key1 == 0)&&(!key1_flag))
- {
- key1_flag = 1;
- number = 1;
- number_display = number;
- Play(2);
- LE=0;
- }
- }
- else
- {
- key1_flag = 0;
- number = 0;
- }
-
- if(key2 == 0)
- {
- delay(8);
- if((key2 == 0)&&(!key2_flag))
- {
- key2_flag = 1;
- number = 2;
- number_display = number;
- Play(2);
- LE=0;
- }
- }
- else
- {
- key2_flag = 0;
- number = 0;
- }
-
- if(key3 == 0)
- {
- delay(8);
- if((key3 == 0)&&(!key3_flag))
- {
- key3_flag = 1;
- number = 3;
- number_display = number;
- Play(2);
- LE=0;
- }
- }
- else
- {
- key3_flag = 0;
- number = 0;
- }
-
- if(key4 == 0)
- {
- delay(8);
- if((key4 == 0)&&(!key4_flag))
- {
- key4_flag = 1;
- number = 4;
- number_display = number;
- Play(2);
- LE=0;
- }
- }
- else
- {
- key4_flag = 0;
- number = 0;
- }
-
- if(key5 == 0)
- {
- delay(8);
- if((key5 == 0)&&(!key5_flag))
- {
- key5_flag = 1;
- number = 5;
- number_display = number;
- Play(2);
- LE=0;
- }
- }
- else
- {
- key5_flag = 0;
- number = 0;
- }
-
- if(key6 == 0)
- {
- delay(8);
- if((key6 == 0)&&(!key6_flag))
- {
- key6_flag = 1;
- number = 6;
- number_display = number;
- Play(2);
- LE=0;
- }
- }
- else
- {
- key6_flag = 0;
- number = 0;
- }
-
- if(key7 == 0)
- {
- delay(8);
- if((key7 == 0)&&(!key7_flag))
- {
- key7_flag = 1;
- number = 7;
- number_display = number;
- Play(2);
- LE=0;
- }
- }
- else
- {
- key7_flag = 0;
- number = 0;
- }
-
- if(key8 == 0)
- {
- delay(8);
- if((key8 == 0)&&(!key8_flag))
- {
- key8_flag = 1;
- number = 8;
- number_display = number;
- Play(2);
- LE=0;
- }
- }
- else
- {
- key8_flag = 0;
- number = 0;
- }
-
- if(number_display != 0)
- {
- return 1;
- }
- else
- {
- return 0;
- }
-
- }
- //复位键扫描函数
- void reset_keyscan()
- {
- if(reset == 0)
- {
- delay(8);
- if((reset == 0)&&(!reset_flag))
- { second=15;
- reset_flag = 1;
- number_display = 0;
- LE = 1;
- //state = 1;
- }
- }
- else
- {
- reset_flag = 0;
- }
- }
-
- //主函数
- void main()
- {
- TMOD=0x01;
- TH0=0x3C;
- TL0=0xB0;
- EA=1;
- ET0=1;
- TR0=0;
-
- while(1)
- { while((start == 0)&&(!start_flag))
- { start_stop_keyscan();
- reset_keyscan();
- while(action)
- {
- while(!key_scan8())
- {
- display(number_display,second);
- if(second == 0)
- {
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
抢答.rar
(67.64 KB, 下载次数: 67)
|