用51生成一个八人抢答器,按下按钮1,数码管显示4,四人抢答器。按下按钮3,抢答开始,按下四个按钮中的一个,其他按钮没有效果,此时数码管显示第几个人抢答成功。按下按钮2数码管显示8,八人抢答器作用,八个抢答按钮其中一个按下,其他按下没有效果,数码管显示第几个人抢答成功,用四位数码管,几人抢答器在数码管的第一位显示,第几个人抢答成功在第四位显示
时间: 2024-03-21 09:43:34 浏览: 30
以下是基于51单片机实现的八人抢答器代码:
```C
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit Key1 = P1^0;
sbit Key2 = P1^1;
sbit Key3 = P1^2;
sbit Key4 = P1^3;
sbit Key5 = P1^4;
sbit Key6 = P1^5;
sbit Key7 = P1^6;
sbit Key8 = P1^7;
uchar cnt = 0; // 抢答计数器
uchar mode = 0; // 抢答器模式,0表示未开始,4表示4人抢答器,8表示8人抢答器
// 数码管段码表
uchar code segTable[] = {
0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07,
0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71
};
// 数码管位码表
uchar code posTable[] = {
0xfe, 0xfd, 0xfb, 0xf7
};
// 数码管显示函数
void display(uchar num) {
uchar i;
for (i = 0; i < 4; i++) {
P2 = segTable[num % 10];
P0 = posTable[i];
num /= 10;
delay(5);
}
}
// 延时函数
void delay(uint x) {
uint i, j;
for (i = x; i > 0; i--) {
for (j = 110; j > 0; j--);
}
}
// 抢答器计数函数
void count() {
cnt++;
if (cnt > mode) {
cnt = 0;
}
}
// 获取按键状态函数
uchar getKey() {
if (Key1 == 0) {
delay(5);
if (Key1 == 0) {
while (Key1 == 0);
return 1;
}
}
if (Key2 == 0) {
delay(5);
if (Key2 == 0) {
while (Key2 == 0);
return 2;
}
}
if (Key3 == 0) {
delay(5);
if (Key3 == 0) {
while (Key3 == 0);
return 3;
}
}
if (Key4 == 0) {
delay(5);
if (Key4 == 0) {
while (Key4 == 0);
return 4;
}
}
if (mode == 8) {
if (Key5 == 0) {
delay(5);
if (Key5 == 0) {
while (Key5 == 0);
return 5;
}
}
if (Key6 == 0) {
delay(5);
if (Key6 == 0) {
while (Key6 == 0);
return 6;
}
}
if (Key7 == 0) {
delay(5);
if (Key7 == 0) {
while (Key7 == 0);
return 7;
}
}
if (Key8 == 0) {
delay(5);
if (Key8 == 0) {
while (Key8 == 0);
return 8;
}
}
}
return 0;
}
// 初始化函数
void init() {
TMOD = 0x01; // 定时器0工作在模式1
TH0 = 0xfc; // 定时器0初值,100ms中断一次
TL0 = 0x66;
TR0 = 1; // 启动定时器0
ET0 = 1; // 允许定时器0中断
EA = 1; // 允许总中断
}
// 定时器0中断处理函数
void timer0() interrupt 1 {
TH0 = 0xfc;
TL0 = 0x66;
count();
}
void main() {
init();
while (1) {
if (getKey() == 1) {
mode = 4;
display(4);
}
if (getKey() == 2) {
mode = 0;
cnt = 0;
display(0);
}
if (getKey() == 3) {
if (mode == 4) {
cnt = 0;
}
mode = 4;
display(0);
while (cnt < mode);
display(cnt);
delay(1000);
cnt = 0;
mode = 0;
display(0);
}
if (getKey() == 5) {
mode = 8;
display(8);
}
if (cnt != 0 && getKey() == cnt) {
display(cnt);
delay(1000);
cnt = 0;
mode = 0;
display(0);
}
}
}
```
其中,getKey()函数用于获取按键状态,count()函数用于抢答计数,timer0()函数是定时器0中断处理函数,display()函数用于数码管显示,init()函数用于初始化硬件。在主函数中,根据按键状态来控制抢答器的模式和计数器,最终显示抢答成功的编号。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)