Programming microcontrollers isn't hard. Building a programmer makes a great first electronics project. The goal of this instructable is to explain the simple 'in circuit serial programming' method used with Microchip PICs.
Step 1: Why ICSP?
Programming a big DIP (through hole) chip is easy. Pop it into a socketed programmer, burn, and return to the application circuit. Test and repeat.
Things get more difficult with smaller (surface mount) chips. There are no standard sockets for QFN, SSOP, QFP, or even the large SOIC .300 packages. There are really expensive ($100s) clips that can attach to, and program, these chips. A different clip is needed for each chip type and pin count you use.
There is an alternative. Its called ICSP.
ICSP means 'in circuit serial programmer(ing?)'. It is a way of programming a PIC while it is still attached to the application circuit. Thats right, no more chip swapping.
1. There are no programming sockets for small package chips. Clips are expensive.
2. Its a pain to move chips in & out of the programmer during development. Impossible for surface mount parts.
Step 2: What Is ICSP?
Five connections are needed to program a PIC while attached to an application circuit. I add a 5 pin header to my circuit boards to make this connection quick and easy.
The basics of PIC programming.
Five connections are required to program a PIC. Power, ground, a programming voltage, clock, and data.
+ (Vdd)/-(Vss) These are the power & ground connections (Vdd, Vss). Pretty standard. If you are using a programmer with 'real' voltage levels (NOT a JDM2!), your application can run from its own power supply when programmed, eliminating these connections.
Vpp This is the programming voltage. PICs enter programming mode when ~13 volts are placed on the MCLR/Vpp pin (usually pin 1 on modern PICs, more on that below).
Clock/Data or PGC/PGD The clock and data lines are used to write and read the PIC firmware. These are usually the same pins as PORTB6 & PORTB7.
Exercise:Identify the ICSP connection points on the PICs in the pictures below.
If the PIC fits, wear it.
I get a lot of questions about my JDM2 design on instructables. The most frequent is "Will it program PIC X?'"' - here is how you can tell:
1) Look at the data sheet. Find the 'Pin Diagram' that looks something like the picture below.
2) Identify the location of the pins that must be connected for programming (Vpp, Vdd, Vss, Data, & Clock).
3) Look at the socket connection on the programmer. Can you match the required pins with a socket on the programmer?
Step 3: How ICSP?
Depending on your design, you can now make all the required connections and program your PIC. There are a few catches that you should know about. Design for ICSP is important. Microchip provides a nice PDF application note about designing for ICSP.
Here are some tips and examples of ICSP designs from my previous instructables.
Tip #1, Threat level: Important
Don't connect anything else to the CLOCK and DATA pins (usually RB6 & RB7, PGC & PGD).
Just don't do it. There are situations where very clever engineering types get away with it, but don't do it. The components attached to the pins will skew the clock and data signals, resulting in unpredictable programming. Additionally, if you ever want to use an in-circuit serial debugger, you can't. Just don't do it.
Tip #2 Threat level: Explosive
Always use a diode between the programming voltage and system voltage.
If using MCLR (master clear) on a PIC you must provide some voltage on the MCLR pin through a 10Kish resistor. This is also the pin to which you will apply ~13 volts to enter programming mode. I put a 1n4148 (equivalent) diode between the resistor and MCLR/Vpp pin (as shown in the schematic and rendering below). This keeps the programming voltage on the Vpp pin, preventing destruction of other components on your board.
Tip #3 Threat level: (re)tired
Low voltage programming is holding you back, man.
I have never been successful with LVP. I have never seen it work (consistently) with my own eyes. Just bite the bullet and spend $2.50 to build a JDM2 programmer.
Step 4: ICSP Programmers
ICSP does not require a different programming protocol. A socket-based programmer already produces the required signaling, but sends it to the socket rather than through wires. Most socket programmers can be used as an ICSP by rigging wires from the socket to the application circuit.
For example, the original JDM2 programmer (here: http://www.jdm.homepage.dk/newpic.htm ) can be used to do ICSP programming by bringing the 5 required signals to a header. This can be seen in the programmer here: http://www.belza.cz/digital/jdm.htm . To keep things handy, I remade this design in Eagle Cad and attached it to this instructable. Watch the transistor orientation, one of the footprints may be incorrect (I made this more than a year ago, I don't remember anymore).
Similarly, my updated JDM2 programmer (here: https://www.instructables.com/id/EN28KZDDYVEP286GRI/ ) can be used for ICSP by sticking wires into the DIP sockets and connecting them to the target PIC.
*****JDM2s use funky voltages...connect it to the circuit WITHOUT external power (or even ground) connected to the application circuit. Remove the ICSP connections before applying power. Failure to do this is not destructive, but will result in failed programming*******
Another option is a (semi-)proper ICD programmer. ICD allows you to control firmware execution on your PIC by setting break points in the code or reading out memory and port values. An ICD can also animate the execution of code, power the application circuit, and program the PIC. It does all this through the same 5 pin ICSP connection we have discussed. Several ICSP clones that you can make yourself can be seen here: http://www.icd2clone.com/wiki/Main_Page . I built the PiCS (rev B) a few months ago and love it.