LED流水灯
思路:每次LED灯熄灭后,下一个LED灯亮
#include <REGX52.H>
#include <INTRINS.H>void Delay500ms() //@12.000MHz
{unsigned char i, j, k;_nop_();i = 4;j = 205;k = 187;do{do{while (--k);} while (--j);} while (--i);
}void main()
{while(1){p2 = 0xFE;//1111 1110Delay500ms();p2 = 0xFD;//1111 1101 Delay500ms();p2 = 0xFB;//1111 1011 Delay500ms();p2 = 0xF7;//1111 0111 Delay500ms();p2 = 0xEF;//1110 1111 Delay500ms();p2 = 0xDF;//1101 1111Delay500ms();p2 = 0xBF;//1011 1111 Delay500ms();p2 = 0x7F;//0111 1111 Delay500ms();}
}
C51数据类型
可自定义延时时间【写一个带有参数的函数】
#include <REGX52.H>void Delay1ms(unsigned int xms); //@12.000MHzvoid main()
{while(1){P2=0xFE;//1111 1110Delay1ms(1000);P2=0xFD;//1111 1101Delay1ms(1000);P2=0xFB;//1111 1011Delay1ms(100);P2=0xF7;//1111 0111Delay1ms(100);P2=0xEF;//1110 1111Delay1ms(100);P2=0xDF;//1101 1111Delay1ms(100);P2=0xBF;//1011 1111Delay1ms(100);P2=0x7F;//0111 1111Delay1ms(100);}
}void Delay1ms(unsigned int xms) //@12.000MHz
{unsigned char i, j;while(xms){i = 2;j = 239;do{while (--j);} while (--i);xms--;}
}