There are lots of cool hardware projects on the web. Many require you to program a microcontroller. Programming, or burning, happens when we copy software from a computer into the flash memory of a microchip. This is just like copying something to a USB flash drive, but it requires a special connection. Without the ability to burn firmware you can't build that awesome open source project -- and you can't develop your own.
Today we'll burn a PIC microcontroller from Microchip - in this case Microchip is a proper noun referring to this company
. "PICs" are the brains in tons of projects -- this USB color changing light
, or these analog gauges
, for example.
Check out the video to see different ways to program a PIC, and read on to build your own simple JDM2 style programmer.
Get all the files for this project in the project archive
Before we delve into the details of programming, lets take a look at the basic connections needed to get a PIC up and running.
Vdd/Vss (power and ground)
Vdd and Vss are the labels Microchip uses to designate the positive supply and ground. Vdd is generally a 5 volt positive supply. Other names for Vdd include: Vcc, power, supply, "+", the bumpy side of the battery, and the red wire. Vss is "ground", almost always 0 volts. You'll also see ground referred to as: negative, ground, "-", gnd, the flat side of the battery, and the black wire. Most circuit boards use a ground plane
, this means that all ground pins connect to the copper areas that remain after other wires have been routed. Ground planes are economical and environmentally friendly - copper must be chemically etched from any unfilled areas on a circuit board, this uses more acid etchants and creates a larger copper waste stream. Check out the RGB color changer circuit board to the right, the large blue area is a ground plane.
Don't forget to connect any pins marked Avdd and Avss
. These pins allow you to provide a clean supply to the analog portions of the chip. Read more about routing these connections
[pdf!] if you design a device that does super-duper delicate measurements with the analog to digital converter
Each pair of Vdd/Vss or Avdd/Avss pins get a 0.1uf capacitor (C1 in the diagram below). These are called "decoupling" capacitors because they isolate (decouple) the chip from noise in the power supply. This noise will cause wild oscillations in the circuit if left unchecked. 0.1uf capacitors should be dirt cheap, buy 100+ online to save a bundle. In practice, a capacitor should be put on every supply (Vdd) pin. If the supply and ground pins aren't next to each other, an annoying thing that happens all too often, connect one side of the capacitor as close to the supply pin as possible and the other to a common ground plane.
VPP/MCLR/master clear and reset
Spikes in the power supply can freeze up a PIC or cause erratic behavior. Most PICs have an MCLR function that causes a full reset when this happens. To use this function, put a 10K resistor (R1) between the power supply and the MCLR pin.
Minimum connection examples
This gallery shows how to make the bare minimum connections for several different PIC microcontrollers.
A PIC in a bare circuit is great, but we need to get some software into it before it's useful. I always add a simple 5 pin connection to the circuit board so that the chip can be programmed without removing it from the circuit. This saves a ton of time with socketed chips, but is absolutely required for surface mount chips that can't be removed from the circuit board. This method of programming is called In-Circuit-Serial-Programming (ICSP). Even if you plan to use a socket programmer, read on to understand what connections the programmer makes to the PIC when you put it in the socket.
We took care of these connections earlier. The programmer MUST share a ground connection to the circuit board, and a connection to the supply voltage is generally used as well.
The MCLR reset pin is also used to put the PIC in programming mode. When a programmer applies the "programming voltage" (Vpp) to this pin, the chip readies itself for new firmware to be copied.
Vpp varies among PIC models, but can be as high as 13 volts. If we route this pin to a header and connect a programmer, the programming voltage will go through the MCLR resistor (R1) and enter the rest of the circuit. Everything on the circuit board could be ruined by 13 volts -- a one-way valve is needed to keep the high Vpp voltage where it belongs. A small signal diode (D1) does exactly that. I usually use the common 1n4148 for this purpose.
Clock and Data
Code flows into the PIC on the clock (PGC) and data (PGD) pins. These are usually found on PORTB, pins 6 and 7, on PIC microcontrollers.
You probably already know that everything on a computer boils down to 0s and 1s. Code files for a PIC, usually with the .hex extension, are represented by 0s and 1s on a disk. The PIC data pin is switched between supply (1) and ground (0) to represent these bits. The clock line is raised high and then dropped low again when the proper value is on the data pin. The clock pulse triggers the PIC to copy the value on the data pin (0 or 1) into memory.
Put it together with VPP for a very simple look at how a PIC is programmed. First, the programming voltage is raised (VPP) to put the PIC in programming mode. Next, the data line is changed to reflect the value 0 or 1. A clock pulse is applied to copy the data pin value into the PIC. When all the data has been copied, the VPP is removed.
ICSP connection examples
This gallery shows the ICSP connections for several PIC microcontrollers.
A programmer is the key to getting software into a PIC. This device physically connects your PC to a microcontoller. They vary greatly in price and complexity.
Pin wigglers are the simplest programmers - they do little more than help delicate PC outputs switch the VPP, data, and clock voltages. These simple circuits usually "buffer" the serial or parallel port pins. PC software wiggles pins to program the PIC. This is a pretty versatile concept - software controls everything. Port wigglers are cheap, use few parts, and are easy to make.
Some examples of pin wigglers are the NOPPP
and the famous JDM2
. Several applications support these programmers. I like WinPIC800
because it's updated for the latest 18F PIC chips. You can also check out the older WinPIC
Intelligent programmers are more expensive than pin wigglers. These programmers have their own microcontroller that actually programs the target chip. Microchip's own USB programmers
(or the Olimex clones
) are an example of this type of programmer. Microcontroller experiment kits from electronics stores often come with this type of programmer - kit programmer usually only program a very narrow range of PICs.
These programmers are designed to work with specific programming software, and aren't usually supported by generic programs like WINPIC800. The programmers mentioned earlier work with the free MPLAB development application
makes custom programmers
that work with their own C, Pascal, and Basic compilers
Debuggers program, but they also add play and pause functions to the program code. This helps software authors hunt down annoying problems during firmware development. Like intelligent programmers, debuggers work with specific applications. Microchip's ICD2
and the exact copy
at Olimex work with Microchip's MPLAB development environment and compilers. There are a few USB and serial ICD2-alikes
that you can build yourself. These aren't clones because they lack some functionality of the originals, such as adjustable voltage levels.
Building the JDM2
In this project we'll make a programmer based on the well-worn JDM2 pin wiggler design. It uses a not so simple circuit
to generate a 13 v programming voltage from a PC serial port. The original design is by Jens Dyekjaer Madsen
Download the circuit board, and other files for this project, here
. The circuit board was designed using Cadsoft Eagle. You can download a free version
The JDM2 design has a few limitations:
- It uses a negative ground voltage to get the full +13 volts separation between ground and VPP. Make sure you disconnect any power source from the circuit before programming with a JDM2.
- It won't work with most laptops because the serial port voltage is too low.
- It probably won't work with a USB to serial adapter for the same reason.
- The PIC 16F line requires 13 volts on Vpp, newer 18F PICs use 12.5 volts, and the newest PICs (24F/32F) use 6 volts or less. If you want to program a newer 18F PIC with a JDM2, you should use a diode (D1) as shown in the diagram. This diode will reduce the programming voltage by about .6 volts, leaving a nearly perfect 12.4 volts.
R1 - 10K ohm resistor
R2 - 1.5K ohm resistor
Simple 1/4 or 1/8th watt resistors are fine.
D1 - 8.2 volt zener diode (should be rated at least 0.5 watts)
D2 - 5.1 volt zener diode (should be rated at least 0.5 watts)
D3,4,5,6 - 1n4148 diode (or 1n4448)
C1 - 100uf/25v electrolytic
C2 - 22uF/16 volt Tantalum or 47uf/6.3 volts Tantalum
This is a high speed capacitor and should be a tantalum type.
Q1,2 - BC547B
These small transistors are really common. Make sure that your transistor has the pin configuration shown in the diagram below. Double check the first page of the manufacturer's datasheet to be sure -- incorrect orientation is the most common problem with new JDM2 programmers.
Serial port plug
This is a common female, right angle, PCB mountable DB9 plug.
I assemble circuit boards from the bottom up, starting with the shortest parts and working my way up.
- First, solder the two jumper wires.
- Next, add the 1.5 K resistor and the 10K resistor. Resistor orientation doesn't matter.
- Now add the 1n4148 diodes. The black bands should oriented as shown in the picture.
- Add the 5.1 volt and 8.2 volt zener diodes. The black band should face the serial port plug.
- Add the transistors. Make sure the transistors are orientated as shown in the pictures and diagrams. Bad transistor placement accounts for most problems with new JDM2 programmers.
- Add the tantalum capacitor and the electrolytic capacitor. Orientation DOES matter. Make sure the black stripe of the electrolytic capacitor faces the edge of the board and the short leg connects to the large copper fill area (the ground plane). Similarly, the short leg of the tantalum capacitor should connect to the ground plane.
- Add the serial port plug.
- Add the header of your choice (male/female pin header, wire, etc).
The gallery has images that show each step in full detail.
Setup Winpic800 and test the JDM2
To test our programmer we'll use Winpic800. Download it here
and install it. It will probably be in Spanish the first time you start, select Idioma->English
(or whatever suits you).
Burn firmware to a PIC
- Navigate to Settings->Hardware.
- Setup Winpic800 for the JDM2 programmer: click on "JDM2 Programmer" in the list, and make sure the COM port is set correctly. Most PCs have a single serial port assigned COM1.
- Click Apply edits.
- Connect the JDM2 programmer to the serial port.
- Navigate to Device->Hardware Test. If your programmer is working, you'll get the message "Hardware OK".
Give me more!
- Connect a PIC to the programmer as described earlier.
- Connect the programmer to the serial port of a (junk) PC.
- Start Winpic800.
- Navigate to Device->Check Device (CTRL+D).
- Winpic800 will detect the connected PIC, click "Accept".
- Navigate to File->Open, and choose the firmware file you want to copy to the PIC. This is usually a .hex file.
- Finally, choose Device->Program All to program the PIC. Winpic800 programs the PIC and verifies the contents.
The JDM2 programmer is a great design because it can be made for just a few dollars. It's a nice way to see if microcontroller programing is the thing for you. If you love it, you'll quickly run into limitations imposed by the simple, out-dated JDM2 design. If you get to this point, you might want to try out one of these more advanced programmers or debuggers:
USB PIC programmer
Serial port debugger
Several ICD2 USB debugger copies
The "Real Deal" ICD2
Olimex programmer and debugger clones
Many have had success building the JDM2 on a bread board or a piece of perf-board, but nothing beats a PCB for convenience. The small PCB included with this project is an easy to make single sided design. Need help making a PCB? No problem, check out some of these tutorials:
Toner transfer PCBs
Order professional PCBs, online
Now that you've got a programmer, you can burn other people's software -- but you can also develop your own. Download some demo compilers and try the examples:
Mikro- Basic, C, and Pascal compilers
for PIC 16 & 18 series.
with free assembly linker for all PICs.
demo C compiler for PIC 18 series.
Other great PIC resources:
Microchip PIC Forum
The MIT PIC-list
mailing list and archive