Programming an AVR using the Arduino (based on instructables)

So recently I was hoping I could start programming AVRs. I like using the Arduino IDE because it is incredibly easy, however, programming a chip using C or even Assembly is far more powerful. I was looking through everything to try and find a feasible solution to get my AVR programming started, so I searched through programming cables, dongles, DYI things, Sparkfun, Atmel, etc etc etc… I found out programming cables can get to be expensive, especially if your computer doesn’t have an RS-232 port.
Luckily, instructables.com exists and saved the day, since they have a pretty good step by step process on how to program an AVR using an Arduino!!! Making the Arduino a middleman in order to program your AVR… so brilliant and convenient! (an arduino costs about 20 dollars, much less than a USB stk mkii programming cable). The instructable is really good and I recommend following it, however I will explain the process I went through to burn a bootloader onto an ATMega168, and the same process can be used to burn a bootloader to any other AVR supported by the Arduino. I’ll also show how you can upload a hex file (from a C or ASM file) to your AVR through this Arduino circuit.

You can find the instructable here .

For the case and purpose of this blogpost, all you really need from this instructable is the Arduino circuit and where to get the code (which I’ll post a link to later on). The circuit involves hooking up the Arduino to the AVR and hooking up some nifty LED’s for debugging purposes (and because they look kind of cool when you reset the ARduino/do your AVR programming/etc). Installing WinAVR is recommendable! To do so, go to this site and get it donee!!!

The first thing to keep in mind is that these Atmel AVRs have an ISP programming option (acronym for In-System Programming). Also, the communication protocol that you can use for the programming of the hardware is SPI (Serial Peripheral Interface). Lucky for us, there are well labeled pinout diagrams for these AVR chips all over the internet, and there is a built in SPI interface on the Arduino boards (obviously, since the Arduino is based on the ATmega168/328/etc et al).
SPI is a basic protocol, but all that you need to know at this point is that you want to connect each respective Arduino pin to their “counterparts” on the AVR you wish to program.

Ok. Now, in order for your Arduino to work as a ‘middleman’ programmer, you’ll have to upload the proper code to it so that it does it’s job. I’m not taking credit for this code, because I obviously didn’t write it, so I’ll link the word after the semi-colon to the website you can find it: here. Download the latest avrisp.xx.zip and extract the code to your computer and upload it to the Arduino.

Bam! Now the LED’s in the circuit start flashing, and the last one is getting brighter, dimmer, brighter, dimmer, etc etc etc..Like a heartbeat! Its alive! You know your circuit is working! And the Arduino is ready to receive its instructions… How will it do that?? Serial Port?? nope! You’ll have to talk to avrdude from you command prompt (quite frankly it makes you feel like a pretty cool hacker). In order to have avrdude installed in your computer, you’ll need to either have Arduino installed (since you burned the code to the ARduino you probably already have that) or you can just download avrdude from its site… i think this one is pretty recent/legit. Anyhow, just make sure you have avrdude installed on your computer before proceeding, or else you’ll be communicating with a non existing entity on your computer, and your computer will think you’re crazy.

Ok. To talk to avrdude, all you have to write in the command prompt (regardless in which directory you’re in) is:
avrdude

to see a list of commands, type in
>avrdude
to your command prompt, you should get the following:


pretty easy right?!?! now let’s actually talk to the Arduino! Make sure you know what SERIAL PORT your Arduino is connected to. Usually Com5 for windows, dev/tty/USB for Mac etc.. To find what port you communicate to, open the Device manager in your control panel and see which one is active ***OR*** just open the Arduino IDE and look for the Serial Port option (like you would usually do to program the Arduino). Ok, so now you know what Port you’ll be talking to, time to find out what CHIP you have! For this part, just look at the AVR thats in your circuit and read the AT***** part. OK, now type the following in your command line

>avrdude -c avrisp

You’ll get an error message and a long list with recognizable MCU names!

Great, now just look for the MCU you have and know which one it is… I’ll be using the ATMega168 as an example because that’s the chip i used. Likewise, my COM port was COM6.
So type in the following, just modify COM6 or m168 IF NECESSARY:

>avrdude c -avrisp -b 19200 -p m168 -P COM6
(then press enter)

I usually get an error message the first time I type that in saying:
avrdude:stk500_getsync(): not in sync: resp=0x15
So i try it again (shortcut, press the up arrow for the last command you typed)
You’ll see your circuit LEDs do a weird blinking cycle, and your command prompt should look like something like this:

Great, so now your computer is successfully talking to the Arduino!
Now let’s go step-by-step to see what that command did

>avrdude -c avrisp -b 19200 -p m168 -P COM6

-c avrisp => (avrdude, I would like to use the avrisp programmer!)
-b 19200 => (avrdude, I want to change the baudrate to 19200 baud)
-p m168 => (avrdude, the part I’m using is the ATMega168)
-P COM6 => (… my Arduino is on port COM6)

We will also use a -U command which writes a specified file or fuse bit to the AVR and the -e command which erases the AVR

Ok, so now to burn the bootloader to the Arduino, you must find the bootloader file on the arduino hardware\atmega folder. If you’re using the 168, you’re lookingfor the ATmegaBOOT_168_diecimila.hex file,if you’re not using the ATmega168, use some common sense to find out what file to specify. Ok so now you have the path that the bootloader file is located in, in my case it was .
Now write these commands (**adjusting it to your configurations**)(press enter after each line, or just copy the entire thing and paste it to the command line)

>avrdude -c avrisp -p m168 -P COM6 -b 19200 -e
>cd C:\Program Files\arduino-0016\hardware\bootloaders\atmega
>avrdude -c avrisp -p m168 -P COM6 -b 19200 -U lock:w:0x3f:m -U efuse:w:0x00:m -U hfuse:w:0xDD:m -U lfuse:w:0xFF:m
>avrdude -c avrisp -p m168 -P COM6 -b 19200 -U flash:w:ATmegaBOOT_168_diecimila.hex

(If you get error messages, check your configurations!!!!!)

All we in that code was say “Hey avrdude, erase whatevers on the chip, then i want you to overwrite the fuses (-U xfuse:w:#x##:m) and this program on the flash memory (-U flash:w:ATmegaBOOT_168_diecimila.hex) I’ll leave it up to you to figure out what the fuse bits are adjusted to (but essentially you need it to se the AVR to work off an external oscillator).

To burn a HEX file, you’ll need to convert the C or ASM file to HEX (using WinAVR or AVRStudio). When you build the project, there will be a folder that saves the HEX version of your code.

now just modify the following according to your computer specifications and type

>avrdude -c avrisp -p m168 -P COM6 -b 19200 -e
>cd
>avrdude -c avrisp -p m168 -P COM6 -b 19200 -U lock:w:0x3f:m -U efuse:w:0x00:m -U hfuse:w:0xDD:m -U lfuse:w:0xFF:m
>avrdude -c avrisp -p m168 -P COM6 -b 19200 -U flash:w:.hex

GREAT! You can use and Arduino to program an AVR and to burn bootloaders.

Any question feel free to find me at M5 or e-mail me at: dbercht@student.umass.edu

Leave a Reply

Your email address will not be published. Required fields are marked *