写一个51单片机按下独立按键S2流水灯从两边往中间流动两次,按下独立按键S3,蜂鸣器喃响三声按下独立按键S4,后两位数码管每隔1s加1,到20清零,按下独立按键S5,前三位数码管显示OFF的程序
时间: 2024-05-08 11:18:10 浏览: 67
好的,这是一个关于51单片机的编程问题。以下是代码实现:
```c
#include <reg51.h>
#define uint unsigned int
sbit LED1 = P1^0;
sbit LED2 = P1^1;
sbit LED3 = P1^2;
sbit LED4 = P1^3;
sbit LED5 = P1^4;
sbit S2 = P3^2;
sbit S3 = P3^3;
sbit S4 = P3^4;
sbit S5 = P3^5;
sbit BEEP = P2^3;
uint i, j;
uint num = 0;
void delay(uint xms)
{
uint i, j;
for (i = xms; i > 0; i--)
for (j = 110; j > 0; j--);
}
void main()
{
while (1)
{
if (S2 == 0)
{
for (i = 0; i < 2; i++)
{
LED1 = 1;
delay(300);
LED2 = 1;
delay(300);
LED3 = 1;
delay(300);
LED4 = 1;
delay(300);
LED5 = 1;
delay(300);
LED1 = 0;
delay(300);
LED2 = 0;
delay(300);
LED3 = 0;
delay(300);
LED4 = 0;
delay(300);
LED5 = 0;
delay(300);
}
}
if (S3 == 0)
{
BEEP = 1;
delay(200);
BEEP = 0;
delay(200);
BEEP = 1;
delay(200);
BEEP = 0;
delay(200);
BEEP = 1;
delay(200);
BEEP = 0;
delay(200);
}
if (S4 == 0)
{
while (1)
{
num++;
if (num > 20)
num = 0;
P0 = num;
delay(1000);
}
}
if (S5 == 0)
{
P0 = 0;
LED1 = 0;
LED2 = 0;
LED3 = 0;
delay(500);
P0 = 0xFF;
LED1 = 1;
LED2 = 1;
LED3 = 1;
delay(500);
}
}
}
```
当按下 S2 时,流水灯从两边往中间流动两次;当按下 S3 时,蜂鸣器喃响三声;当按下 S4 时,后两位数码管每隔1s加1,到20清零;当按下 S5 时,前三位数码管显示 OFF 的程序。
希望这个程序可以满足您的需求。
阅读全文