Programming an ATMega With No Bootloader Using the USBtinyISP
The title says it all: for my “Deconspectrum” installation, I am burning a program onto a bunch of ATMega328 chips using a USBtinyISP (In-System Programmer). At first I was running into a perplexing problem: the program was running much slower than it should have been. I could tell immediately because I had a short POST (Power-On Self Test) at the beginning of the program that flashes the LED Red-Green-Blue, and it was going far slower than it should have been.
My first thought was that there was a problem with the ATMega “fuses,” which are just settings for various esoteric details of the chip’s functioning, including clock rate. But I had never had to bother with them before. Then I made a further discovery that the only time the program ran correctly was if I installed the Arduino bootloader first and uploaded the program using it. What gives?
Thanks to the plucky denizens of the ITP Physical Computing list, I learned that indeed, it was a matter of fuses—the Arduino IDE apparently only sets the correct fuses when burning the bootloader, not when burning a program directly using an ISP.
The solution: use avrdude, the program that Arduino uses to actually program the chip behind the scenes, directly. This turned out to not only solve my problem, but to make the programming process much faster and simpler. Avrdude takes a “hex” file as an input to transfer to the chip, which is the compiled bytecode that the ATMega actually runs. This way, I only have to compile the program once, and burn it directly—and since I’m programming about 40 chips, this makes things far faster.
All you need to do is get the AVR “CrossPack” installed (for OSX; for Windows you can use AVR Studio or a number of other packages) and then find your hex file. In the Arduino IDE, hold Shift while pressing the “Verify” button to produce a verbose debug output. In there you’ll see a path like this:
You can then copy that file to your project directory:
cp /var/folders/.../myProgram.cpp.hex ~/Projects/myProject/myProgram.cpp.hex
And program your chip like so:
avrdude -c usbtiny -p m328p -b 57600 -U flash:w:myProgram.cpp.hex:i -U efuse:w:0x05:m -U hfuse:w:0xde:m -U lfuse:w:0xff:m
Those settings are for the ATMega328; for the 168 use:
To get a list of parts, type:
avrdude -c avrisp
And there you have it! You’ll find that this saves a lot of time if you have to program lots of chips. Besides, not using the Arduino bootloader will save a little space on your chip 😉