STK500 and USBtinyISP
Reading Bill Bumgarner’s latest posts on Atmel AVRs got me interested in AVR development, again. I’ve done a fair amount of microcontroller hacking in the past, and I bought an STK500 when they went on sale last year for $50 (plus a free AVR Dragon). Unfortunately, I never even bothered to power it up! Reading his posts finally got me motivated to setup an AVR development environment on my Mac.
One downside to the STK500 is that it typically requires a serial port to program the chips. This is especially problematic for Macs, as they haven’t shipped with a serial port in a long time. Yeah, you can buy USB to RS-232 adapters, but that’s a bit clunky. The supported programming software (AVR Studio) only runs on Windows, as well.
One the upside, AVRs support low-voltage in-system programming (ISP) using a standard 6- or 10-pin header. The STK500 also has these headers, so theoretically any ISP programmer should work.
ISP programming is superior to programming over RS-232 since it’s much more versatile, anyways. AVR ISP doesn’t require a high voltage or sacrificing of any I/O pins, and you can put a 6-pin ISP header on your own custom hardware very easily. This allows you to reprogram your chips without having to pull them out and into a special-purpose programmer. So if I could get ISP and the STK500 to work together, this would be an ideal setup.
I couldn’t find a lot of information about using ISP programmers and the STK500 (most people probably just use the serial port), but it turns out, you can program chips on the STK500 over ISP, even on a Mac, just fine. Read on for the details.
As per bbum’s advice, I decided to buy ladyada’s USBtinyISP from Adafruit. I made the modifications that he recommended as well (replace R6 and R7 with wires). Since this is a USB device, it doesn’t require any serial port and works on a Mac via avrdude. avrdude is included in Objective Development’s AVR MacPack (also highly recommended).
My USBtinyISP showed up last weekend, and I finally had time to solder and assemble it on Monday. My soldering skills were a bit rusty, but the board came together pretty easily. When it was time to see if it actually worked, I plugged it in to my Mac to see if it recognized it. Sure enough, the green LED came on, and the Mac recognized a new USB device (verified using the USB Prober app in /Developer/Utilities
).
The STK500 comes with a pre-programmed ATmega8515. I wanted to reprogram the chip with a simple blinky LED Hello World program. Unfortunately, avrdude couldn’t communicate with this chip for some reason. The STK500 also came with a blank ATmega16, so I decided to pull the 8515 and try this chip instead. This time, avrdude was able to ping the chip, so I tried to program it. It seemed to program just fine, but when I reset the board, I got no blinking LED. Also avrdude could no longer ping the chip, which means I was no longer able to reprogram it. Drat! I essentially bricked the chip.
I’m not sure exactly what went wrong, but I suspect one of two things. First, I forgot to disconnect the LED ribbon cable before programming. ISP uses I/O pins, and if the pins are hooked up to something else, it’s possible they could interfere with ISP programming. The other way you can render a chip unusable is by programming bad fuse values.
AVRs have these special programmable registers called fuses. Fuses tell the AVR about its clock source, as well as other things, such as disabling reset and ISP. Programing the fuse bytes incorrectly can easily lead to a bricked chip, so I think that’s what happened. I tried to program the fuse bytes to use the STK500’s on-board 3.68 MHz software clock. It’s possible I chose bad fuse values.
I now had 2 chips that I was no longer able to program, so I bought a few more from Mouser. Mouser is nice because you don’t need a minimum order. I bought two ATmega16s to replace the one I busted, and I bought two ATmega168s, since those seem to be quite popular. I also bought a few crystals, so I could play with higher frequencies. The ATmega168s can run up to 20 Mhz.
My Mouser order arrived on Thursday, so I decided to try an ATmega168, first. This way I could try bbum’s one-led-blinker code and makefile directly. Even though the STK500 User Guide doesn’t explicitly list the ATmega168 as supported, it works in the green SCKT3200A2 socket (which I found out from this post). I hooked up the USBtinyISP to the green SPROG2 6-pin ISP header, and sure enough avrdude could ping it. This time, I decided to use with the default internal 1 MHz clock, so I could stick with the default fuse values. I removed the XTAL1 jumper on the STK500 to disconnect the board’s clock system.
bbum’s one-led-blinker requires PORTB to be hooked up to the LEDs, which is easy enough with the supplied STK500 10-pin ribbon cable. However, this time I disconnected the LEDs before running avrdude, and it seemed to work! When I reset the chip, I got a blinky LED.
One minor difference between bbum’s EMSL setup and the STK500 is that the STK500’s LEDs light up when pulled low, not high, so I needed to invert the PORTB values he had. But now that I could reprogram the chip at will, this was an easy fix.
So finally, I have a working AVR development setup on my Mac. I’ve got no projects in mind, but it’s nice to have an AVR playground.