Using the MPLAB debugger

From MARS Wiki

Jump to: navigation, search

Contents

Background

MPLAB comes with an integrated debugger which allows you to view the memory of certain PICs as the code runs. This is an invaluable tool for those times when your code doesn't work quite right and you want to see the internal memory space without inserting extra code into your program.

Using the debugger

Make sure your chip is supported

First, you should make sure the chip you intend to use supports debugging. In MPLAB, go to the Configure menu and choose Select Device. In the Device field, enter the device you wish to use and press Enter. In the debugger section of the window, you will see a green circle next to the name of any tool that is supported for that chip.

Write code

It's an ugly job, but someone has to do it. Make sure your code compiles, otherwise debugging won't help you at all.

Set up the hardware

Connect the programmer of your choice (PICKit2 or ICD2 in most cases) to the PIC using the correct connections. Reference the datasheet for the chip and/or programmer if you are not sure which signal goes where. Hooking this up incorrectly can DESTROY both the microcontroller and programmer, so be careful.

Set up the debugger

Under the Debugger menu, choose Select Tool. From the submenu choose the programming hardware you are using, which will probably be either the PICKit2 or the ICD2. Finally, choose Program from the Debugger menu. This will download the debugging code as well as your application code to the chip.

View registers

Under the View menu, choose Watch. From the rightmost drop-down menu, choose the a variable name you defined, then click the Add Symbol button to add it to the list.

Run your code

Once you program the chip, go to the Debugger menu and choose Run, Step, or Animate. When choosing Animate or Step, the Watch window updates after each line of code executes. When using the Run option, the Watch window updates each time you halt execution.

Breakpoints

Breakpoints make it such that your code will run normally (in debug mode, of course) until it reaches a certain point, at which time it will stop running and update the register displays in MPLAB. These are useful for the time when almost all your code works, but there is one section you need to execute at a lower speed. To set a breakpoint, right-click a line in the source code and choose Set Breakpoint, or simply double click on the line in question.

Troubleshooting

If you encounter problems, try making sure you have the newest versions of MPLAB and the firmware for the programmer you're using. Usually updating MPLAB will take care of updating the programmer firmware. Also, it never hurts to double check your connections to the programmer.

Personal tools