Building Your Electronic Gadgets

Serial Debugging for PIC Projects

Here is a quick tip for getting diagnostic information from your Microchip PIC project without having to add a MAX3232 or similar device to it.

A lot of my projects are simple and I like to keep the parts count and size as low as possible. I do, however, rely on getting text messages from my projects, particularly during the early stages to help me with debugging. I do this by connecting my PIC directly to a PC serial port and then display the debug output in a terminal program. Of course, you’d be hard-pressed to find a current PC or laptop with a real serial port so I use a USB to serial converter cable. You plug it into a USB port and the serial end usually has a DB-9 connector. On this connector I’m interested in Pin 5 for Ground, Pin 2 for Receiving data from the PIC, and, sometimes, pin 3 for Sending data to the PIC.

This photo shows a typical setup when I start a new project. The PCB is a Microchip Low Pin Count Demo Board. I’m using the PicKit2 programmer and a no-name-brand USB to serial converter:

Setup for Debugging PIC Projects

The yellow wire connects the transmit pin from the PIC to the Receive pin on the DB-9 connector. I rarely transmit information to my projects so I don’t connect the green wire which would send data from the PC to the PIC.

If you use the typical serial output commands such as puts() or printf() in the various C compilers or DEBUG or PRINT commands in some BASICs, and try to get that into the terminal program on your PC, you’ll get garbage because the polarity of the signals does not match. That’s what an inverter chip such as the MAX3232 fixes, but I’m trying to avoid that extra hardware.

Fortunately you can change the polarity of the serial signals coming from the PIC with some software commands.

Here is the solution for various compilers:

If you’re using the CCS PIC C Compiler then you can add the “INVERT” to your “#USE RS232” like so:

        #use rs232 (invert, baud=9600, xmit=PIN_A0)

Note that this will be a software UART instead of using the built-in hardware. The advantage of that is that you can use any I/O pin that supports digital output.

If you’re using the PICBASIC PRO compiler from microEngineering Labs, then set up your serial port by including the following command:

        DEFINE DEBUG_MODE 1 '1=inverted 0=true

If you’re using the Microchip C compilers, you’ll have to set some bits in the registers that control the UART. The names for these vary from part to part. Search for “transmit polarity” and “receive polarity” in the data sheet for the part you are using.

Happy Coding!

Leave a Reply

+ 3 = ten