Lab 2 - Using the Simulator (rev. 3.00)

NOTE: Microchip updates its tools regularly. This page is an older version that we have preserved for the convenience of those who are supporting existing designs based upon older versions of our tools. Please check for the updated version of this page.


Lab 2 picks up where Lab 1 ends. Now that we can create a project, enter code, and build the project, we need to verify the code works before we run it on hardware. To verify the code we will use the MPLAB® X simulator. This lab reviews how to build a debug version of a project and send it to the MPLAB® X simulator. Once the simulation session has started, this lab reviews how to control the program execution. This lab provides the basics of observing the program to ensure the correct lines of code are executed. This lab also reviews how to monitor and control program variables and the PIC® Special Function Registers (SFRs)


Hardware Tools (Optional)

Tool About Purchase
Explorer 16
Development Board
In-Circuit Debugger

Software Tools

Tool About Installers
Windows Linux Mac OSX
Integrated Development Environment
C Compiler

Exercise Files

The contents of the following zip file need to be placed in this directory:

File Download
Windows Linux Mac OSX
Project and Source Files



Open Lab02 Project

Close any previously opened projects by right clicking on the project name and selecting Close

Click the Open Project icon Main_Open_Project.png

Browse to C:\MTT\TLS0101\Lab02.X and select Open Project

Double click on Lab02.c under Source Files to open the C file in the editor window


Lab2.c toggles eight I/O pins which are connected to LEDs. Observe the variable definitions of ctr and delayVal. The variable ctr is incremented each time the main loop executes. The variable delayVal is used to control the amount of time in the delay routine. Altering delayVal affects the amount of time consumed by the delay function. These two variables are used during the debugging session.


Select Simulator as the Hardware Tool

Select Lab02 from the project list

Right click

Select “Properties

Click Conf:[default]

Under “Hardware Tools”, verify that Simulator is selected

Click OK ok.png

The MPLAB® X Simulator is a software simulator even though it's listed under the “Hardware Tools”. As you will learn later in this lab, the simulator has most of the functionality of a hardware debugger.

After building the debug version of a project, MPLAB X IDE will launch a debug session and begin a simulation. This auto-start feature can save time, however, the user may not wish for the debug session to proceed into the main program until directed to do so. To prevent the debug session from automatically running after we build, we will set an option in MPLAB X IDE requesting the debug session halt at the beginning main().


Setting the Simulator to start at the beginning of main() function


From the “Tools” pull down menu select “Options”

Select the Embedded icon

Select the “Generic Settings” tab

Ensure the “Debug startup” is set to ‘Halt at Main’

Click OK ok.png


Starting the Debug Session

Build a debug version of the open project by clicking on the Debug Project icon Main_Debug_Project.png

You will notice that clicking the “Debug Project” icon will build the project, download it to the simulator, start the simulator session, and run until the program enters the main() function.


Observe the green code highlight on the first line within main(); The simulation has started but stopped at this place in the code. The green line indicates the current position of the PC during a debug session.

Notice the addition of the Debug Toolbar



Step Into

Click “Step Into” Debug_Step_Into.png icon to execute one line of source code. Notice the PC increment as the green line goes to the next line in code.

We could continue to step through the program one line at a time to get to a place of interest but this may be time consuming.

Place the cursor on the line of code containing the instruction LATA=0x55;

Right click and select Run to Cursor


Place the cursor over the LATA to observe the that the value of LATA is all zero


The simulator breaks BEFORE the code is executed. At this time we have not yet loaded 0x55 into the output register for PORTA

Click Step Into Debug_Step_Into.png to load 0x55 into LATA

Place the cursor over LATA and observe the new value



To set a breakpoint, click the mouse on the number in the left column of the source window corresponding to the code LATA = 0xAA; The red line indicates a break point has been set and the simulation will stop on this line of code.


To get out of delay() we will set a break point in the main function and run until that breakpoint is reached.

