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 https://microchip-dev.wikidot.com/tls0101:lab2 for the updated version of this page.
Objective
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)
Materials
Hardware Tools (Optional)
Tool | About | Purchase |
---|---|---|
| | |
| |
Software Tools
Tool | About | Installers |
Installation
Instructions |
||
---|---|---|---|---|---|
Windows | Linux | Mac OSX | |||
MPLAB® X
Integrated Development Environment |
| | | | |
MPLAB® XC16
C Compiler |
| | | | |
Exercise Files
The contents of the following zip file need to be placed in this directory:
C:\MTT\TLS0101
File | Download |
Installation
Instructions |
||
---|---|---|---|---|
Windows | Linux | Mac OSX | ||
Project and Source Files
|
| | | |
Procedure
1
Open Lab02 Project
Close any previously opened projects by right clicking on the project name and selecting Close
Click the Open Project icon
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.
2
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
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().
4
Starting the Debug Session
Build a debug version of the open project by clicking on the Debug Project icon
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
5
Step Into
Click “Step Into”
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

Place the cursor over LATA and observe the new value
6
Breakpoints
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.

7
Step Over
Click “Step Into”
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
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
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).
8
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
9
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
Repeat steps above to add the variable ‘ctr’ from the list of “Global Symbols”
Click the Continue
Click the Reset

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