流水灯代码

流水灯代码可以根据不同的平台和需求有多种实现方式。以下是几种常见的流水灯代码示例:

1. 基于AT89C51单片机的流水灯代码

#include <reg51.h>
#define unint unsigned int
#define on 0
#define off 1

sbit led1 = P1^0;
sbit led2 = P1^1;
sbit led3 = P1^2;
sbit led4 = P1^3;
sbit led5 = P1^4;
sbit led6 = P1^5;
sbit led7 = P1^6;
sbit led8 = P1^7void delay(unsigned int time);
void led_init(void);

void main(void) {
    led_init();
    while(1) {
        led1 = on; led2 = off; delay(100);
        led1 = off; led2 = on; delay(100);
        led1 = on; led2 = off; delay(100);
        led1 = off; led2 = on; delay(100);
        led1 = on; led2 = off; delay(100);
        led1 = off; led2 = on; delay(100);
        led1 = on; led2 = off; delay(100);
        led1 = off; led2 = on; delay(100);
    }
}

void delay(unsigned int time) {
    unsigned int i, j;
    for (i = 0; i < time; i++)
        for (j = 0; j < 200; j++);
}

void led_init(void) {
    P1 = 0xFF// 初始化所有LED为灭
}

2. 基于Arduino平台的流水灯代码

const int LEDPin1 = 2const int LEDPin2 = 3const int LEDPin3 = 4const int LEDPin4 = 5const int LEDPin5 = 6const int delayTime = 200void setup() {
    pinMode(LEDPin1, OUTPUT);
    pinMode(LEDPin2, OUTPUT);
    pinMode(LEDPin3, OUTPUT);
    pinMode(LEDPin4, OUTPUT);
    pinMode(LEDPin5, OUTPUT);
}

void loop() {
    digitalWrite(LEDPin1, HIGH); digitalWrite(LEDPin2, HIGH); delay(delayTime); digitalWrite(LEDPin2, LOW);
    digitalWrite(LEDPin3, HIGH); digitalWrite(LEDPin4, HIGH); delay(delayTime); digitalWrite(LEDPin3, LOW);
    digitalWrite(LEDPin4, LOW); digitalWrite(LEDPin5, HIGH); delay(delayTime); digitalWrite(LEDPin4, LOW);
}

3. 基于Verilog的流水灯代码

Top