Designing a MCU program does not start from scratch, but rather reading and analyzing existing related programs. Program analysis is an important step in design, modification, optimization, and maintenance. Therefore, it is very necessary to master the analysis method of MCU programs.
1. Read the equipment manual carefully.
The single-chip microcomputer system that can be put into production (use) is accompanied by an instruction manual (manual), which introduces the product's (working principle) technical indicators, usage steps, operation methods and precautions, as well as the system's relevant status, display, alarm and other information. This provides great help for reading programs and dividing modules for analysis functions. If you have this system, you should operate it repeatedly to become familiar with the usage steps and operation methods. The operation should be comprehensive and detailed to ensure that every part of the program and peripheral circuits has been executed.
2. Understand the microcontroller chip models and functions
At present, the representative single-chip computers are: 8-bit single-chip computers, namely the 51 series; 16-bit single-chip computers, namely the 96 series; 32-bit embedded microprocessors, namely the i386EX series with X86 structure; ultra-low power consumption i486 series and i960 series with RISC structure, etc. Due to the advantages of high integration, strong processing function, good reliability and low price, the MCS-51 series single-chip computers have been widely used in the fields of intelligent instruments, control engineering, testing technology, etc. The various varieties of single-chip computers require readers to first know which single-chip computer chip is used in the circuit, and to understand its pin function, memory structure, instruction system, etc.
3. Analyze and understand the functions of each part of the peripheral circuit
In the single-chip microcomputer application system, hardware is the foundation, and the function of software is to reasonably allocate and use the hardware. In the development of the control system, software and hardware design are basically carried out simultaneously.
3.1 Commonly used peripheral circuits of single-chip microcomputers
The commonly used peripheral circuits of single-chip microcomputers include program expansion circuits, memory expansion circuits, parallel port expansion circuits, watchdog circuits, serial communication level conversion circuits, LED (LCD) digital display circuits, keyboard circuits, A/D conversion circuits, D/A conversion circuits, indicator light circuits, etc. At the same time, the implementation method of the circuit must be clarified. The schematic diagram of the single-chip microcomputer peripheral circuit is shown in Figure 1.
3.2 Other circuits and connections
In order to match the impedance, add a drive circuit, use a photoelectric isolation circuit when driving a large current circuit, process the redundant input terminals of the CMOS circuit (connect to the positive power supply or ground), arrange the ground system (system ground, chassis ground or shield ground, digital ground, analog ground), and connect the signal lines including read and write signal lines, chip select signal lines, clock signal lines, read enable signal (PSEN), address latch signal (ALE), reset signal, etc., and allocate I/O addresses. Mastering these is crucial to reading the program.
4. Reading Analysis Procedure
4.1 Language used for microcontroller programming
As assembly language is concise and efficient, small single-chip computer programs often use assembly language. C language has all the characteristics of high-level languages, and can operate hardware and perform structured programming. For MCS-51 single-chip computers, ASM51, C51, etc. are currently used more. In addition, there are single-chip computer programs that are mixed with assembly language and BASI language.
4.2 Basic structure and composition of MCU program
In the entire MCU program, the initialization and self-test parts are arranged at the beginning of the MCU program, followed by the main (monitoring) program, various modules and subroutines, in addition to the glyph table and data table.
4.2.1 Traditional Program Structure
The MCU program generally adopts modular design. The central idea is to divide a program with many functions and large program volume into several relatively independent program segments according to its functions. The entire program that controls the work of the MCU application system is usually called a monitoring program. The monitoring program is divided into a monitoring main program and a command processing subroutine according to the module. The command processing subroutine completes the specific operations specified by various commands, and it is divided into different subroutine modules according to various commands. Its programming method and functional requirements are closely related to the system hardware, and there is no universal method. The monitoring main program receives and analyzes the commands from the keyboard, and then transfers the control to the entrance of the corresponding processing subroutine, playing a guiding role.
In the case of multiple meanings of a key, the monitoring main program needs to determine the direction of control. In addition to knowing the current key, it also needs to clarify the current "condition" of the system, that is, the "state". The state table method can be used to design the monitoring main program. The core is to design a state table to specify the meaning of each key in a certain state of the application system, including which command processing subroutine should be executed and the correct transition of the completion state. The format of the state table is shown in Table 1.
During design, a current state unit is allocated in the memory to store the current state. When a key is pressed, the monitoring main program consults the state table based on the current key code and the current state keywords to determine which processing subroutine the system should execute and what the next state of the system is, and immediately modifies the content of the current state unit so that the system can respond correctly when a new key is pressed. Not all keys are defined in each state, that is, there are empty keys.
When an interrupt occurs, the interrupt service routine sets the corresponding flag, and the main program constantly judges these flags to decide which handler to start. After the corresponding handler completes the relevant task, it clears the flag and returns control to the main program. For delay or timing processing, a timer can be used to complete it.
4.2.2 MCU Program Based on Time Slice
The multi-tasking concept of modern operating systems is introduced into the single-chip microcomputer real-time control system. The time slice round-robin scheduling algorithm is used to improve the traditional single-chip microcomputer programming concept and apply it in distributed systems. It has a clear structure and flexible scheduling, and is suitable for systems with complex timing requirements and strong real-time requirements.
4.3 Read the program and draw a flow chart
A program flow chart (block diagram for short) is a tool used to describe the program processing process. The reasoning process is clear, intuitive, and logical. It can be used to characterize the control flow logic of the program and provides an important auxiliary means for program testing and understanding.
Only when the analyst scans the program multiple times and draws a detailed flow chart for each module, can the tasks and connections between each module, resource allocation, and the design ideas of the entire program, the entry and exit of each program module, and the occupation of the CPU internal registers, be clarified. In addition, what control signal should the CPU send to start the A/D conversion; what is the port address; how the CPU knows that the A/D conversion is over so that the conversion result can be input; how long is the sampling cycle, etc. This is the most important step in analyzing and designing the program, and it is also the basis for logic analysis and optimization. Common functional modules of single-chip microcomputers include: timing, data acquisition, digital acquisition, digital filtering, scale conversion, control algorithm, display, alarm, printing, etc.
4.4 Algorithm Analysis
An algorithm is a set of definite and finite steps taken to solve a specific problem. It is a set of well-defined rules used to solve a problem. In practical applications, it is also called a method, approach, system, etc. For some more complex algorithms, it is best to analyze them by drawing a flowchart, which is clear, concise and intuitive.
An important part of single-chip control system program design is to reasonably select algorithms according to the characteristics of the controlled object to achieve the required control accuracy and indicators. For example, digital filtering is performed during data acquisition. Common digital filtering methods include: program judgment filtering, mid-position filtering, arithmetic average filtering, recursive average filtering, pulse interference-proof average filtering, first-order lag filtering, etc. Therefore, before reading the program, you must have a certain understanding of the solution to the problem in order to determine which specific algorithm is used.
4.5 Resource Allocation
Determine resource allocation, including ROM, timer/counter, RAM, interrupt source, etc. The most critical one is the allocation of on-chip RAM, such as 00H~2FH working registers, which are often used as pointers, counters, loop variables, etc.; 20H~2FH has bit addressing function, which is used to store various flag bits, logical variables, and state variables. A detailed RAM resource allocation table should be listed for RAM for reading and reference. In the data storage, arrange the sampling data area, processing result data area, display and print data area, flag area, etc. The allocation table can be drawn manually, but it is more convenient to use Excel table.
4.6 Flags
The microcontroller has a wealth of bit operation instructions and bit storage resources. One type serves I/O operations, such as the various logical information monitored can be stored in the bit resources and can be used by the program at any time. The other type serves the program itself, indicating the various status characteristics of the system, transmitting control information between modules, controlling the flow of the program, etc. This part is called software flags. When reading the program, each bit resource must be recorded in detail and the user manual of the software flag must be drawn. The items that need to be explained are: Name and bit address: the code of the software flag in the program and the bit unit where it is stored. Meaning: Explain the state or function represented by logic 1 and logic 0. For globally defined software flags, it has a unique definition. For locally defined software flags, its effective range must be indicated. Life cycle: Analyze the initialization state of each software flag, the conditions and time of changes when the program is running, and record them. User: The state or module that reads the software flag is called the user of the software flag. There are two ways to use software flags. One is non-destructive, which is read-only and not write-only; the other is destructive use. This software flag is mostly a certain application flag, which is cleared immediately after the response to avoid repeated responses. The same flag bit can have different meanings in different states. Multi-bit flags represent multiple states, thus saving flag bits.
4.7 Anti-interference processing
Since the working environment of the single chip microcomputer is relatively harsh and complex, it often affects the reliability and safety of the system. These interferences include power supply system interference, process channel interference, space interference, etc., which will lead to increased data acquisition errors, control state failure, data changes due to interference, and program operation abnormalities.
In order to ensure the long-term stable operation of the single-chip control system, various interferences must be processed, including necessary hardware circuits, such as hardware watchdog technology; software anti-interference technology, such as input channel anti-interference technology, digital filtering, RAM data redundancy technology, software traps, etc. The combination of software and hardware anti-interference technology can achieve the best effect.
5. Conclusion
Program design is a creative work, and program analysis is often a preparation for creative work. Reading a program starts with an existing program, analyzing its hardware and software functions, digesting and absorbing it, and then modifying, maintaining, and optimizing the program, thereby expanding the system's adaptability and improving technical indicators and performance parameters.
Previous article:Written for those who are about to get in touch with MCU or those who have just gotten in touch with MCU
Next article:LED dot matrix imitates stage lighting
Recommended ReadingLatest update time:2024-11-16 13:57
- Popular Resources
- Popular amplifiers
- Wireless Sensor Network Technology and Applications (Edited by Mou Si, Yin Hong, and Su Xing)
- Modern Electronic Technology Training Course (Edited by Yao Youfeng)
- Modern arc welding power supply and its control
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Limited time download gift: "What you need to know about USB3.1"
- Bluetooth host and slave address mutual binding communication test
- [Evaluation and experience of Zhongke Yihaiwei EQ6HL45 development platform] + ip_pll routine and eLinx software debugging
- Let’s discuss the development trend and direction of RF GaN
- Today's live broadcast prize addition: Introduction to Keysight's new optical communication test solution
- Chat with Vicor engineers about how to improve throughput and uptime of automated test equipment?
- Questions about the waveform generated by amplitude modulation by AD9106
- [Technical Video Collection] Littelfuse's Efficient, Reliable, and Accurate Power Control and Circuit Protection Solutions
- Nuvoton N76E003AQ20 3*3 package 0.4BSC lib-AD
- Solution to ARM debugging DAbt_Handler problem