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|
| Windows|| Linux|| Mac OSX|
Integrated Development Environment
The contents of the following zip file need to be placed in this directory:
| 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 CloseClick 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.
Select Simulator as the Hardware Tool
Select Lab02 from the project list
Under “Hardware Tools”, verify that Simulator is selectedClick 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().
Starting the Debug SessionBuild 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
Step IntoClick “Step Into” 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
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.
Step OverClick “Step Into” 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 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
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
Repeat steps above to add the variable ‘ctr’ from the list of “Global Symbols”Click the Continue 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 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.
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 icon