Lab 3 - Debugging on Hardware

 Objective

This lab steps through several procedures to teach the following:

  • How to select the appropriate debugger/programmer
  • How to download the firmware (including the debug executive) into a PIC® MCU
  • How debugging techniques on hardware compare to simulator debugging
  • How to directly control SFRs and alter program variables on a PIC MCU during operation
  • How to remove the debug executive from a project and create a customer deliverable HEX that runs without a debugger/programmer

 Materials

Hardware Tools

Tool About Purchase
Explorer16-50px.png
Explorer 16
Development Board
ICD3-50px.png
MPLAB® ICD 3
In-Circuit Debugger

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 Lab03

Close any open projects by right-clicking on the project name and selecting Close.

Click the Open Project Main_Open_Project.png icon.

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

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

Lab03.png

Notice the variable delayVal. This variable is defined with an initial value of decimal 10. This variable, delayVal, is used to control the execution time of the delay() function. We will alter this variable in the latter part of Lab03.

2

Verify the Hardware Tool

Verify the Hardware Tool assigned to this project is the REAL ICE/ICD3.

NewProteus2.png

Right-click on Lab03 from the project list.

Select Properties.

Under the Conf:[default] section ensure the REAL ICE / ICD3 option has been selected.

Click OK ok.png

3

Verify the debug session will start at the beginning of main()

HaltAtMain.png

Tools > Options > Embedded > Generic Settings > Debug startup > Halt at Main

4

Debug Project

Click the Debug Project Main_Debug_Project.png icon to build a debug image and download it to the REAL ICE / ICD3. You will notice the project builds and then programs the Explorer16 processor through the REAL ICE / ICD3.
Lab3Code.png

Above we see the editor window showing Lab03.c after simulation ready to continue at the beginning of main().

5

Set Breakpoints

Set breakpoints on the two LATA assignment statements within the while(1) loop.

Click on the left margin of lines of code containing LATA=0xAA; and LATA = 0x55;

TwoBreakpoints.png
Click the Continue Debug_Continue.png icon to run the program until the first breakpoint is encountered.

You should notice the green bar on the line with the breakpoint set.

GreenBarOnBreakpoint.png

Skid on breakpoint. When running on hardware, the PIC MCU may execute the instruction at the breakpoint and then stop. Under software simulation, the CPU halts before the instruction is executed. Stopping after executing the breakpoint instruction is referred to as a skid.

If the green bar is located below the red bar you are observing a skid. The red bar indicates where the breakpoint was set and the green bar is where the processor stopped. Depending on the particular version of MPLAB® X IDE and the debugger, you will need to take this skid into consideration when debugging on hardware.

6

Operation of Program

Click the Continue Debug_Continue.png icon to move the program to the next breakpoint. Each time the PIC MCU stops at a breakpoint, observe the LEDs on the Explorer16 change. Repeat this step several times until you are familiar with the operation of this program.

7

Remove Breakpoints from the Code

From the Window menu, select Debugging, then Breakpoints.

BreakpointsPath.png

Right-click in the Breakpoint window and select Delete All.

Breakpoints.png
Click the Continue Debug_Continue.png icon to run the project code on hardware at full speed.

Do you see the LEDs blink or do they appear to stay on constantly? To answer that question, let's look at the speed of the processor. The clock rate of the PIC MCU is such that the delay() function executes so quickly the human eye cannot easily notice the toggling of the LEDs. To adjust the speed we will manually adjust the variable delayVal using MPLAB X IDE.

Pause the program by clicking the Pause Debug_Pause.png icon.

8

Adding Variables to the Watches Window

Open the Watch window (if not already open) by selecting it from the Window menu. Watches from previous labs might show up (right-click and select Delete All).

WatchesPath.png

Within the Watch window right-click and select New Watch…

NewWatch.png

Highlight the Global Symbols radial button.

Select the variable delayVal from the menu.

Click OK ok.png
GlobalSymbols.png

If LATA is not already in the Watch window, repeat the above step to add this SFR to the list of data points to watch.

WatchWindow.png

You may notice delayVal is displayed with a value of 0x00AA and not 10 as written in the source code. The Watch window will default to display values in HEX while the value in the code was decimal. To synchronize the value in the Watch window with what radix we coded, we will change the display on the Watch window.

9

Changing the Radix of the Watches Window

Within the Watch window, right-click on delayVal.

Move the mouse over Display Value As and select Decimal.

Decimal.png

Highlight the value displayed for delayVal (in this case 10).

Change the number to 2048 then press Enter.

2048.png
Click Continue Debug_Continue.png and notice slower LED blinking. Pause the program by clicking Pause Debug_Pause.png

10

Saving Changes

To save the changes, we need to enter the new delayVal into the source code.

Change the line of coding reading delayVal = 10; to delayVal = 2048;.

SavingValues.png

Before we rebuild and then run the code, we will manually control the SFRs on the PIC MCU. Directly controlling the contents of SFRs can be very useful during the debugging process. For today’s lab, we will manually alter the LATA SFR to allow a different pattern to be displayed on the LEDs.

In the Watch window, enter "0x003C".

LataValue.png

Notice an immediate change in SFR value as displayed on the LEDs.

End the debug session Debug_Finish_Debugger_Session.png

11

Make and Program Device

Build and run a production version of the code by clicking the Make and Program Device Main_Program_Target_Project.png icon. This will remove the debug executive and build a version of the code which will run without prompting from a debugger. Please note this step will only work if you are using a programmer debugger such as the PICkitTM 3, MPLAB ICD3, or MPLAB REAL ICE.
© 2022 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.