論壇中常見的是8位流水燈程序,4位的很少見,而本人近期波及到這種設計,特分享以下程序,大佬飄過,一般學習者請笑納:
/*****************************************
*實驗效果:程序運行后,實現流水燈圖形
*****************************************/
#include <REGX52.H> //包含單片機寄存器的頭文件
#include<intrins.h>
#define uchar unsigned char
uchar j, num;
void delay(unsigned int xms) //延時x毫秒(ms)
{
unsigned int i, j;
for(i = xms; i > 0; i--)
for(j = 112; j > 0; j--);
}
void main()
{
num = 1;
P0 = 0x01; //P0=0000 0001,對應第1個燈亮
delay(1000); //延遲1秒
while(1) //無限循環,以使led燈持續閃爍,并防止程序跑飛
{
if(num == 1)
{
for(j = 0; j < 3; j++)
{
P0 = _crol_(P0, 1); //左移函數,依次是00000010,00000100,00001000
delay(1000); //延遲1秒
if(P0 == 0x08)
num = 0;
}
}
else
{
for(j = 0; j < 3; j++)
{
P0 = _cror_(P0, 1); //左移函數,依次是0000 0100,0000 0010,0000 0001
delay(1000); //延遲1秒
if(P0 == 0x01)
num = 1;
}
}
}
}
|