laitimes

8 LEDs light up left and right

author:August Sophora flowers 2013

03 8 LEDs lit up left and right /* Name: 8 LEDs lit up left and right

To use an 8-bit microcomputer to achieve the left and right blinking of 8 LED lights, you need to first determine which brand and model of single-chip microcomputer to use. Here, I take STC89C52RC microcontroller as an example to give a simple example program.

Here is the code:

c复制代码#include <reg52.h>

// 定义LED灯的IO口
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^7;

void delay(unsigned int count) {
    while(count--);
}

int main() {
    while(1) {
        // 左 -> 右
        for (int i = 1; i <= 8; ++i) {
            switch (i) {
                case 1: LED1 = 1; break;
                case 2: LED2 = 1; break;
                case 3: LED3 = 1; break;
                case 4: LED4 = 1; break;
                case 5: LED5 = 1; break;
                case 6: LED6 = 1; break;
                case 7: LED7 = 1; break;
                case 8: LED8 = 1; break;
            }
            delay(10000);

            switch (i) {
                case 1: LED1 = 0; break;
                case 2: LED2 = 0; break;
                case 3: LED3 = 0; break;
                case 4: LED4 = 0; break;
                case 5: LED5 = 0; break;
                case 6: LED6 = 0; break;
                case 7: LED7 = 0; break;
                case 8: LED8 = 0; break;
            }
        }

        // 右 -> 左
        for (int i = 8; i >= 1; --i) {
            switch (i) {
                case 1: LED1 = 1; break;
                case 2: LED2 = 1; break;
                case 3: LED3 = 1; break;
                case 4: LED4 = 1; break;
                case 5: LED5 = 1; break;
                case 6: LED6 = 1; break;
                case 7: LED7 = 1; break;
                case 8: LED8 = 1; break;
            }
            delay(10000);

            switch (i) {
                case 1: LED1 = 0; break;
                case 2: LED2 = 0; break;
                case 3: LED3 = 0; break;
                case 4: LED4 = 0; break;
                case 5: LED5 = 0; break;
                case 6: LED6 = 0; break;
                case 7: LED7 = 0; break;
                case 8: LED8 = 0; break;
            }
        }
    }
}
           

In the above program, LED1 to LED8 correspond to the P1.0 to P1.7 ports of the single-chip microcomputer, respectively. In the main() function, the effect of flashing from left to right and right to left is achieved through two for loops, respectively. Each LED light is lit for a period of time, and then turned off again to move to the next LED light.

Note: The above code is only an example, and the specific implementation needs to be adjusted according to the actual situation.

It can also be implemented using loop functions as follows

Description: The program uses the cyclic shift functions _crol_ and _cror_ to form the effect of scrolling back and forth

8 LEDs light up left and right
8 LEDs light up left and right
8 LEDs light up left and right

Read on