编写一个流水灯程序
时间: 2023-08-08 22:14:04 浏览: 66
流水灯程序是一种经典的嵌入式系统入门程序,下面是一个简单的流水灯程序,使用C语言编写:
```c
#include <reg52.h> // 引入51单片机头文件
sbit LED1 = P2^0; // 定义LED1的IO口
sbit LED2 = P2^1; // 定义LED2的IO口
sbit LED3 = P2^2; // 定义LED3的IO口
sbit LED4 = P2^3; // 定义LED4的IO口
void main()
{
while(1) // 无限循环
{
LED1 = 0; // 点亮LED1
LED2 = 1; // 熄灭LED2
LED3 = 1; // 熄灭LED3
LED4 = 1; // 熄灭LED4
delay(500); // 延时500ms
LED1 = 1; // 熄灭LED1
LED2 = 0; // 点亮LED2
LED3 = 1; // 熄灭LED3
LED4 = 1; // 熄灭LED4
delay(500); // 延时500ms
LED1 = 1; // 熄灭LED1
LED2 = 1; // 熄灭LED2
LED3 = 0; // 点亮LED3
LED4 = 1; // 熄灭LED4
delay(500); // 延时500ms
LED1 = 1; // 熄灭LED1
LED2 = 1; // 熄灭LED2
LED3 = 1; // 熄灭LED3
LED4 = 0; // 点亮LED4
delay(500); // 延时500ms
}
}
void delay(unsigned int t) // 定义延时函数
{
unsigned int i, j;
for(i=0; i<t; i++)
{
for(j=0; j<10; j++);
}
}
```
这个程序使用了P2口的四个IO口控制四个LED灯,每次点亮一个LED灯,持续500ms后熄灭,然后点亮下一个LED灯,依次循环。其中,delay函数实现了延时功能,可以根据实际需要进行修改。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)