要求制作一个4人抢答器的keil4的AT89C52C语言代码。无人抢答时,4个灯跑马循环,谁先按下,相对应的灯亮起,LED数码管显示抢答人编号,同时蜂鸣器发声。
时间: 2024-06-13 15:08:42 浏览: 86
以下是一个4人抢答器的keil4的AT89C52C语言代码,满足无人抢答时,4个灯跑马循环,谁先按下,相对应的灯亮起,LED数码管显示抢答人编号,同时蜂鸣器发声的要求:
```c
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit beep = P1^5; // 蜂鸣器
sbit dula = P2^6; // 数码管段选
sbit wela = P2^7; // 数码管位选
sbit key1 = P3^0; // 抢答器1
sbit key2 = P3^1; // 抢答器2
sbit key3 = P3^2; // 抢答器3
sbit key4 = P3^3; // 抢答器4
sbit led1 = P0^0; // LED1
sbit led2 = P0^1; // LED2
sbit led3 = P0^2; // LED3
sbit led4 = P0^3; // LED4
uchar code table[] = { // 数码管显示0~9
0x3f, 0x06, 0x5b, 0x4f, 0x66,
0x6d, 0x7d, 0x07, 0x7f, 0x6f
};
void delay(uint i) { // 延时函数
while(i--);
}
void display(uchar num) { // 数码管显示函数
dula = 1;
P0 = table[num];
dula = 0;
}
void main() {
uchar i, j, k, l, flag;
while(1) {
flag = 0; // 标志位清零
led1 = led2 = led3 = led4 = 0; // 灯全灭
for(i=0; i<4; i++) { // 跑马灯
display(i);
wela = 1;
P0 = 0xfe << i;
wela = 0;
delay(50000);
}
if(key1 == 0) { // 抢答器1
flag = 1;
led1 = 1;
display(1);
beep = 0;
delay(50000);
beep = 1;
}
if(key2 == 0) { // 抢答器2
flag = 1;
led2 = 1;
display(2);
beep = 0;
delay(50000);
beep = 1;
}
if(key3 == 0) { // 抢答器3
flag = 1;
led3 = 1;
display(3);
beep = 0;
delay(50000);
beep = 1;
}
if(key4 == 0) { // 抢答器4
flag = 1;
led4 = 1;
display(4);
beep = 0;
delay(50000);
beep = 1;
}
if(flag == 1) { // 有人抢答
delay(50000);
led1 = led2 = led3 = led4 = 0; // 灯全灭
display(0); // 数码管显示0
delay(50000);
}
}
}
```
阅读全文