单片机的led跑马灯程序

/*
* 跑马灯程序,做单片机的第一个小程序,相当于软件开发中的hello world
* 可实现led灯循环移动
* 通过P1口的8个引脚控制8个led灯的亮灭。
* 滨州学院信息技术研发中心 孙继磊  2014-4-11
*/
#include <reg52.h>
unsigned char  rotate(unsigned char n);
void main(void)
{    
	unsigned int j;
	unsigned char i=0xf0,k=0xf0;
	while(1)
	{ 
	  P1 = i;             //为p1口赋值(即相当于给8个引脚赋值)
	  for(j=0;j<40000;j++);//延时,保持当前灯亮状态一段时间,使人眼能够观察得到
	  i=rotate(i);         //循环右移
    }
}
//循环右移一位
unsigned char  rotate(unsigned char n)
{
    unsigned char t ;
    t = n & 1;		//取最低位
    n = n>>1;
    if(t) n = n | 0x80;
    return n;
}



发布了14 篇原创文章 · 获赞 4 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/greatwall_sdut/article/details/23510809