How to analyze microcontroller programs

Publisher:RadiantGlowLatest update time:2015-07-20 Source: 51heiKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Analyzing the microcontroller program is a preparation for writing the program, including the analysis of the hardware circuit and software functions, proposing practical analysis methods, and summarizing the general steps of the analysis program and the issues that should be paid attention to.

    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.

Keywords:MCU Reference address:How to analyze microcontroller programs

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

How to light up and turn off a diode using MCU assembly language
main: mov P0, #10011000B //P0 port outputs high level lcall Delay //Delay mov P0, #11111111B Call Delay ljmp main //Long transfer to the starting address main label   Delay: mov R7, #250 D1: mov R6, #250 D2: djnz R6, D2 //If the R6 register is not 0, transfer to D2 label djnz R7, D1 //If the R7 registe
[Microcontroller]
PIC microcontroller RS232 serial communication
Hello everyone, through the previous study, we have learned and become familiar with the usage and learning methods of ICD2 simulation burner and enhanced PIC experiment board, and learned how to use single-chip microcomputer to control light-emitting tubes, relays, buzzers, buttons, digital tubes and other resources,
[Microcontroller]
PIC microcontroller RS232 serial communication
New improved LCD screen polarization drive circuit design based on MCU
introduction Due to design and process reasons, some panels have problems such as the characteristics of liquid crystal molecules being easily destroyed. Therefore, when designing the driving circuit of the liquid crystal panel, it is necessary to add a special functional circuit to control the deflection direction
[Microcontroller]
New improved LCD screen polarization drive circuit design based on MCU
[MCU Framework] Framework file layering introduction
What is a framework? The program framework is actually similar to a file outline or template. Because writing a program is similar to writing an article, if you don't have an outline or template, then you will have a hard time writing. A good framework can get twice the result with half the effort, save time, and redu
[Microcontroller]
[MCU Framework] Framework file layering introduction
Timing Analysis of Power-on Reset and Reset Delay of 80C51 MCU
The power-on reset (POR) of the 80C51 microcontroller is essentially a power-on delay reset, which means that the microcontroller is locked in the reset state during the power-on delay period. Why is it necessary to add a certain delay time every time the microcontroller is powered on? The analysis is as follows.
[Industrial Control]
Timing Analysis of Power-on Reset and Reset Delay of 80C51 MCU
Design of intelligent charger based on UCC3895 and PIC microcontroller
Using the new generation of phase-shifted PWM control chip UCC3895 and PICl6F917 single-chip computer, an intelligent charger is designed and developed for commonly used lead-acid batteries. Its hardware design ideas and software implementation methods are introduced, and an intelligent control strategy is
[Industrial Control]
Design of intelligent charger based on UCC3895 and PIC microcontroller
51 microcontroller to make a simple electric fan timing controller
The mechanical timer of my bedside fan was broken, so I prepared a 51 microcontroller to design one. The program passed preliminary debugging on the experimental board, and I could start making the board after the components were sent. It was just a waste of the resources of the microcontroller. 1. Design ideas: Sta
[Microcontroller]
51 microcontroller to make a simple electric fan timing controller
Research on the testing method of RAM in single chip microcomputer system
In various single-chip microcomputer application systems, the normality of the memory is directly related to the normal operation of the system. In order to improve the reliability of the system, it is necessary to test the reliability of the system. Through testing, the damage caused by memory failure to the system
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号