❶ 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的位置循环左移一位
}
}
❷ 51单片机C语言如何实现8个流水灯左移三次,后右移三次;如此循环
51单片机C语言实现循环8个流水灯左移三次,后右移三次。
例程:
#include<reg51.h>//51单片机头文件
#include<intrins.h>//包含有左右循环移位子函数的库
#defineuintunsignedint//宏定义
#defineucharunsignedchar//宏定义
sbitbeep=P2^3;
voiddelay(uintz)//延时函数,z的取值为这个函数的延时ms数,如delay(200);大约延时200ms.
{//delay(500);大约延时500ms.
uintx,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
voidmain()//主函数
{
uchara,i,j;
while(1)//大循环
{
a=0xfe;//赋初值
for(j=0;j<3;j++)for(i=0;i<8;i++)//左移三次
{
P1=a;//点亮小灯
beep=0;//开启蜂鸣器
delay(50);//延时50毫秒
beep=1;//关闭蜂鸣器
delay(50);//再延时50毫秒
a=_crol_(a,1);//将a变量循环左移一位
}
a=0x7f;
for(j=0;j<3;j++)for(i=0;i<8;i++)//右移三次
{
P1=a;//点亮小灯
beep=0;//开启蜂鸣器
delay(50);//延时50毫秒
beep=1;//关闭蜂鸣器
delay(50);//再延时50毫秒
a=_cror_(a,1);//将a变量循环右移一位
}
}
}
51单片机是对所有兼容Intel 8031指令系统的单片机的统称。该系列单片机的始祖是Intel的8031单片机,后来随着Flash rom技术的发展,8031单片机取得了长足的进展,成为应用最广泛的8位单片机之一,其代表型号是ATMEL公司的AT89系列,它广泛应用于工业测控系统之中。很多公司都有51系列的兼容机型推出,今后很长的一段时间内将占有大量市场。51单片机是基础入门的一个单片机,还是应用最广泛的一种。