keil软件用单片机控制8个LED流水灯来回点亮(C语言程序)

最好不要用数组

#include "reg51.h"
main()
{
unsigned int i,j;
while(1)
{
P1=0xfe; //点亮第一个LED
for(i=200;i>0;i--)
for(j=500;j>0;j--);
P1=0xfd; //点亮第二个LED
for(i=200;i>0;i--)
for(j=500;j>0;j--);
P1=0xfb; //点亮第三个LED
for(i=200;i>0;i--)
for(j=500;j>0;j--);
P1=0xf7; //点亮第四个LED
for(i=200;i>0;i--)
for(j=500;j>0;j--);
P1=0xef; //点亮第五个LED
for(i=200;i>0;i--)
for(j=500;j>0;j--);
P1=0xdf; //点亮第六个LED
for(i=200;i>0;i--)
for(j=500;j>0;j--);
P1=0xbf; //点亮第七个LED
for(i=200;i>0;i--)
for(j=500;j>0;j--);
P1=0x7f; //点亮第八个LED
for(i=200;i>0;i--)
for(j=500;j>0;j--);
}
}

或:
#include "reg51.h"
void delay(unsigned int t)
{
unsigned inti,j;
for(i=t;i>0;i--)
for(j=110;j>0;j--);
}
main()
{
unsigned char w,i;
while(1)
{
w=0xfe;
for (i=0;i<8;i++)
{
P1=w; //循环点亮LED
w<<=1; //点亮灯的位置移动,最低位补0
w=w|0x01; //将最低位置1
delay(500); //延时
}
}
}

或:
#include "reg51.h"
//程序中使用_crol_函数,所以要包含头文件"intrins.h"
#include "intrins.h"
void delay(unsigned int t)
{
unsigned int i,j;
for(i=t;i>0;i--)
for(j=110;j>0;j--);
}
main()
{
unsigned char temp;
temp=0xfe;
while(1)
{
P1=temp;
delay(500); //延时
temp=_crol_(temp,1); //点亮LED的位置循环左移一位
}
}
温馨提示:答案为网友推荐,仅供参考
第1个回答  2013-03-21
//==============================
//8个LED 闪烁
//-------------------------------------
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int

//--------------------------------
void DelayMS(uint ms)
{
uchar t;
while(ms--) for (t=0;t<120;t++);
}
//----------------------------------
void main()
{
P2= 0xfe;
while (1)
{

P2 =_crol_(P2,1);
DelayMS(200);
}
}追问

能给一份不用数组的吗?

追答

没数组啊...

本回答被提问者采纳
相似回答