keil5led流水灯呼吸灯
时间: 2023-11-06 13:04:40 浏览: 166
以下是Keil5的流水灯和呼吸灯代码:
流水灯代码:
```c
#include <reg52.h>
void delay(unsigned int xms) //延时函数
{
unsigned int i, j;
for (i = xms; i > 0; i--)
for (j = 112; j > 0; j--);
}
void main()
{
unsigned char i;
while (1)
{
for (i = 0x01; i < 0x80; i <<= 1)
{
P1 = ~i;
delay(500); //延时500ms
}
}
}
```
呼吸灯代码:
```c
#include<reg52.h>
void delay(unsigned int xms) //延时函数
{
unsigned int i, j;
for (i = xms; i > 0; i--)
for (j = 112; j > 0; j--);
}
void main()
{
unsigned char i, j;
while (1)
{
for (i = 0; i < 255; i++)
{
for (j = 0; j < 30; j++)
{
P1 = i;
delay(10); //延时10ms
}
}
for (i = 255; i > 0; i--)
{
for (j = 0; j < 30; j++)
{
P1 = i;
delay(10); //延时10ms
}
}
}
}
```
这两个程序都是使用单片机控制 LED 灯的亮灭,流水灯是让 LED 灯从左往右依次亮起,然后从右往左依次亮起;呼吸灯是让 LED 灯逐渐变暗,然后逐渐变亮。
阅读全文