Debugging Microcontrollers in MPLAB: Step-by-Step Workflows

Debugging Microcontrollers in MPLAB: Step-by-Step Workflows

Debugging is essential for embedded development. This guide provides clear, prescriptive workflows for debugging PIC microcontrollers using MPLAB X IDE and MPLAB® XC compilers, covering setup, breakpoints, watch variables, hardware debugging with MPLAB ICD/REAL ICE, and common pitfalls.

1. Prepare your project and tools

  • Toolchain: Install MPLAB X IDE (latest stable) and the matching MPLAB XC compiler for your PIC family.
  • Hardware: Connect a hardware debugger/programmer (MPLAB ICD 4, PICkit 4, or REAL ICE) to the target board and your PC via USB.
  • Target board: Ensure power, ground, and MCLR/RESET lines are correct. If using external power, confirm debugger target voltage matches.
  • Project settings: In MPLAB X, open Project Properties → Conf: [default] → Hardware Tools and select your debugger. Set Compiler Toolchain correctly.

2. Build with debug symbols

  1. Project Properties → Conf → XC Compiler → Categories → Debugging: set Generate Debug Info (or select “Debug” build configuration).
  2. Clean and Build Project (right-click project → Clean and Build). Confirm no optimization removes variables you need (see next).

3. Configure compiler optimization for effective debugging

  • For step-through debugging, use low optimization: set Optimization Level to -O0 or -Og. Higher optimizations (-O1, -O2, -O3) may inline or eliminate variables, making breakpoints and watches behave unexpectedly.

4. Connect to the target and start a debug session

  1. Plug in debugger and power the target.
  2. In MPLAB X, set the project’s active configuration (Debug).
  3. Click the Debug Project (bug icon) or Run → Debug Project. MPLAB will program the device and open debug windows (Variables, Watches, Call Stack, Breakpoints).

5. Use breakpoints and run control

  • Set breakpoints: Click left margin in source or right-click → Toggle Breakpoint. Conditional breakpoints: right-click breakpoint → Properties → add condition (e.g., counter == 100).
  • Run controls: Use Continue (F5), Step Over (F8), Step Into (F7), Step Out (Shift+F8).
  • Breakpoint types: Software breakpoints (supported for debug builds) and hardware breakpoints (limited number depending on device). If you see “too many breakpoints,” remove unused ones.

6. Inspect variables and memory

  • Variables window: Shows local/global variables. If a variable shows optimized-out, rebuild with lower optimization.
  • Watches: Add expressions or variables to Watches panel to monitor values continuously.
  • Memory view: Window → Debugging → Memory Views → add address or symbol to view raw memory and peripheral registers.
  • SFRs and Peripherals: Use the Registers or Special Function Registers window to view peripheral registers in real time.

7. Use I/O and logic tools

  • Pin state: Use MPLAB I/O to monitor/force I/O pins (if supported by debugger). Be cautious forcing pins on powered circuits.
  • Data Visualizer: For supported devices, plot variables over time using the Data Visualizer plugin. Helps spot timing issues.

8. Trace and advanced debugging

  • Program Trace (if hardware supports): Enable instruction or data trace to record execution history. Useful for intermittent bugs and timing analysis. Configure trace buffer size in Project Properties → Debugger → Trace.
  • RTOS-aware debugging: If using an RTOS, enable thread-aware debugging or use plugins that show task lists and stack usage.
  • Performance profiling: Use cycle counters or trace to find hotspots.

9. Common debugging workflows (step-by-step)

Workflow A — GPIO not toggling:

  1. Confirm build with debug symbols and low optimization.
  2. Set breakpoint at main loop start. Start debug session.
  3. Step to the GPIO configuration code; inspect TRIS, LAT, ANSEL registers in SFRs.
  4. Verify external hardware (pull-ups, connections).
  5. Step through toggle code; watch LAT register and pin state.

Workflow B — Peripheral (e.g., UART) not communicating:

  1. Break at initialization; inspect baud rate generator registers and BRG calculation.
  2. Verify clock source and frequency (FOSC) value used in calculations.
  3. Step into transmit/receive routines; monitor TX/RX buffers and status flags.
  4. Use Logic Analyzer or Data Visualizer to check signal on physical TX/RX pins.

Workflow C — Intermittent crash or watchdog reset:

  1. Enable debug and trace if available.
  2. Add a breakpoint in error/exception vectors (ISR, reset handler) to catch resets.
  3. Inspect stack pointer, call stack, and return addresses.
  4. Check stack size and reduce deep recursion or increase stack allocation.
  5. If watchdog causes reset, temporarily disable WDT or add periodic clears to isolate issue.

10. Best practices and tips

  • Start simple: Reproduce bug with minimal code.
  • Use logging: For devices without full debug support, implement UART or SWO prints with minimal timing impact.
  • Version control: Keep working versions in VCS; tag debug builds.
  • Document assumptions: Clock rates, oscillator settings, and config bits—wrong config bits often cause mysterious behavior.
  • Power and grounding: Many bugs are hardware-level; check supply stability and decoupling caps.
  • Firmware safety: When forcing pins or memory, know hardware limits to avoid damage.

11. Troubleshooting common issues

  • MPLAB won’t connect: Confirm debugger firmware updated, USB drivers installed, correct power to target, and correct debug tool selected.
  • Breakpoints ignored: Ensure debug build, check optimization level, and confirm available hardware breakpoints.
  • Variables show garbage: Rebuild with -O0, confirm variable scope, and check for stack corruption.

12. Example: Minimal debug session (PIC16F example)

  1. Set project to Debug configuration, Optimization = -O0.
  2. Add breakpoint at UART init and main loop.
  3. Start Debug Project; step through init; verify SPBRG and TXSTA registers in SFR window.
  4. Add watch for txBuffer index; send test data; observe buffer changes and pin toggles.

Conclusion Follow these structured workflows to methodically find and fix firmware issues in MPLAB. Combine source-level debugging, register inspection, trace, and external measurement tools for the fastest resolution.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *