23 May 2013
How to Do ISP Programming – Part 2: Hardware Considerations
What Hardware do I need?
Obviously you need the controller. This can be any Atmel ATtiny or ATMega. You need a circuit board to put the controller in (the "target board") and you need an ISP programmer. Since ISP uses a serial format, the programmer can be rather compact.
There is a wide variety of programmers on the market, from full featured expensive to cheap and simple. Since this tutorial is targeted at beginners, who don't want to risk a lot of money for their first steps, I will describe using one of the cheapest options here.
And that option is a programmer called USBasp. It is a great open-source project by Thomas Fischl. The programmer is connected to the USB bus of a PC and has an ISP cable on the other end. It is supplied through the USB bus, so no external power supply is needed.
The project is so popular, that it is widely used and supported by many software environments. And some Chinese companies saw a business opportunity and are now offering the ready made programmer for very little money. I wrote a review on this earlier.
How to connect
As mentioned in part 1 of this tutorial, you need only a few lines to connect your ISP programmer to the target board. There is the serial interface, comprising MISO, MOSI and SCK pins, there is the RESET pin and there are the power supply lines Vcc and GND. Here are two examples how the connections have to be made. The first one is from an earlier post. I is for the ATMega8:
The second example is for the ATtiny13:
The programmer is supplied through the USB port. This is good. But there is something to look out for. The ISP connector has a Vcc pin. This can be used in two ways. Either, the programmer can supply the target board, or the target board can supply the programmer. Since the USBasp programmer has its own supply, the second option makes no sense for us.
WARNING: If your target board has its own supply, DO NOT CONNECT THIS PIN. Otherwise you have two supplies working against each other! This could destroy your hardware. In many cases I do use the programmer to supply my target board. It is usually quite convenient.
Hint: Some USBasp variants have a dedicated jumper to disconnect the power supply from the programming cable. In that case you can connect Vcc and connect / disconnect it via that jumper.
SCK Clock Issues
When programming an AVR controller via ISP there are two clocks involved. First, there is the system clock. In order to keep things simple, the standard system clock is used for this, just like during normal operation.
The second clock is the SCK. This is a shift register clock, which is used to move serial data into and out of the serial interface. There is a very important condition: The frequency of SCK must be less than one quarter of the system clock.
So if for some reason your programmer cannot communicate with your target, check whether this condition is met.
And there is something else that you have to know about the USBasp project: Older versions of the programmer's firmware used a very simple scheme to set SCK. The standard frequency was 375 kHz. This would only work for system clock frequencies over 4 x 375 = 1.5 MHz. But many AVRs come with a preset frequency of 1 MHz (e.g. ATMega8: Internal RC oscillator with 8 MHz, but CKDIV8 fuse is set, i.e. the system clock is divided by 8).
So in order to slow down SCK the project initially used a jumper ("J3"), which would reduce the clock frequency to 8 kHz when set. Later versions of the firmware can set the frequency by software.
The only problem is that some Chinese programmers still come with the older firmware version. And avrdude doesn't like that. It throws a warning:
avrdude.exe: warning: cannot set sck period. please check for usbasp firmware update.
avrdude.exe: AVR device initialized and ready to accept instructions
Don't panic. Programming should still work, as long as the SCK frequency isn't too high for you target. If it is, you have to set J3.
In the next part we look at the software and how to use it.