Debugging C on a PIC

From Mech
Revision as of 22:05, 25 December 2007 by LIMS (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

The ICD can be used just as a programmer, to transfer compiled code to the PIC and start it running. The ICD can also be used as an interactive debugger, to monitor code execution, insert breakpoints, and let you probe variable values.

To use it in debugger mode you can insert the statement #DEVICE ICD=TRUE at the top of your program, or, just recompile using the Debug menu item. Either way, some debugging and communication code is added, which you will want to remove later to save space and time.

The debugger is wonderful, especially in its ability to let you set breakpoints and probe variable values. However for an embedded computer working with real-time hardware, a lot of problems can't be investigated except in actual operation at full speed. So, besides the debugger, here are some other ways to debug a running program on a PIC:

  • Illuminate LEDs in your program, to monitor program flow or variable values.
  • For fast signals, drive an output pin high/low, and monitor with an oscilloscope.
  • Set up RS232 communication, so your program can use printf statements to write to a 2-line LCD device, or to a serial port window on your laptop. PIC C has no scanf function, so it can't read numbers that you type in a text window. It does has functions for reading characters and strings.
    • tip - don't try to debug a whole program at once. Get each part working individually. Write small test programs if necessary to help you get each part working.
    • warning - a variable defined outside the scope of main() or any function is global, and is used in all of them. If you define a commonly used variable such as i as a global, your functions may inadvertently overwrite it. Very mysterious behavior results (e.g. loops never finish.)
    • warning - because many variables will be int8s, you need to be especially aware of how type conversion is handled. For example int16value = int8value100 + int8value200 will give a result of 44.
    • help - read the PIC C book. Search the CCS forum.