c52单片机流水灯怎么编程

时间:2025-01-25 15:18:28 游戏攻略

在C52单片机上实现流水灯,可以通过多种方法。以下是几种常见的方法:

方法一:使用goto语句

```c

include

void delay(uint xms) {

uint x, y;

for(x = xms; x > 0; x--)

for(y = 110; y > 0; y--);

}

void main(void) {

uchar temp, temp1, i;

// 流水灯向左流动

while(1) {

P1 = 0xfe;

for(i = 0; i < 7; i++) {

temp = P1;

temp = _crol_(temp, 1);

P1 = temp;

delay(200);

}

goto loop;

}

// 流水灯向右流动

loop:

while(1) {

P1 = 0x7f;

for(i = 0; i < 7; i++) {

temp1 = P1;

temp1 = cror_(temp1, 1);

P1 = temp1;

delay(200);

}

}

}

}

```

方法二:使用循环和goto语句

```c

include

void delay(uint xms) {

uint x, y;

for(x = xms; x > 0; x--)

for(y = 110; y > 0; y--);

}

void main(void) {

uchar temp, temp1, i;

// 流水灯向左流动

while(1) {

P1 = 0xfe;

for(i = 0; i < 7; i++) {

temp = P1;

temp = _crol_(temp, 1);

P1 = temp;

delay(200);

}

goto loop;

}

// 流水灯向右流动

loop:

while(1) {

P1 = 0x7f;

for(i = 0; i < 7; i++) {

temp1 = P1;

temp1 = cror_(temp1, 1);

P1 = temp1;

delay(200);

}

}

}

}

```

方法三:使用循环移位实现

```c

include

void delay(uint xms) {

uint x, y;

for(x = xms; x > 0; x--)

for(y = 110; y > 0; y--);

}

void main(void) {

uchar temp, temp1, i;

P3 = 0xff;

while(1) {

for(i = 3; i > 0; i--) {

temp = 0xaa;

P1 = temp;

delay();

temp = 0x55;

P1 = temp;

delay();

}

for(i = 3; i > 0; i--) {

temp = 0xfe;

for(i = 7; i > 0; i--) {

P1 = temp;

temp = _crol_(temp, 1);

delay();

P1 = temp;

delay();

}

}

}

}

```

方法四:使用查表法