Simulation Design of PIC Microcontroller Source Program

Publisher:自由思想Latest update time:2019-12-04 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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.

Simulation Design of PIC Microcontroller Source Program

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.

Simulation Design of PIC Microcontroller Source Program

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.

Simulation Design of PIC Microcontroller Source Program

Simulation Design of PIC Microcontroller Source Program

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.

Simulation Design of PIC Microcontroller Source Program

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.

Simulation Design of PIC Microcontroller Source Program

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.

Simulation Design of PIC Microcontroller Source Program

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.

Simulation Design of PIC Microcontroller Source Program

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.

Simulation Design of PIC Microcontroller Source Program

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.

Simulation Design of PIC Microcontroller Source Program

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.

Simulation Design of PIC Microcontroller Source Program

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!

Simulation Design of PIC Microcontroller Source Program

(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.

[1] [2]
Reference address:Simulation Design of PIC Microcontroller Source Program

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

Information and source code instructions for making an alarm using a microcontroller
1. Experimental tasks Use the AT89S51 microcontroller to generate the "beep, beep,..." alarm sound and output it from the P1.0 port. The generation frequency is 1KHz. According to the above figure, it can be seen that the 1KHZ square wave is output from P1.0 for 0.2 seconds, and then the power is output from P1.0 for
[Microcontroller]
Information and source code instructions for making an alarm using a microcontroller
AT89C2051 drive stepper motor source code
program: stepper.c  stepper.hex  /*  * STEPPER.C  * sweeping stepper's rotor cw and cww 400 steps  * Copyright (c) 1999 by W.Sirichote  */  #i nclude c:mc5151io.h /* include i/o header file */  #i nclude c:mc5151reg.h  register unsigned char j,flag1,temp;  register unsigned int cw_n,ccw_n;  unsigned char step ={0x80
[Microcontroller]
Analysis and arrangement of Zhilin STM32 program source code 01
1. Purpose 1. A few days ago, I wrote a program for keyboard scanning, character input and simple shell on the development board. The program was modified based on the previous project. The source code was organized in a mess and there were no good comments. In the past two days, I organized the code and added more de
[Microcontroller]
Use AT89S51 microcontroller to create infrared remote control source code
The main content of this document details the information and source code of using the AT89S51 microcontroller to make an infrared remote control. Generally, the output of an infrared TV remote control is generated by pulse amplitude modulation of a 38-40kHz square wave using encoded serial data. When the transmit
[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号