效果如下
51单颗流水循环灯(博客自用)
代码中有较为详细的实现逻辑思路
#include <STC89C5xRC.H>
#include <INTRINS.H>
#define LED P0
//因为char类型占用1个字节也就是8个比特所以定义一个char类型
typedef unsigned char u8;
//定义一个延迟函数
void Delay_1MS(u16 t)//@11.0592MHz
{
unsigned char i, j;
while (t > 0) {
t--;
_nop_();
i = 2;
j = 199;
do {
while (--j);
} while (--i);
}
}
void main(){
u8 temp = 1; //其实值为0000 0001
while(1){
LED = ~temp; //取反低电平点亮 1111 1110
temp <<= 1; //按位左移 第一次 0000 0010
if(temp == 0){ //移动8次后 结果为 0000 0000
temp = 128; //满足条件重新赋值 为 1000 0000
break; //结束此次循环
}
Delay_1MS(100);
}
while(1){
LED = ~temp; //取反 0111 1111
temp >>= 1; //按位右移 第一次 0100 0000
if(temp == 0){
break;
}
Delay_1MS(100);
}
}