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
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?
Previous article:51 MCU-function parameter calling
Next article:51 MCU - Array and Shift
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Application of FPGA in image processing
- [Experience sharing] [Scene reproduction project based on AI camera] AI recognition solution based on Allwinner v831
- How to isolate power modules and non-isolated power supplies respectively
- High Voltage Impedance Tuning Quick Guide
- Will 5G really bring about big changes?
- Today I suddenly thought of this: If resistors of the same resistance are first connected in series and then in parallel, what is the resistance after N?
- Let’s talk about high technology today… Why is a chip so difficult to make?
- I thought foreign manufacturers were indifferent to the RISC-V core...
- 【Zero Knowledge ESP8266 Project】1 OLED Weather Clock
- Is it necessary to enroll in a training course to learn embedded software programming?