|
#include <reg52.h>
#include <intrins.h>
//選手按鍵
#define GPIO_KEY P1
typedef unsigned int uint; //對數(shù)據(jù)類型進(jìn)行聲明定義
typedef unsigned char uchar;
sbit beep=P0^7;// 蜂鳴器
sbit start=P0^1;//開始按鍵
sbit led=P0^0;//開始旁的紅燈
sbit res=P0^2;//復(fù)位按鍵
bit reset_flag=0;
char s;//時間
uchar num=0; //選手變量
char time1;// 搶答時間
char time2;// 答題限時時間
uint m,n;//T0,T1 定時器定一秒時
bit flag1,flag2,flag3,flag4; // 標(biāo)志位flag3開始 flag2 選手標(biāo)志
//數(shù)碼管位選
sbit LSB=P3^1;
sbit LSD=P3^0;
//函數(shù)聲明
uchar KeyValue;
uchar number_display=0;
void kaishi();
void display() ;
void KeyDown();
void Beep();
//延時函數(shù)
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=120;y>0;y--);
}
/*共陰數(shù)碼管段碼值*/uchar code table1[]={
0x3f,0x06,0x5b,0x4f,
0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x3f
};
//初始化函數(shù)
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; //數(shù)碼管顯示 1 號 "1"
TR0=1;
s=time2;
flag2=0;
led=0;
KeyValue=11;
flag1=0;
}
if(KeyValue==2)
{
Beep();
num=2; //數(shù)碼管顯示 2 號 "2"
TR0=1;
s=time2;
flag2=0;
led=0;
KeyValue=11;
flag1=0;
}
if(KeyValue==3)
{
Beep();
num=3; //數(shù)碼管顯示 3 號 "3"
TR0=1; //關(guān)閉定時器 0,時間停止
s=time2;
flag2=0;
led=0;
KeyValue=11;
flag1=0;
}
if(KeyValue==4)
{
Beep();
num=4; //數(shù)碼管顯示4 號 "4"
TR0=1;
s=time2;
flag2=0;
led=0;
KeyValue=11;
flag1=0;
}
if(KeyValue==5)
{
Beep();
num=5; //數(shù)碼管顯示 5號 "5"
TR0=1;
s=time2;
flag2=0;
led=0;
KeyValue=11;
flag1=0;
}
if(KeyValue==6)
{
Beep();
num=6; //數(shù)碼管顯示 6 號 "6"
TR0=1;
s=time2;
flag2=0;
led=0;
KeyValue=11;
}
if(KeyValue==7)
{
Beep();
num=7; //數(shù)碼管顯示7 號 "7"
TR0=1;
s=time2;
flag2=0; // 關(guān)閉開始鍵標(biāo)志位 ,使再按其他七個鍵不會響?
led=0;
KeyValue=11;
flag1=0;
}
if(KeyValue==8)
{
Beep();
num=8; //數(shù)碼管顯示 8 號 "8"
TR0=1;
s=time2;
flag2=0; // 關(guān)閉開始鍵標(biāo)志位 ,使再按其他七個鍵不會響?
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();
}
}
|
-
-
-
單片機(jī)(1).zip
2019-12-5 09:57 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
97.6 KB, 下載次數(shù): 13, 下載積分: 黑幣 -5
評分
-
查看全部評分
|