1.实现原理
通过不断给P1中不同的IO口置低电平,从而达到LED流水灯的效果。
2.程序思路
方法一:通过给P1口赋不同的值从而达到流水灯的效果
/* 头文件声明区域 */
#include <REGX52.H>/* 延时函数 */ //需要记!!!
void Delay(unsigned char xms) //@12.000MHz
{unsigned char data i, j;while(xms--){i = 2;j = 239;do{while (--j);} while (--i);}
}/* 流水灯Main */
void main()
{while(1){//法一:通过给P1口赋不同的值从而达到流水灯的效果P1_3 = 1;P1_0 = 0;Delay(500);P1_0 = 1;P1_1 = 0;Delay(500);P1_1 = 1;P1_2 = 0;Delay(500);P1_2 = 1;P1_3 = 0;Delay(500);}
}
方法二:通过内置函数库实现流水灯效果
_crol_:循环左移 _cror_:循环右移 所属库:intrins.h
/* 头文件声明区域 */
#include <REGX52.H>
#include <intrins.h>/* 延时函数 */ //需要记!!!
void Delay(unsigned char xms) //@12.000MHz
{unsigned char data i, j;while(xms--){i = 2;j = 239;do{while (--j);} while (--i);}
}/* 变量声明区域 */
unsigned char ucLed = 0xfe;//第一个灯点亮/* 流水灯Main */
void main()
{while(1){//法二:通过内置函数库达到流水灯效果// _crol_:循环左移 _cror_:循环右移 所属库:intrins.hucLed = _crol_(ucLed,1); //(变量名,移动位数)P1 = ucLed;Delay(500);}
}
若想实现流水灯变速,可以再定义一个变量time,然后延时。
/* 头文件声明区域 */
#include <REGX52.H>
#include <intrins.h>/* 延时函数 */ //需要记!!!
void Delay(unsigned char xms) //@12.000MHz
{unsigned char data i, j;while(xms--){i = 2;j = 239;do{while (--j);} while (--i);}
}/* 变量声明区域 */
unsigned char ucLed = 0xfe;//第一个灯点亮
unsigned int time = 1000;//做变速处理/* 流水灯Main */
void main()
{while(1){//法二:通过内置函数库达到流水灯效果// _crol_:循环左移 _cror_:循环右移 所属库:intrins.hucLed = _crol_(ucLed,1); //(变量名,移动位数)P1 = ucLed;Delay(time);time = time - 100;}
}