51单片机小灯左循环
时间: 2023-12-29 08:24:08 浏览: 92
以下是51单片机小灯左循环的代码实现:
```c
#include <reg51.h> //导入51的库
sbit led1 = P1^0; //定义小灯1的负极接到的端口,P1.0
sbit led2 = P1^1; //定义小灯2的负极接到的端口,P1.1
sbit led3 = P1^2; //定义小灯3的负极接到的端口,P1.2
sbit led4 = P1^3; //定义小灯4的负极接到的端口,P1.3
sbit led5 = P1^4; //定义小灯5的负极接到的端口,P1.4
sbit led6 = P1^5; //定义小灯6的负极接到的端口,P1.5
sbit led7 = P1^6; //定义小灯7的负极接到的端口,P1.6
sbit led8 = P1^7; //定义小灯8的负极接到的端口,P1.7
void main() //主函数
{
unsigned int delay;
while(1){ //死循环防止程序退出
led1 = 1; //点亮小灯1
delay = 20000; //延时
while(delay--); //空循环delay次,实现简单延时
led1 = 0; //熄灭小灯1
led2 = 1; //点亮小灯2
delay = 20000; //延时
while(delay--); //空循环delay次,实现简单延时
led2 = 0; //熄灭小灯2
led3 = 1; //点亮小灯3
delay = 20000; //延时
while(delay--); //空循环delay次,实现简单延时
led3 = 0; //熄灭小灯3
led4 = 1; //点亮小灯4
delay = 20000; //延时
while(delay--); //空循环delay次,实现简单延时
led4 = 0; //熄灭小灯4
led5 = 1; //点亮小灯5
delay = 20000; //延时
while(delay--); //空循环delay次,实现简单延时
led5 = 0; //熄灭小灯5
led6 = 1; //点亮小灯6
delay = 20000; //延时
while(delay--); //空循环delay次,实现简单延时
led6 = 0; //熄灭小灯6
led7 = 1; //点亮小灯7
delay = 20000; //延时
while(delay--); //空循环delay次,实现简单延时
led7 = 0; //熄灭小灯7
led8 = 1; //点亮小灯8
delay = 20000; //延时
while(delay--); //空循环delay次,实现简单延时
led8 = 0; //熄灭小灯8
}
}
```
阅读全文