This article was originally published on Digital-DIY and is an original work by Jon Chandler. It is licensed under a Creative Commons 3.0 BY SA agreement.
There are a few common problems that lead to unreliable PIC operation. Base on my experiences and other people posting on the forums, here are several commandments that must be followed.
I've also added some comments about practices that I have found to work well for me. These are not absolutes, but can smooth the development path.
If having problems with example and tutorial code, also check here for some guidelines.
Multiple pins are not put there for your convenience. They must all be connected for the chip to work properly. In the picture below, the VDD pins 11 and 32 must be connected to V+ and VSS pins 12 and 31 connected to ground.
2. Bypass caps of 0.1 micro-farad are to be installed across VDD and VSS as close to the chip as possible.
Bypass caps must be used regardless of any other capacitors in the power supply circuit. These may appear insignificant compared to large filter capacitors or capacitors used for the voltage regulator, but they must be used.
A floating /MCLR pin may lead to intermittent operation, if the chip operates at all.
Many port pins can have multiple functions depending on how the PIC is configured. When using these pins, ensure that the desired function is enabled. Most notable are pins with analog functions, which often default to the analog state. When planning to use these pins for digital functions, such as driving an LED or reading a switch, the digital function must be specified.
5. If using a development board, verify the purpose and connection of jumpers and accessories on the board, and understand the effect these may have on your circuit.
For example, if the development board has a pot connected to one of the analog inputs, your sensor input will be inaccurate or not seen at all. Digital inputs may never change if the pot is rotated all the way to one end.
Trivial and silly, yes. It verifies that the power supply is working, the chip is running, and that the programmer can actually program the chip. If the LED flash rate is set to 1 second, it's also easy to verify that the clock is operating at the right frequency.
Some of these may go against the main-stream thought, but they work for me.
It's hard to beat the simplicity and reliability of an external crystal, with 22 pico-farad capacitors on each leg to ground. Internal oscillators may be used but this adds complexity to the code and there may be issues with start-up delays.
Removing the chip each time it's programmed is a waste of time. Boot loaders can work well, but ICSP is by far the easiest way to program a chip. Using Microchip's PICkit 2 is the easiest and nearly the cheapest way to implement ICSP. The logic analyzer and UART tool are additional benefits of the PICkit 2.
When using ICSP, it's best to keep the PRGC and PRGD pins dedicated to programming. These pins may be used for other purposes if the design doesn't load the pins excessively. See the Microchip documentation on ICSP for design ideas.
There are several designs for programmers that use a serial port or parallel port but these ports are rare on new computers, and available USB adapters don't always work.
Even if a UART output is not to be part of the final project, setting up and testing a UART output can be a good idea. Since the PICkit 2 makes it simple to monitor a UART output, adding the code at the start can aid troubleshooting later. A message that says "subroutine 2, x = 5" is far easier to understand than an LED blinking on and off.
Having the necessary reference material handy makes programming easier.
When things don't work as planned, a methodical approach is needed to find and correct the problem. Beware the problem that suddenly resolves itself for no apparent reason - it may re-appear at any time.
Some voltage regulators require a surprisingly large overhead voltage. Make sure the chip has a solid supply voltage. If a voltage regulator is used on the board, follow the data sheet recommendations for input and output capacitors. These are separate and distinct from the bypass capacitors at the micro-controller.
If the blinking LED program does not work, check the connections. If the power supply polarity is reversed, the chip is probably dead. If the chip will not program, erase it, blank check it and try again. Check the ISCP connections if it still fails.
If the LED does not flash, first check that the LED is connected to the correct port pin. Also check the polarity of the LED.
No output: Verify the RxD and TxD directions carefully. The methods of identifying inputs and outputs varies and can be confusing.
Garbage Output: The sender and receiver must be set to the same baud rate, parity and number of stop bits. Any mismatch will result in garbage. Note: if a software UART is used, the default state in Swordfish Basic is inverted. This must be changed to communicate to the PICkit UART tool. Finally, verify the system clock speed is correct by checking the LED flash pattern as discussed above. If the clock speed is wrong, the baudrate will be wrong.
If example or tutorial code isn't working properly, check here for guidance.
There are a few common problems that cause many PIC circuits to fail. A circuit may function even if these commandments aren't followed but its operation may be flaky or cease for no apparent reason. Following these guidelines helps make a reliable circuit that will function consistently.