51 MCU-Flowing Light

Publisher:草木知秋Latest update time:2021-08-16 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This lecture explains how to use the 8 IO ports of P0 to control 8 LEDs to realize a flowing light.


The hardware connection of the development board used in this tutorial is simplified as follows

3.png

1. Digital simulation lighting


We use eight bits of binary to represent eight lights. If the corresponding bit is 0, the light is on, and if it is 1, the light is off.


The implementation of the water lamp is as follows


11111110 //corresponding hexadecimal = 0xFE, P0.0 outputs 0, and the rest outputs 1


11111101 //Corresponding hexadecimal = 0xFD, P0.1 outputs 0, and the rest outputs 1


11111011 //Corresponding hexadecimal = 0xFB, P0.2 outputs 0, and the rest outputs 1


11110111 //corresponding hexadecimal = 0xF7, P0.3 outputs 0, the rest outputs 1


11101111 //corresponding hexadecimal = 0xEF, P0.4 outputs 0, the rest outputs 1


11011111 //corresponding hexadecimal = 0xDF, P0.5 outputs 0, the rest outputs 1


10111111 //corresponding hexadecimal = 0xBF, P0.6 outputs 0, the rest outputs 1


01111111 //corresponding hexadecimal = 0xF7, P0.7 outputs 0, the rest outputs 1


11111110


11111101


......


2. Flowing light code


Here, to control 8 IO ports at a time, there is no need to define a single IO port with sbit (please review the second lecture of this chapter). With the foundation of the previous chapter, the implementation of the code is not difficult.


#include  

//sbit LED2 = P0^0; This statement is not used in this section of the code

sbit ADDR2 = P1^2;

sbit ADDR1 = P1^1;

sbit ADDR0 = P1^0;

sbit ENLED = P1^4;

sbit ADDR3 = P1^3;

 

void delay_ms(unsigned int x)

{

    unsigned int i,j;

    if(x==1000)

    {

        for(i=0;i<19601;i++)//delay 1s

        {

            for(j=5;j>0;j--);

        }

    }

    else while(x--)for(j=115;j>0;j--);

}

 

void main()

{  

    ADDR3 = 1; // Enable 38 decoder

    ENLED = 0; // 

   

    ADDR2 = 1; //****************************

    ADDR1 = 1; //Let IO6 of the 38 decoder output low level

    ADDR0 = 0; //****************************

   

    while(1)

    {

        P0 = 0xFE; 

        delay_ms(100); // Delay for a while 

        P0 = 0xFD; 

        delay_ms(100);

        P0 = 0xFB; 

        delay_ms(100);   

        P0 = 0xF7; 

        delay_ms(100);

        P0 = 0xEF; 

        delay_ms(100);   

        P0 = 0xDF; 

        delay_ms(100);

        P0 = 0xBF; 

        delay_ms(100);   

        P0 = 0x7F; 

        delay_ms(100);

    }

}

 

3. Code Optimization


In order to let everyone understand the use of switch statements in MCU programs for the first time (the usage of switch statements is introduced in Section 6.3 of the document "Teaching You to Learn 51 MCU Step by Step"), we use switch statements to make the code look neater and do not need to repeatedly write "delay_ms(100);", which achieves writing optimization and avoids taking up too much space. The main function is modified as follows


void main()

{  

    unsigned char i=1; //Define new variables to call different case statements in turn

    ADDR3 = 1; // Enable 38 decoder

    ENLED = 0; // 

   

    ADDR2 = 1; //****************************

    ADDR1 = 1; //Let IO6 of the 38 decoder output low level

    ADDR0 = 0; //****************************

   

    while(1)

    {

        switch(i)

        {   

            case 1: P0 = 0xFE; break;

            case 2: P0 = 0xFD; break;

            case 3: P0 = 0xFB; break;   

            case 4: P0 = 0xF7; break;

            case 5: P0 = 0xEF; break;   

            case 6: P0 = 0xDF; break;

            case 7: P0 = 0xBF; break;  

            case 8: P0 = 0x7F; i=0; break; //i returns to 0 and the statement is executed from case 1 again

        } 

        i++; //i changes from 1 to 8, so that the switch statement calls "P0 = xxxx;" in turn

        delay_ms(100);

    }

}


4. Reminder


It is mentioned here that there is a variable named i in the "delay_ms();" function, and the same variable named i is also in the main function, but these two variables with the same name are local variables (there is a brief explanation in Section 7.1 of the document). The two variables with the same name each occupy independent memory and are not affected by their own value changes. The basic knowledge of C language is popularized.


In the main function, we define i as unsigned char type to save RAM memory. After all, we only need i in the main function to vary between 1 and 8, not more than 255 (Section 4.2 of the document has popular data types). And don’t forget to add “break;” at the end of the case statement to prevent the program from executing from “case 1” to “case 8” at one time without implementing the delay part. You can try to remove all 8 “break;”, then compile and download it to the development board to see what happens?

Reference address:51 MCU-Flowing Light

Previous article:51 MCU-function parameter calling
Next article:51 MCU - Array and Shift

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号