Click the Continue Debug_Continue.png icon and notice the simulation stop at the breakpoint.


Step Over

Click “Step Into” Debug_Step_Into.png once so the PC is pointing to the call to the function delay()

We would like to execute the delay() function and halt on the line containing ctr++;. We do not wish to step through delay() instruction by instruction. We also do not wish to set a breakpoint in the main code after the call to delay().

To execute a function and stop the program after the function has executed select the Step Over Debug_Step_Over.png icon

Notice the simulation completes the delay() function then pauses when it returns


Set a breakpoint each of the lines of code containing the LATA assignment statements

Click the Continue Debug_Continue.png icon several times, noticing the simulation progress through the breakpoints

Verifying the accuracy of the data

The program executes the code in the expected order, but does the program do the correct thing? In order to determine if the code is making the correct decisions we have to observe what information the PIC® MCU sees and the data it outputs. To do this we will set up a Watch Window to observe program variables and some of the PIC® MCU Special Function Registers (SFRs).


Setting up the Watches Window


From the Window menu select Debugging ,then Watches

It can be useful to debug program flow before determining accuracy of the data being processed


MPLAB® X IDE will display the Watch Window

Are there any variables in the Watch window when you opened it up? MPLAB® X IDE keeps a history of the last Watches used. Keeping this persistent data can speed up debug session by not requiring the programmer to reconfigure watches. Today we are going to delete everything in the Watch window before we start. To erase the Watch Window right click the mouse in the Watch Window and select Delete All


Adding an Item to the Watches Window

Right click in the Watches Window and select New Watch

Highlight the radio button “SFR’s

Select LATA from the scrolling menu of Special Function Registers (SFR) on the PIC24FJ128GA010.

Click OK ok.png

Repeat steps above to add the variable ‘ctr’ from the list of “Global Symbols”

Click the Continue Debug_Continue.png icon several times to observe the changes in the value of LATA and ctr. LATA should change after each click of Continue. The variable ctr should change every other time Continue is clicked.
Click the Reset Debug_Reset.gif icon to reset the processor

Observe that the Watches Window entries revert to 0 and the program counter is set to the beginning of main();


Remove all break points from the source code by clicking once on each pink breakpoint box in the margins of the source code.


Running the Simulation

Click the Continue Debug_Continue.png icon to start the simulation. Wait 5 to 10 seconds, then click the Pause Debug_Pause.png icon.

Observe how the variable ctr has been incrementing during this simulation.


Ending the Debug Session

Congratulations! You have finished Lab 2 and learned how to build a debug version for the simulator, set breakpoints, add SFRs and variables to the Watches Window and control program flow.

The last remaining item in this lab is to inform MPLAB® X IDE to end the debug session. To accomplish this task click the Finish Debugger Session Debug_Finish_Debugger_Session.png icon
© 2024 Microchip Technology, Inc.
Notice: ARM and Cortex are the registered trademarks of ARM Limited in the EU and other countries.
Information contained on this site regarding device applications and the like is provided only for your convenience and may be superseded by updates. It is your responsibility to ensure that your application meets with your specifications. MICROCHIP MAKES NO REPRESENTATIONS OR WARRANTIES OF ANY KIND WHETHER EXPRESS OR IMPLIED, WRITTEN OR ORAL, STATUTORY OR OTHERWISE, RELATED TO THE INFORMATION, INCLUDING BUT NOT LIMITED TO ITS CONDITION, QUALITY, PERFORMANCE, MERCHANTABILITY OR FITNESS FOR PURPOSE. Microchip disclaims all liability arising from this information and its use. Use of Microchip devices in life support and/or safety applications is entirely at the buyer's risk, and the buyer agrees to defend, indemnify and hold harmless Microchip from any and all damages, claims, suits, or expenses resulting from such use. No licenses are conveyed, implicitly or otherwise, under any Microchip intellectual property rights.