1. Clean code
Generally, the reading of single-chip microcomputer programs starts with the main function. The program writing method in the previous lecture is not concise in the main function, which affects the efficiency of reading the code. Sometimes we only need to know what a statement means, so we encapsulate the delay part into a function, and call the function name in the main function to indicate that the statement here is delayed for 1 second. This is much more readable and concise. Readers can also first understand Section 4.6 in the document "Teaching You to Learn 51 Single-chip Microcomputers Step by Step".
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 twenty one twenty two twenty three twenty four 25 26 27 28 29 30 31 32 33 34 | #include sbit LED2 = P0^0; sbit ADDR2 = P1^2; sbit ADDR1 = P1^1; sbit ADDR0 = P1^0; sbit ENLED = P1^4; sbit ADDR3 = P1^3;
void delay()//The delay function is encapsulated as a function { unsigned int i,j; //define two variables to complete the delay function for(i=0;i<19601;i++)//delay 1s { for(j=5;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) { LED2=0; //light up the rightmost light delay(); //delay 1s LED2=1; //Turn off the rightmost light delay(); //delay 1s } } |
The principle is very simple. We can use a diagram to show what function encapsulation is.
Here, the variables i and j are simply defined in "void delay()", and the functions implemented are the same as the code in the previous lecture. However, the author found through software simulation that this will take up 4 microseconds of the CPU calling the function. For the convenience of reading, it is necessary to sacrifice these 4 microseconds. However, we do not need to add statements such as "LED2=0;" or "i=0;" at the end to solve the strange phenomenon mentioned in the previous lecture that the light jump and the stopwatch timing digital jump are not synchronized.
We recommend using function encapsulation for modular programming, because this is more in line with programming ideas and is also a programming technique we must use.
2. Suggestions
Although there is not much code in this section, we should develop the habit of reading the code from the main function instead of reading the code from the beginning. For example, the first thing that catches our eyes is the "void delay()" function. At this time, we should not enter the function body to read the content inside, but immediately find "void main()" to read and study the code inside. When we see "delay();", we have a first impression of this function with the comment, and then we can check the content of this function.
The common steps are as follows:
You must press F12 after compiling. Although the advantage of this method is not obvious when the amount of code is small, when we read other people's code, the amount of code is generally large, and this method can quickly understand the layers of meaning of the program written by others. Regarding this habit, everyone will understand it after learning the following programs.
Previous article:51 MCU-Delay 1 second
Next article:51 MCU-function parameter calling
Recommended ReadingLatest update time:2024-11-15 07:54
- Popular Resources
- Popular amplifiers
- MCU C language programming and Proteus simulation technology (Xu Aijun)
- 100 Examples of Microcontroller C Language Applications (with CD-ROM, 3rd Edition) (Wang Huiliang, Wang Dongfeng, Dong Guanqiang)
- Fundamentals and Applications of Single Chip Microcomputers (Edited by Zhang Liguang and Chen Zhongxiao)
- Single chip microcomputer control technology (Li Shuping, Wang Yan, Zhu Yu, Zhang Xiaoyun)
- 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
- CGD and Qorvo to jointly revolutionize motor control solutions
- CGD and Qorvo to jointly revolutionize motor control solutions
- Keysight Technologies FieldFox handheld analyzer with VDI spread spectrum module to achieve millimeter wave analysis function
- Infineon's PASCO2V15 XENSIV PAS CO2 5V Sensor Now Available at Mouser for Accurate CO2 Level Measurement
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- Advanced gameplay, Harting takes your PCB board connection to a new level!
- A new chapter in Great Wall Motors R&D: solid-state battery technology leads the future
- Naxin Micro provides full-scenario GaN driver IC solutions
- Interpreting Huawei’s new solid-state battery patent, will it challenge CATL in 2030?
- Are pure electric/plug-in hybrid vehicles going crazy? A Chinese company has launched the world's first -40℃ dischargeable hybrid battery that is not afraid of cold
- JHL HOOD 1969 small class A amplifier board
- Solutions to several difficult problems in the CCS debugging process
- [MM32 eMiniBoard Review] Part 7: IIC Read and Write EEPROM
- Altium Designer's dirty tricks to combat piracy
- TMS320F2812 generates PWM waveform process
- TI Power Stage Designer Tool Introduction Documentation
- I would like to ask you, the STM32CUBEMX+SPI+DMA method always sends incorrectly. My chip is STM32F103RC
- Keysight Technology Award-winning Live Broadcast | Thanksgiving Month Opening Ceremony - Oscilloscope Lecture Hall Registration Starts~
- LLC resonance start-up conditions
- GPIO configuration and usage process of CC1310SimpleLink SDK under CCS8