51单片机做的带有计分功能的8路抢答器proteus仿真电路工程文件和c51源代码
单片机源程序如下:
- #include<reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit k0=P1^0;
- sbit k1=P1^1;
- sbit k2=P1^2;
- sbit k3=P1^3;
- sbit k4=P1^4;
- sbit k5=P1^5;
- sbit k6=P1^6;
- sbit k7=P1^7;
- sbit beep=P3^7; /*蜂鸣器*/
- sbit ks=P2^4; /*开始计时*/
- sbit jiayi=P2^5; /*分数加1*/
- sbit jianyi=P2^6; /*分数减1*/
- sbit fw=P2^7; /*复位*/
- int flg=1;
- bit flg1=0;
- int t=0,s=0;
- uchar qiangdanum=0x0b;
- uchar code table[]=
- {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00,0x00};
- uchar ge=0,shi=0;
- uint num,time=10, fs=0;
- int nums[]={50,50,50,50,50,50,50,50};
- void kyy(void) ;
- void displayfs();
- void display(); /*显示函数*/
- void kys(); /*设置按键函数*/
- void kys2(); /*抢答按键函数*/
- void delay(unsigned int z) /*延迟函数*/
- {
- unsigned int x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- void intial()
- {
- TMOD=0X01; /*采用方式一,即十六位计数器*/
- TH0=(65536-50000)/256; /*设置初始值,50ms中断一次*/
- TL0=(65536-50000)%256;
- EA=1;ET0=1; /*开启总中断源,定时器中断*/
- }
- void intia2()
- { TMOD=0X10; /*采用方式一,即十六位计数器*/
- TH1=(65536-50000)/256; /*设置初始值,50ms中断一次*/
- TL1=(65536-50000)%256;
- EA=1;ET1=1; /*开启总中断源,定时器中断*/
- }
- main()
- { intia2();
- intial(); /*初始化*/
- beep=0;
- while(1)
- { kys();
- if(flg1==1)
- displayfs();
- else
- display(); /*显示函数*/
-
- if(flg==0)
- {
- kys2();
- }
- }
- }
- void kys2() /*抢答按键函数*/
- {
- if(k0==0) /*开关0按下*/
- {
- delay(5);
- if(k0==0)
- { flg=1;
- qiangdanum=1; /* 1号选手抢答成功,数码管显示1*/
- beep=1;
- delay(1000);
- beep=0;
-
- time=20;
-
- }
- }
- if(k1==0) /*开关1按下*/
- {
- delay(5);
- if(k1==0)
- { flg=1;
- qiangdanum=2; /* 2号选手抢答成功,数码管显示2*/
- beep=1;
- delay(1000);
- beep=0;
- time=20;
-
- }
- }
- if(k2==0) /*开关2按下*/
- {
- delay(5);
- if(k2==0)
- { flg=1;
- qiangdanum=3; /* 3号选手抢答成功,数码管显示3*/
- beep=1;
- delay(1000);
- beep=0;
- time=20;
-
- }
- }
- if(k3==0) /*开关3按下*/
- {
- delay(5);
- if(k3==0)
- { flg=1;
- qiangdanum=4; /* 4号选手抢答成功,数码管显示4*/
- beep=1;
- delay(1000);
- beep=0;
- time=20;
-
- }
- }
- if(k4==0) /*开关4按下*/
- {
- delay(5);
- if(k4==0)
- { flg=1;
- qiangdanum=5; /* 5号选手抢答成功,数码管显示5*/
- beep=1;
- delay(1000);
- beep=0;
- time=20;
-
- }
- }
- if(k5==0) /*开关5按下*/
- {
- delay(5);
- if(k5==0)
- { flg=1;
- qiangdanum=6; /* 6号选手抢答成功,数码管显示6*/
- beep=1;
- delay(1000);
- beep=0;
- time=20;
-
- }
- }
- if(k6==0) /*开关6按下*/
- {
- delay(5);
- if(k6==0)
- { flg=1;
- qiangdanum=7; /* 7号选手抢答成功,数码管显示7*/
- beep=1;
- delay(1000);
- beep=0;
- time=20;
-
- }
- }
- if(k7==0) /*开关7按下*/
- {
- delay(5);
- if(k7==0)
- { flg=1;
- qiangdanum=8; /* 8号选手抢答成功,数码管显示8*/
- beep=1;
- delay(1000);
- beep=0;
- time=20;
-
- }
- }
- }
- void kys() /*设置按键函数*/
- {
- uchar i;
- if(qiangdanum==0x0a) //复位按键按下后开始按键才有效
- {
- if(ks==0)
- {
- delay(5);
- if(ks==0)
- {
- TR0=1;
- flg=0;
- }
- }
- }
- if((qiangdanum!=0x0a)&&(qiangdanum!=0x0b)&&(time!=0)) //在规定时间内或规定时间内答题结束后,分数加减按键才有效
- {
- if(jiayi==0)
- {
- delay(5);
- if(jiayi==0)
- TR0=0;
- time=nums[qiangdanum-1];
- for (i=0;i<5;i++)
- {display();
- delay(250);
- delay(250);
- }
-
- fs=nums[qiangdanum-1];
- fs++;
- nums[qiangdanum-1]=fs;
- time=nums[qiangdanum-1];
- // qiangdanum=0x0b;
- while(!jiayi);
-
- }
- if(jianyi==0)
- {
- delay(5);
- if(jianyi==0)
- TR0=0;
- time=nums[qiangdanum-1];
- for (i=0;i<5;i++)
- {display();
- delay(250);
- delay(250);
- };
- fs=nums[qiangdanum-1];
- fs--;
- nums[qiangdanum-1]=fs;
- time=nums[qiangdanum-1];
- // qiangdanum=0x0b;
- while(!jianyi);
- }
- }
- if(fw==0) //复位按键
- {
- delay(5);
- if(fw==0)
- { flg=1;
- TR0=0;
- time=10;
- flg1=1;
- s=0;
- TR1=1;
-
- }
- }
- }
- void time0() interrupt 1 /*1秒定时函数,采用方式1重装*/
- {
- num++;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- if(num==20)
- {
- num=0;
- time--;
- if(time==0) //时间到,抢答无效
- {
- TR0=0;
- flg=1;
- }
- }
-
- }
-
- void time1() interrupt 3
- {
- t++;
- TH1=(65536-50000)/256;
- TL1=(65536-50000)%256;
- if(t==200)
- { qiangdanum=0x0b;
- t=0;
- s++;
- if(s==8) //时间到,抢答无效
- { s=0;
- TR1=0;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
带计分功能的8路抢答器仿真电路和源代码分享.zip
(218.9 KB, 下载次数: 99)
|