基于STC89c51单片机的四路抢答器
编程软件 Keil uVision5 题目: 四路抢答器 其中主持人操控S5与S6两个按键。选手共四位,分别操控S1–S4中的一个按键。 当主持人按下抢答开始按键S5后,倒计时开始,计时10s。此后最先按下按键的选手号码将显示与数码管上,并且蜂鸣器发出响声,后来按下的将无显示。若十秒计时结束后,再按下按键也不会显示。若主持人没有按开始键,就有选手抢答,则视为犯规。此时犯规的选手号码将被显示于数码管上(最多显示三位犯规选手,不显示时间),同时,蜂鸣器一直发出长笛声报警 而当主持人按下清零键S6后,一切状态均恢复,可以开始新一轮的抢答。 按键和数码管功能简介 S5 抢答开始 S6 清零 S1–S4 分别为1到4号选手按键 数码管:两段显示时间 一段显现选手号码
#include "reg51.h"
typedef unsigned int u16;
typedef unsigned char u8;
#define GPIO_DIG P0
#define GPI0_KEY P1
#define T 150
sbit Beep = P1 ^ 5;
sbit LSA = P2 ^ 2;
sbit LSB = P2 ^ 3;
sbit LSC = P2 ^ 4;
u8 Keyvalue = 100, k = 0, flag = 0, fg[4] = { 0 };
u8 q, w, e, r;
u16 x = 0, c = 1;
u8 code smgduan[] = { 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,
0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71 };
void InitTimer0(void)
{
TMOD = 0x10;
TH0 = 0x0FF;
TL0 = 0xce;
EA = 1;
ET0 = 0;
TR0 = 0;
}
void InitTimer1(void)
{
TMOD = 0x01;
TH1 = 0xFC;
TL1 = 0x66;
EA = 1;
ET1 = 1;
TR1 = 1;
}
void delay(u16 n)
{
while (n--);
}
void delay_ms(u16 x)
{
u8 l, m;
for (l = 0;l <= x;l++)
for (m = 0;m < 123;m++);
}
void keydown()
{
char a = 0;
GPI0_KEY = 0X0F;
if (GPI0_KEY != 0x0F)
{
delay(100);
if (GPI0_KEY != 0x0F)
{
GPI0_KEY = 0X0F;
switch (GPI0_KEY)
{
case 0x07: Keyvalue = 0; break;
case 0x0b: Keyvalue = 1; break;
case 0x0d: Keyvalue = 2; break;
case 0x0e: Keyvalue = 3; break;
}
GPI0_KEY = 0XF0;
switch (GPI0_KEY)
{
case 0x70: Keyvalue = Keyvalue; break;
case 0xb0: Keyvalue = Keyvalue + 4; break;
case 0xd0: Keyvalue = Keyvalue + 8; break;
case 0xe0: Keyvalue = Keyvalue + 12; break;
}
while ((a < 50) && (GPI0_KEY != 0XF0))
{
delay(100);
a++;
}
}
}
}
void Display(u16 num)
{
u16 bits, ten;
bits = num % 10;
ten = num / 10;
LSA = 1;LSB = 0;LSC = 0;
P0 = smgduan[ten];
delay(T);
LSA = 0;LSB = 0;LSC = 0;
P0 = smgduan[bits];
delay(T);
}
void caipan()
{
if (Keyvalue == 4)
{
TR0 = 0;
ET0 = 0;
TR1 = 1;
c = 1;
x = 10;
k = 0;
q = 0;
w = 0;
e = 0;
r = 0;
Keyvalue = 100;
}
if (Keyvalue == 5)
{
TR1 = 0;
TR0 = 0;
ET0 = 0;
c = 0;
x = 0;
fg[0] = 0;
fg[1] = 0;
fg[2] = 0;
fg[3] = 0;
flag = 0;
}
}
void fangui()
{
if (flag < 3)
{
switch (Keyvalue)
{
case 0:ET0 = 1;TR0 = 1;if (q == 0) { flag++;q++; }delay(9000);Keyvalue = 50;fg[0] = 1;break;
case 1:ET0 = 1;TR0 = 1;if (w == 0) { flag++;w++; }delay(9000);Keyvalue = 50;fg[1] = 2;break;
case 2:ET0 = 1;TR0 = 1;if (e == 0) { flag++;e++; }delay(9000);Keyvalue = 50;fg[2] = 3;break;
case 3:ET0 = 1;TR0 = 1;if (r == 0) { flag++;r++; }delay(9000);Keyvalue = 50;fg[3] = 4;break;
}
}
}
void fanguirs()
{
switch (flag)
{
case 0:LSA = 1;LSB = 1;LSC = 0; P0 = smgduan[0];delay(300); break;
case 1:LSA = 1;LSB = 1;LSC = 0; P0 = smgduan[1];delay(300); break;
case 2:LSA = 1;LSB = 1;LSC = 0; P0 = smgduan[2];delay(300); break;
case 3:LSA = 1;LSB = 1;LSC = 0; P0 = smgduan[3];delay(300); break;
}
}
void fanguir()
{
LSA = 1;LSB = 1;LSC = 1;P0 = smgduan[fg[0]];delay(T);
LSA = 0;LSB = 1;LSC = 1;P0 = smgduan[fg[1]];delay(T);
LSA = 1;LSB = 0;LSC = 1;P0 = smgduan[fg[2]];delay(T);
LSA = 0;LSB = 0;LSC = 1;P0 = smgduan[fg[3]];delay(T);
}
void xuanshou()
{
if (Keyvalue == 0 && x > 0 && k == 0)
{
if (k == 0)
{
TR0 = 1;
ET0 = 1;
delay(5000);
TR0 = 0;
ET0 = 0;
k = 1;
}
}
else if (Keyvalue == 1 && x > 0 && k == 0)
{
if (k == 0)
{
TR0 = 1;
ET0 = 1;
delay(5000);
TR0 = 0;
ET0 = 0;
k = 2;
}
}
else if (Keyvalue == 2 && x > 0 && k == 0)
{
if (k == 0)
{
TR0 = 1;
ET0 = 1;
delay(5000);
TR0 = 0;
ET0 = 0;
k = 3;
}
}
else if (Keyvalue == 3 && x > 0 && k == 0)
{
if (k == 0)
{
TR0 = 1;
ET0 = 1;
delay(5000);
TR0 = 0;
ET0 = 0;
k = 4;
}
}
else if (k == 1)
{
TR1 = 0;
LSA = 1;LSB = 1;LSC = 1;
P0 = smgduan[1];
delay(T);
}
else if (k == 2)
{
TR1 = 0;
LSA = 0;LSB = 1;LSC = 1;
P0 = smgduan[2];
delay(T);
}
else if (k == 3)
{
TR1 = 0;
LSA = 1;LSB = 0;LSC = 1;
P0 = smgduan[3];
delay(T);
}
else if (k == 4)
{
TR1 = 0;
LSA = 0;LSB = 0;LSC = 1;
P0 = smgduan[4];
delay(T);
}
}
void main()
{
InitTimer0();
InitTimer1();
while (1)
{
keydown();
caipan();
Display(x);
if (c == 0)
{
fangui();
fanguirs();
fanguir();
}
if (c == 1)
{
xuanshou();
}
}
}
void Timer0Interrupt(void) interrupt 1
{
u8 count;
TH0 = 0xFF;
TL0 = 0xCE;
count++;
if (count == 10)
{
Beep = ~Beep;
count = 0;
}
}
void Timer1Interrupt(void) interrupt 3
{
u8 i;
TH0 = 0xFC;
TL0 = 0x66;
i++;
if (i == 100 && x > 0)
{
x = x - 1;
i = 0;
}
}
|