#include "public.h"
#include "display.h"
#include "led.h"
#include "button.h"
#include "speaker.h"
u8 count=0; //20是1s
u8 flag_player=0; //0:无选手按下
u8 flag_sta=0; //1:按下开始键
u8 time_use=0; //用了的时间
void int_init() //定时器0初始化
{
EA=1;
ET0=1;
TMOD=0X01;
TH0=15536/256;
TL0=15536%256;
}
void deal_key() //按键处理函数
{
u8 key=0;
key=key_scan(0);
switch(key)
{
case KEYSTA_PRESS: //按下开始键
{
flag_sta=!flag_sta;
break;
}
case KEY1_PRESS:
{
if(flag_player==0)
flag_player=1;
break;
}
case KEY2_PRESS:
{
if(flag_player==0)
flag_player=2;
break;
}
case KEY3_PRESS:
{
if(flag_player==0)
flag_player=3;
break;
}
}
}
void timer0() interrupt 1 //定时器0中断处理函数
{
TH0=15536/256;
TL0=15536%256;
count++;
if(count==20) //满1s
{
count=0;
time_use++;
if(time_use==10) //时间到
{
TR0=0;
flag_player=0;
time_use=0;
led1=1;led2=1;led3=1;
}
}
}
void main()
{
int_init();
while(1)
{
delay_10us(2000);
deal_key();
if(flag_sta==0&&flag_player!=0) //有人抢答
{
alarm(2000);
}
else if (flag_sta==1&&flag_player!=0) //正常答题
{
TR0=1;
display(10-time_use); //显示剩余时间
switch(flag_player)
{
case 1:
{
led1=0;
break;
}
case 2:
{
led2=0;
break;
}
case 3:
{
led1=0;
break;
}
default:
{
}
}
}
else if(flag_sta)
{
display(time_use);
}
}
}