|
#include <reg52.h>
#include <intrins.h>
//选手按键
#define GPIO_KEY P1
typedef unsigned int uint; //对数据类型进行声明定义
typedef unsigned char uchar;
sbit beep=P0^7;// 蜂鸣器
sbit start=P0^1;//开始按键
sbit led=P0^0;//开始旁的红灯
sbit res=P0^2;//复位按键
bit reset_flag=0;
char s;//时间
uchar num=0; //选手变量
char time1;// 抢答时间
char time2;// 答题限时时间
uint m,n;//T0,T1 定时器定一秒时
bit flag1,flag2,flag3,flag4; // 标志位flag3开始 flag2 选手标志
//数码管位选
sbit LSB=P3^1;
sbit LSD=P3^0;
//函数声明
uchar KeyValue;
uchar number_display=0;
void kaishi();
void display() ;
void KeyDown();
void Beep();
//延时函数
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=120;y>0;y--);
}
/*共阴数码管段码值*/uchar code table1[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x3f
};
//初始化函数
void init()
{
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
ET0=1;
EA=1;
TR0=0;
P2=0x00;
time1=10;
time2=10;
num=0;
flag1=1;
flag2=1;
flag3=0;
flag4=1;
led=1;
beep=1;
s=100;
}
void Beep() //蜂鸣器
{
beep = 0;
delay(500);//延时一秒
beep = 1;
}
void KeyDown() //按键扫描
{
GPIO_KEY =0xff;
start = 1;
if((GPIO_KEY)!=0xff || (start)!= 1) //检测到有按键按下
{
delay(10); //消抖
if(GPIO_KEY==0xfe)
KeyValue=1;
if(GPIO_KEY==0xfd)
KeyValue=2;
if(GPIO_KEY==0xfb)
KeyValue=3;
if(GPIO_KEY==0xf7)
KeyValue=4;
if(GPIO_KEY==0xef)
KeyValue=5;
if(GPIO_KEY==0xdf)
KeyValue=6;
if(GPIO_KEY==0xbf)
KeyValue=7;
if(GPIO_KEY==0x7f)
KeyValue=8;
if(start==0)
KeyValue=9;
if(res==0)
KeyValue=10;
}
}
void t0() interrupt 1
{
TH0=0x3C;
TL0=0xB0;
if(flag3) // 开始 (START) 键按下 ,,表示开始抢答
{
delay(10);
m++;
if(m==20)
{ m=0;
s--;
if(s==0)
{
while(1)
{
s=100;
display();
TR0=0;
KeyDown();
if(KeyValue == 9)
{
KeyValue=0;
init();
flag4=1;
delay(300);
break;
}
}
}
}
}
}
void kaishi()
{
if(KeyValue==9)
{
led=1;
TR0=1;
s=time1;
flag3=1;
flag2=1;
flag4=0;
num=0;
KeyValue=0;
}
}
void qc1()
{ if(KeyValue==10)
{
led=1;
LSB=0;LSD=0;
s=0;num=0;
KeyValue=0;
}
}
void xuanshou()
{
if(KeyValue==1)
{
Beep();
num=1; //数码管显示 1 号 "1"
TR0=1;
s=time2;
flag2=0;
led=0;
KeyValue=11;
flag1=0;
}
if(KeyValue==2)
{
Beep();
num=2; //数码管显示 2 号 "2"
TR0=1;
s=time2;
flag2=0;
led=0;
KeyValue=11;
flag1=0;
}
if(KeyValue==3)
{
Beep();
num=3; //数码管显示 3 号 "3"
TR0=1; //关闭定时器 0,时间停止
s=time2;
flag2=0;
led=0;
KeyValue=11;
flag1=0;
}
if(KeyValue==4)
{
Beep();
num=4; //数码管显示4 号 "4"
TR0=1;
s=time2;
flag2=0;
led=0;
KeyValue=11;
flag1=0;
}
if(KeyValue==5)
{
Beep();
num=5; //数码管显示 5号 "5"
TR0=1;
s=time2;
flag2=0;
led=0;
KeyValue=11;
flag1=0;
}
if(KeyValue==6)
{
Beep();
num=6; //数码管显示 6 号 "6"
TR0=1;
s=time2;
flag2=0;
led=0;
KeyValue=11;
}
if(KeyValue==7)
{
Beep();
num=7; //数码管显示7 号 "7"
TR0=1;
s=time2;
flag2=0; // 关闭开始键标志位 ,使再按其他七个键不会响?
led=0;
KeyValue=11;
flag1=0;
}
if(KeyValue==8)
{
Beep();
num=8; //数码管显示 8 号 "8"
TR0=1;
s=time2;
flag2=0; // 关闭开始键标志位 ,使再按其他七个键不会响?
led=0;
KeyValue=11;
flag1=0;
}
}
void display()
{
LSB=0;LSD=0;
P2 = ~table1[s/10];
delay(5);
LSB=1;LSD=0;
P2 = ~table1[s%10];
delay(5);
LSB=0;LSD=0;
P2 = ~table1[0];
delay(5);
LSB=0;LSD=1;
P2 = ~table1[num];
delay(5);
}
void main()
{
init();
while(1)
{
KeyDown();
if((flag1==1)&&(flag4==1))
kaishi();
if((flag3==1)&&(flag2==1))
xuanshou();
display();
}
}
|
评分
-
查看全部评分
|