When we edit the C language of PIC microcontroller, we often use the simulation technology of the source program, because the simulation of the program can replace the hardware production and debugging of part of the microcontroller. Even when we study the C language program of PIC microcontroller in depth, the simulation technology is indispensable. Here we use the program example to demonstrate its operation method.
The simulation of the source program can complete the following functions: directly observe the whole process of the C program from the main program main(); in order to clearly observe the changes of various variables and registers in the program, the single-step run (stepinto) command and the single-step over (stepover) command can be used for simulation running debugging, making it more convenient to observe variables; set a breakpoint and then select the Run (fast) command, the program will stop at the breakpoint, so as to observe the variable function; in addition, it can also quantitatively observe the change value in the program, such as directly observing the delay of the program and many other functions. Of course, when performing program simulation, there are some technical processing of the source program, which will be described separately below.
1. Introduction to C Compilation of PICC and PICC18
To develop PIC microcontrollers and simulation debugging programs in C language, you must have a corresponding compiler. The C compilers of PICC and PICC18 are optimized PIC microcontroller C compilers, developed by Australia's HI-TECH company. This compiler is currently the PIC series microcontroller C compiler software with excellent performance and fully complies with the US ANSIC standard. PICC is used for PIC16 series products: PICC18 can be used for PIC18 series products; both compilers can run in the MPLABIDE integrated development environment of Microchip.
Since the C compiler runs in the MP/ABIDE integrated development environment, users are required to be familiar with the usage of the MPLABIDE integrated development environment.
(1) The main features of the PICC and PICC18 compilers. This compiler can debug source code programs in the MPLAIDE integrated development environment. Breakpoints can be set on the source program, and when the source program reaches the breakpoint, it will automatically stop, so that the operator can observe and modify the variable values; single-step and continuous operation of the program in the integrated development environment also facilitates the operator to observe the values of various variables in the program; supports hardware simulation, namely MPLABICE (ln-CircuitEmulator), supports software simulation, namely MPLABSIM (Simulator) and some online debuggers, such as MPLABICD (ln-CircuitDebugger) ICD2 debugger. In addition, mixed programming of assembly language and C language is allowed in a single project.
(2) Variable types supported by the PICC and PICC18 compilers. When editing a PIC microcontroller C program, be sure to write the program in the format of the variable types supported by the PICC and PICC18 compilers. These formats are shown in the attached table.
The above contents can be explained in the following program examples. The software of MPLABIDE, PICC and PICC18 can be downloaded from the website and installed on your computer. Note: the computer desktop should be XP system.
2. Preparation before software simulation debugging of source program
When debugging the software simulation of the PIC microcontroller, you should first edit its source program in the MPLABIDE integrated development environment (the author uses MPLABIDEv7.40 version) and name it XX.C.
Then create a project and compile the program with the PICC compiler (in the MPLABIDE environment). Finally, after the target code .hex is generated, the source program can be simulated and debugged.
Transfer the C program PIC1.c that has been successfully compiled in C to the desktop of MPLABIDE directly (if the author wants to quote PICl.e for simulation debugging, it should still be compiled by the pice compiler in the MPLABIDE environment to generate .hex before simulation debugging can be performed), as shown in the following figure (partial interface). MPLABIDEV7.40 and part of the PICl.c program are marked on the interface of Figure 4.
Use the mouse to click on the Debugger (debug) on the interface, and select the command MPLAB SIM (software simulation debugging) under the SelectTool drop-down menu. Use the mouse to click it to generate the interface (part) shown above. There are 7 buttons (debug shortcut icons) at the top of the interface. In order to illustrate the problem, remove them separately, as shown in the figure below.
The characteristics of each button command:
(1) Run: Click the Run button with the mouse to run the program directly until a breakpoint is encountered (described below) or the Halt button is clicked. In most cases, the Run mode is not used for program debugging because it is difficult to observe the status of the program during operation. It is only useful when breakpoints are set in the program.
(2) Halt: Click the Halt button with the mouse to put the microcontroller into a shutdown state. If other buttons are working and the program is running, just click the Halt button and the program will stop running immediately.
(3) Animate: Click this button with the mouse to slow down the simulation program and continuously refresh the register values during operation. The change of register values can be observed using the watch window (quantitative observation).
(4) Step Into: Click this button with the mouse to execute only one code instruction, then stop the computer. Click the button again to execute another code instruction.
(5) Step over: Click this button with the mouse to execute the called subroutine completely and then pause at the next instruction opcode. This is particularly useful when the called program is a delay program, which is completely equivalent to single-step execution.
(6) Step out: When you click this button with the mouse, the program will be exited directly.
(7) Reset: Click this button with the mouse to reset the program.
The above-mentioned simulation button functions are often used in combination rather than individually in actual program simulation debugging. This will be clearly seen in the following simulation debugging.
3. Software simulation operation of source program
(1) Qualitatively observe the running process of the source program PICl.e: Place the successfully compiled PICl.e source program Figure 5 on the computer desktop and enlarge it to obtain the complete interface. Then use the mouse to click the Animate button (slow speed), and then quickly click the Halt button (halt) in Figure 6. This will allow the program to quickly run to the main function and stop running, as shown in the figure below.
From the interface, you can see that on the left side below the main program main(), there is a yellow arrow pointing to the assignment statement TRISB=0x00 in the program body; it means that the C program always starts from the main program. Then click the button Step Into (single step) in Figure 6 with the mouse. Each time you click the yellow arrow, the program will run downwards in sequence until it reaches the main display function display(x) for the first time; you can stop clicking, as shown in the figure below.
The reason why the program runs to the main display function display(x) for the first time is because of the assignment statement x=0 in the body of the main() function; if you click the Step Into button with the mouse again, the program will go to the called function, and the actual parameter 0 of the main function will be passed to the formal parameter of the called function Voiddisplay(unsigned int x) by value, and then the called function will execute its display function, and the digital tube LED will display 00. The display interface is shown in the figure below.
If you want to run the program again, you need to keep clicking the Step Into button with the mouse until it reaches the assignment statement urut_bit=X%l0;, and then stop clicking, as shown in the figure below.
To run the program again, use the Step over button. The program will then run sequentially, as shown in the figure below.
Since the program runs to while (d>0) and D=97, it is difficult to jump out of the loop if you click the Stepover button with the mouse. To make the program jump out of the while (d>0) loop, you can click the Stepout button with the mouse. At this time, the program jumps out of the loop to the x++ in the while (l) loop statement, which is the position indicated by the yellow arrow (not the first display (x), as shown in the figure below.
At this time, the program executes X++0+1=1. If you click the Step Into button with the mouse, the program runs to the next if statement, which determines whether X reaches 100 (the maximum value displayed by the digital tube LED is 99), as shown in the figure below.
If X++<100, and you click the Step Into button with the mouse, the program will immediately return to the place where the display function display(x) is called, as shown in the figure below.
At this time, still pay attention to the yellow arrow indicating that the program is running.
The above source program software simulation operations are all performed using the buttons in the figure below. In actual operation, you can also use the Debugger drop-down menu of MPLAB IDE. There are also commands corresponding to the buttons in the figure below from Run-Reset, but it is not very convenient to use!
(2) Quantitative observation of the changes in variable values in the source program: The quantitative observation here refers to two types of quantification: 1) variables, such as the ones and tens digits of the digital tube LED in PICl.c; 2) the time consumed by each command code or the time consumed by the program segment when the program is running.
Previous article:How to send data on PIC microcontroller
Next article:How to develop a PIC microcontroller
Recommended ReadingLatest update time:2024-11-16 13:01
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
- 【Qinheng Trial】Five final chapters--IoT system based on CH549
- What should you pay attention to when purchasing encryption chips?
- What exactly is op amp noise?
- [Hua Diao Experience] 11 Getting Started with ESP32C3
- 【GD32E503 Review】 Experiment on displaying Chinese characters and character strings
- I bought a Juyang JY-E2300 emulator on Xianyu, but it lacks an emulation head. Does anyone have an emulation head or schematic diagram for this emulator?
- This week's highlights
- 03. Light up the LED and test the buttons
- 7.4V battery charge management IC?
- Sensor question