First contact with ATmega8 microcontroller - part 2
Friday, October 26, 2007 3:00:00 PM
Go to: part 1, part 2, part 2.1 (video), part 3, part 4 (video).
What is an AVR ISP Programmer?
The ATmega8 (as well as many other microcontrollers) has a feature called ISP (In-System Programming), which means they can be programmed while still connected to the final circuit. You do this by sending electric signals to the microcontroller, there is no need for UV light, like many chips from the last century. In other words, all this just means it is very easy to program the microcontroller.
By programming the microncontroller I mean writing a new firmware on it. And the firmware is the software you are going to write to be run inside the microcontroller.
Available AVR Programmers
There are many types of AVR Programmers available for sale everywhere. Most of them are somewhat expensive. Some of them use the parallel port, others use the serial port and others use a USB port. Some may require external power supply.
At Cerne-Tec (the place where I bought the ATmega8), they sell a programmer for R$ 89.90 (about US$ 50.50), which is nine times the price of the microcontroller itself! I really don't know what kind of special features it has, but I won't spend so much money if I can build a working one for R$ 8.80 (maybe even less, if you happen to have some components). For DIY (do it yourself) projects, it's probably more than enough.
How AVR Programming works
The ATmega8 (and probably other AVR microcontrollers too) has two methods of programming: parallel programming and serial programming. The former uses 17 pins (plus 3 for Vcc and GND) to send all data and control commands in parallel. The latter uses just 4 pins (plus 3 for Vcc and GND) and send commands and data in serial. This means that serial programming requires less wires and may use a smaller connector. In addition, serial programming looks more simple than parallel programming (5 pages versus 11 pages at ATmega8 datasheet).
The serial programming uses almost the same pins as the SPI (Serial Peripheral Interface) - the SS (Slave Select) pin is not used. It works by sending a RESET signal at the RESET pin, which will... huh... reset the microcontroller and enter the serial programming mode. Then, the commands are sent at the MOSI (Master Output, Slave Input) line and the clock signal is sent at the SCK (Serial Clock) line. Data sent from the microcontroller back to the programmer (or to the computer) goes through the MISO (Master Input, Slave Output) line. And that's all you need to know! (refer to Serial Programming section at ATmega8 datasheet, pages 237-241, for details)
The AVR ISP Parallel Port Programmer
You must be asking to yourself: Why did this guy describe the serial programming method if we are going to use the parallel port? The answer is simple: we are going to use the PC parallel port to transfer serial data to the microcontroller serial pins. This works very well because the complicated (well, not so complicated) logic of sending the required bits at the exact time and the exact order is done by software. The parallel port will just receive some bytes (which are just a couple of bits together) and set the respective pins high or low, which will be directly (except by resistors) connected to the microcontroller pins. This means the parallel port programmer dongle is the cheapest programming hardware you can build: it has just the connectors, the wires and some resistors.
While searching the web for instructions about how to build it, I found many sites with different ways of connecting the parallel port pins. This made me confused about which one I should try to build, and which one would work. After looking at /etc/avrdude.conf file, I understood that all of them (or at least most of them) will work, even though they are different. You just need to select the correct programmer type in your programmer software (be it avrdude, uisp or even PonyProg).
I found basically 3 different schematics almost everywhere. Unfortunately, not all of them looked similar at first sight, and also not all of them had a name describing what type of parallel port programmer it was. Fortunately for you, I'm going to list here these three schematics with their respective names. (See how kind I am? )
The schematic diagrams below were drawn by me using Inkscape. Click on the image for a high-resolution PNG file. You can also get the original SVG file using the link just below the image. I release these images as public domain, but I would love to be credited if you use them . Note: these circuits are not my creation.
The first one is called bsd, probably because it was originally available at AVRPROG program for FreeBSD. (Well, I'm not sure if this is true, but it's the best explanation I have to give you.) This program was later renamed to AVRDUDE to avoid confusion with Atmel's AVRPROG.EXE. You can find a description of this parallel port programmer at AVRDUDE old homepage.
The second one is called dapa, which means Direct AVR Parallel Access cable. This one is described at uisp-parport-connect.txt file from uisp. It is also found at Arduino's ParallelProgrammer page and at LinuxFocus 352 article.
The third one is called stk200, because it behaves like the STK200 AVR Starter Kit. It is the schematic used by How To...Build A DIY USB Joystick project and available at a personal homepage address found at #avr channel topic on FreeNode (direct link to the image).
Other similar designs
While writing this post I also found another design. It's the one used by SpokePOV project. There is a kit available for sale. It works like the DT006 AVR Development Board (the little rAVeR programmer) and is recognized by avrdude and uisp as dt006. Sorry, I don't have the schematic diagram for this one, but don't you think that 3 different schematics are more than enough?
Notes about the parallel port dongles
The first thing to be noted is the absence of a Vcc line. This happens because the parallel port can't supply enough power. This implies that the microcontroller must be powered by something else (like the USB port or batteries) while using these dongles.
You can also see I've connected 8 pins to the GND wire. I did that because the parallel port has 8 GND pins, but you are not required to solder all of them together. You can use just one GND pin and it shoud be enough.
Another thing to be noted are the resistors. I've not specified their values because each site used different values (and sometimes the same site used different values). The stk200 schematic from How To...Build A DIY USB Joystick (direct link to the image) uses 4 resistors of 330 ohms each, one for each line (of course, GND is not included). On the other hand, this personal homepage tells us to use 4 x 220 ohms, but also hosts an image that shows one 100 ohms resistor for the MISO line and 3 x 330 ohms for the other lines.
Both Arduino's ParallelProgrammer page and LinuxFocus 352 article use the dapa schematic and both use the same values for resistors: 220 ohms for the MISO line, 470 ohms for the SCK and the MOSI lines, no resistor for the RESET line. In addition, the LinuxFocus 352 article says the cable should not be longer than 70 cm.
The (old) AVRDUDE homepage does not even give many details about this: "Be sure and include series resistors between the signal wires and the parallel port. 1K resistors should work fine and may save your parallel port from damage in case of mis-wiring or some other mishap."
Finally, the dongle used in SpokePOV project, which works as dt006, uses 3 x 1K ohms and one 47 ohms.
My conclusion about this: put some resistors and avoid too long cables. I've used a 220 ohms resistor for the MISO line and 3 x 330 ohms resistors for the other lines. My cable is 1 meter long.
If you run into problems, read this post at AVR Freaks forum and try shortening the cable length, or try using one of the other (a little more expensive) programmers listed below.
My parallel port AVR programmer
From all possible dongles above (and below), you just have to choose one to build. All of above are very similar to each other, and all of them should work on both avrdude and uisp (and both programs are available on multiple platforms).
I've choosen to build the bsd one. I got a flat cable with 14 wires, but since I needed only 6 (actually, only 5), I've split the cable. To make sure the wires wouldn't short-circuit inside the DB-25 case and to make everything inside the case less likely to break, I've applied an amount of hot glue after I finished with the soldering. If you look at the photos, the dongle looks like a bit messy and ugly, but since that hot-glued part will be hidden inside the DB-25 case, no problem.
I've not used any standard AVR connector for this project, but I will, if I build another programmer. Things will be easier in future if you use the standard 6-pin plug (or the 10-pin, but I prefer the 6-pin one), just in case you happen to use another programmer with your project, or your programmer with another project.
Update on 2008-12-02: Originally, the 6-pin pinout was wrong. It has been fixed, so the pinout below is correct.
Even though I have not used the standard connector, I kept the same wire order as the 6-pin one. Then, I tried to use the following pen to mark the wire 1 as black:
Unfortunately, it was not a good choice, because the painted portion of that cable now leaves a black stain everywhere it touches.
The photos above were taken using a SonyEricsson K750i phone at full resolution (2 megapixels). Then, I used Gimp to scale down the images after applying Layer -> Colors -> Auto -> White Balance.
Other DIY AVR Programmers
Update at 2011-08-10: This section is now somewhat obsolete, as I've just written part 2.1, which deals with USBasp programmer. If you prefer, skip the rest of this post and go directly to part 2.1.
Buffered parallel port
That personal homepage address found at #avr channel topic has a link to a PDF schematic diagram (local mirror) (Update on 2008-12-02: be careful, the pinout in this PDF is wrong) that describes how to build a buffered parallel port dongle. It uses one 100K pull-up resistor, one 0.1uF decoupling capacitor, and a 74HC244 buffer chip. A similar diagram can be found elsewhere on the web.
I'm not sure, but as I understood the schematics, this dongle will get the needed Vcc from the microcontroller connector.
The MiniPOV3 DIY project uses a serial port to program the chip. This works in a similar way to the parallel port programming, but requires some diodes too. You can see the schematic diagram at Design link. You can also see a page dedicated to the serial dongle at SpokePOV project.
Personally, I don't see enough good reasons to use the serial port over the parallel port. The only good reason is the size (a serial port is about half of the size of a parallel port). However, I guess that, if a PC does not have a parallel port, most likely it won't have a serial port either. In addition, the serial port programmer requires a few more componentes (3 diodes). Finally, it uses a method called "bitbanging". I don't like this name, it sounds too hackish and cheap for me.
There is a so-called inexpensive USB AVR programmer. You can buy the kit with all needed components for US$ 22.00, or you can use the instructions and find or buy the components by your own.
This programmer is more complex. It uses an AVR microcontroller and some tri-state buffers. It will be difficult (or impossible, or at least ugly and messy) to build this without a PCB or a stripboard. It also has two LEDs (one red and one green) to display the current status. If you buy the kit, this programmer looks pretty neat because of the case and the LEDs.
This programmer is also good because it can use the USB to give enough power to the microcontroller being programmed, avoiding the need of external power supply, unlike the parallel/serial programmers.
In my opinion, if your PC has neither the parallel port nor the serial port, but has at least one available USB port, and you don't want to buy a USB-to-Serial or a USB-to-Parallel adapter, then this is the programmer you should build (I've changed my mind, see the update below).
On the other hand, there are some disadvantages with this project. The first one is the price (US$ 22.00 versus US$ 7.50 for the parallel/serial dongle kit at the same site). The second one is the need for a USB A-B cable. Of course you must have at least one of this cable lying around somewhere (or at least one connecting your printer or scanner), but the parallel/serial programmers don't need any extra cable. Ok, I know this is easy to fix. Just get a cable, cut one of the connectors and solder the cable to the dongle.
Another disadvantage is the buggy libusb on 64-bit architectures, as you can read on FAQ. It also looks like you need to be root to use libusb and this device (but I'm not sure), while it's not needed with parallel/serial programmers (I need to get root once to change permissions of the /dev/something device or to add my ordinary user to some group).
Yet another disadvantage is the difficulty to build this dongle without buying the kit. You must buy two chips and a PCB or a stripboard and solder everything together. The buffer chip should be easy to find (but I don't know if it really is). The ATTINY2313-20PU chip, however, might not be so easy. I found ATmega8 and ATmega16 at Cerne-Tec, (which is located in Rio de Janeiro, where I live), but only these two models are sold there, they don't sell ATtiny. Probably I can replace the ATtiny with ATmega, but it will probably make the project a bit more expensive. Finally, whenever you get the microcontroller, you still need to program it with the USBtinyISP firmware, but how? (the "chicken & egg" problem) Well, you can use someone else's programmer, or you can just use a serial or parallel programmer. But, in the latter case, why don't you just use that programmer for everything else?
Update at 2007-01-03: I've just found another USB ISP programmer. It's called USBasp. "It simply consists of an ATMega48 or an ATMega8 and a couple of passive components. The programmer uses a firmware-only USB driver, no special USB controller is needed." After reading this, I've changed my mind. If I ever need to use an AVR programmer on a PC without parallel port, I will build this one.
I've read at a page with instructions about USBasp that you should connect the programmer to a port on the computer, avoiding USB hubs. I'm not sure if this is the case of other USB programmers, but since nowadays computers fortunately have many USB ports, I hope this won't be a problem.
Update at 2011-08-10: I've bought USBasp, and I've also mounted it on a breadboard. Everything is described in detail at part 2.1.
- PC parallel LPT port pinout (SPP)
- ECP Parallel LPT port (IEEE-1284A) pinout
- Information about using AVR ISP programmers
Go to: part 1, part 2, part 2.1 (video), part 3, part 4 (video).