#include <reg52.h>
#define SIZE 8
void delay(void); /*function prototype of delay*/
void light_on_led(int num); /*function prototype of light_led*/
void flow_water_led(void); /*function prototype of flow_water_led*/
int main (void)
{
flow_water_led();
return 0;
}
/*the function of delay*/
void delay(void)
{
unsigned int num = 3000;
while (num--);
}
/*the function of light_led*/
void light_on_led(int num)
{
unsigned char led = 0x00;
unsigned char temp = 0x01;
temp <<= num-1; /*move the 1*/
led ^= temp;
P1 = ~led;
}
/*the function of flow_water_led()*/
void flow_water_led(void)
{
int i = 0;
while (1){
while (i < SIZE) {
light_on_led(i+1);
delay();
P1 = 0xff;
delay();
i++;
}
i--;
while (i > -1){
light_on_led(i+1);
delay();
P1 = 0xff;
delay();
i--;
}
}
}
说明:
1.程序分为三个主要部分,点亮任意一个num的灯,延迟,生成流水线。
2.点亮用的移位位取反,总线操作
3.延迟用的while()循环
4.流水线设计为顺序下去,倒序上来。