《基于51单片机的五路抢答器.doc》由会员分享,可在线阅读,更多相关《基于51单片机的五路抢答器.doc(11页珍藏版)》请在三一办公上搜索。
1、 实习(设计)报告姓 名 班 级 学 号 实习(设计)科目 基于51单片机的五路抢答器 实习(设计)地点 实习(设计)时间 电气工程及自动化学院School of Electrical Engineering & Automation一 实验安排1. 设计一个八路抢答器。2. 必须有主持人按钮,当主持人按下按钮,开始30秒倒计时,这30秒内即是八位选手抢答的时长,若30秒内无人抢答,则视为所有选手弃权。3. 倘若主持人按下开始的30秒内有人抢答,定时到10秒,同时在一屏幕上显示该抢答选手号码,当主持人按下开始答题键时,则倒计时从10秒开始,同时选手号码清零。若10秒内无法回答,则该选手视为题目
2、回答错误。/*五路抢答器c语言程序设计*/#include #define uchar unsigned char#define uint unsigned intsbit wela_hao=P30; /显示选手的数码管位选sbit wela1=P31;sbit wela2=P37;sbit qing=P35;sbit host=P36;sbit answer=P34;sbit key1=P10;sbit key2=P11;sbit key3=P12;sbit key4=P13;sbit key5=P14;uchar hao,shu,shi,ge,t0,start,flag;uchar cod
3、e table=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71;void init();/*初始函数申明*/void display(uchar shi,uchar ge,uchar hao);void delay(uint z);void keyscan();void main() init(); while(1) if(host=0)/主持人 delay(5); if(host=0) shu=30; hao=0; flag=1; start=1; delay(5); while(
4、!host); if(qing=0)/清零 delay(5); if(qing=0) while(1) shu=0; shi=shu/10; ge=shu%10; TR0=0; delay(5); hao=0; wela1=0; wela2=0; wela_hao=0; delay(1); display(shi,ge,hao); if(host=0)break; delay(5); while(!qing); if(answer=0) /开始答题 delay(5); if(answer=0) flag=1; shu=10; start=1; hao=0; delay(5); while(!a
5、nswer); if(flag=1) if(start=0) /选手按下,定时10秒 shu=10; shi=shu/10; ge=shu%10; wela1=0; wela2=0; wela_hao=0; delay(1);TR0=0; display(shi,ge,hao); delay(1); if(start=1) /主持人按下,倒计时30秒 wela1=1; wela2=1; wela_hao=1; delay(1); TR0=1; display(shi,ge,hao); delay(1);keyscan(); void init() /*初始化*/ t0=0; t1=0; shi
6、=shu/10; ge=shu%10; flag=0; shu=30; wela_hao=1; hao=0; TMOD=0x11; TH0=(65536-50000)/256; TL0=(65536-50000)%256; EA=1; ET0=1; TR0=0;void display(uchar shi,uchar ge,uchar hao) /*数码管动态扫描*/ wela1=1; wela2=0; P0=tablege; delay(5); wela2=1; wela1=0; P0=tableshi; delay(5); wela_hao=0; P2=tablehao; delay(5)
7、;void keyscan() /*按键扫描函数*/ if(key1=0) delay(5);if(key1=0) hao=1; P2=tablehao; start=0; delay(5); while(!key1); if(key2=0) delay(5);if(key2=0) hao=2; P2=tablehao; start=0; delay(5); while(!key2); if(key3=0) delay(5);if(key3=0) hao=3; P2=tablehao; start=0; delay(5); while(!key3); if(key4=0) delay(5);i
8、f(key4=0) hao=4; P2=tablehao; start=0; delay(5); while(!key4); if(key5=0) delay(5);if(key5=0) hao=5; P2=tablehao; start=0; delay(5); while(!key5); void delay(uint z) /*延时函数*/ uint x,y; for(x=z;x0;x-) for(y=110;y0;y-);void time0() interrupt 1 /*定时器0*/ TH0=(65536-50000)/256; TL0=(65536-50000)%256; shi=shu/10; ge=shu%10; t0+; if(t0=20) t0=0; shu-; shi=shu/10; ge=shu%10; if(shu=0) while(1) shu=0; hao=0; wela1=0; wela2=0; wela_hao=0; delay(1); display(shi,ge,hao); if(host=0)break;