There are a few common problems that lead to unreliable PIC operation. Based 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.
1. All VSS and VDD pins on the chip must be connected.
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.
3. /MCLR must be pulled to VDD with a 10k resistor or explicitly disabled in code as Graham has explained.
A floating /MCLR pin may lead to intermittent operation, if the chip operates at all.
4. Ensure that multiplexed port pins are correctly set up.
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.
6. The first programming step is a blinking LED program.
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.
I had the mistaken idea that this went without saying but I've been proven wrong:
Once the oscillator configuration has been tested and the clock rate verified, do not change the oscillator configuration. If the osciallor configuration is changed, the test must be repeated.
Some of these may go against the main-stream thought, but they work for me.
1. Use a crystal oscillator.
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.
2. Use ICSP and the PICkit 2.
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.
3. A good starting point for many programs is a UART output.
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.
4. Keep data sheets, schematics and programming documentation close at hand.
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.
1. Check the power supply at the chip.
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.
2. Check Connections.
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.
3. UART output:
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.
4. Example and Tutorial Code
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.