Debugging a PIC16 with MPLAB X

debuggingpic

I am having problems with the MPLAB X debugger (although the release build works fine). It behaves very weirdly with my PIC16LF1937-based PICKIT3. Specifically:

  • It gets caught in random infinite loops
  • The step over function doesn't work most of the time
  • It only seems to debug the first few instructions
  • Setting a breakpoint on a late instruction will cause the PIC to freeze early

What could I be doing wrong? What things should I check?

Best Answer

I question the stability of your release build if you cannot debug it at all.

Building for debugging adds some extra meat to your HEX file, meaning things aren't always in the same RAM and flash locations they are in a production build. This has (for me) sometimes shown some software bugs that I didn't see in production mode (case in point - I think I found a bug in the XC16 string library where sscanf is throwing address errors when dealing with chars, which only showed up after I started debugging and a char moved to a different RAM location)

If you're debugging and you have WDT enabled, disable it - the WDT doesn't play well with the debugger freezing time by stopping the micro.

Before you add breakpoints and other stuff, simply run the program as-is in debug mode - no single steps, just execute. The code should just 'work'.

For detailed help, you'll need to post a minimum code sample that works fine in production mode but hangs up when debugging. (This exercise may show you some buggy code that you didn't realize you had.)