1、51单片机常见的五种流水灯编写方式通过改变赋值实现流水灯void main() while(1) P2=0xfe; delay1ms(500); P2=0xfd; delay1ms(500); P2=0xfb; delay1ms(500); P2=0xf7; delay1ms(500); P2=0xef; delay1ms(500); P2=0xdf; delay1ms(500); P2=0xbf; delay1ms(500); P2=0x7f; delay1ms(500); 通过公式运算实现流水灯void main() while(1) uint a,b=1; P2=0xfe; delaym
2、s(500); for(a=0;a7;a+) P2-=b; / P2=P2-b delayms(500); b=b*2; /都化为同一类型进制运算 通过操作符与“|”实现流水灯(通过移位实现流水灯)void main()uchar a,i; while(1) a=0xfe; /点亮第一位LED灯for(i=0;i8;i+) P2=a;a=a1; /左移一位a=a|0x01; /左移一位后与0x01相或,保证左移后最低位为1 delay_ms(500); 通过库函数_crol_(字符左移)实现流水灯void main() uint a;a=0xfe; while(1) P2=a;a=_crol_(a,1);delay_ms(500);采用数组实现流水灯uchar code table=0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f;void main(void) uchar i; while (1) for(i=0;i8;i+) /循环点亮8只LED灯 P2=tablei; delay_ms(500); /延时500毫秒