Program Atmel AVR Microcontrollers Using Arduino

Posted by in Experiments, Technology, Tutorials

Arduino

 

Why program an AVR Chip using Arduino?! Well, there may be 2 reasons:

1) Either you don’t have a dedicated AVR programmer(or ISP)

2) You want to play a little with arduino!

First is my reason! I don’t have a dedicated AVR chip burner. I had an ATMega16A micro controller laying around for some time without any use for some time which i bought accidently from an electronics shop. I asked for an ATMega168 but instead of that, the guy there gave me an ATMega16! I didn’t bother to check when i was at the shop since i bought a lot of parts that day & after reaching home, i realized the mistake. I didn’t went back to replace, instead choose to keep it, hoping that it’ll be of some use someday!

I read somewhere that i can use an Arduino to program other chips & I thought to give it a try. Its really simple! Actually, there are a lot of tutorials out there to accomplish the same. I decided to write one because the ones out there were kinda confusing(i found some of them confusing!).

Before starting

Before starting anything, make sure you’ve installed WinAVR on your system. Click here to go to the download page of WinAVR. We need AVRDUDE, which comes with WinAVR to write AVR chips. Install WinAVR first.

Above all, you must have an Atmel AVR chip to program! Find them here, on the Element14 Page.

How to..

1) Connect your Arduino board to your PC & open Arduino software

2) Goto File>Examples & select ArduinoISP. The ArduinoISP sketch will load up

ArduinoISP

3) Click upload to board. After uploading, disconnect the Arduino from your PC.

4) Now, generate the HEX file you want to burn to the target chip. I use mikroC Pro for AVR for programming AVR chips.

5) Make a new folder on your desktop & copy the generated HEX file (assuming that you know how to program & generate HEX file for an AVR uC 😛 ). Let the name of the hex file be “LEDBlink.hex”.

Hex

6) Connect 5v & ground from the arduino to the target chip (assuming that the chip is secured on a breadboard)

7) Now, find the MOSI,MISO,SCK & RESET pins of the target chip from its datasheet. I’m using an ATMega16A

8) Make the connections as follows:

Arduino Pin 10 -> RESET

Arduino Pin 11 -> MOSI

Arduino Pin 12 -> MISO

Arduino Pin 13 -> SCK

Make sure the connections are neat & tight. Use single strand wires to ensure tight connections on the breadboard

9) Connect the arduino back to the PC

6) Now, open a command prompt (Start>run>type in “cmd” without quotes & press enter)

7) Type in at the command prompt:

avrdude -F -P COM12 -b 19200 -c avrisp -p m16 -v -e -U flash:w:LedBlink.hex

Make sure you change “COM12″ to the port to which your arduino is connected. In My case, its comport 12 & hence, its COM12. Press enter after typing the command & you should see something like this:

Command Prompt

 

This means AVRDUDE has finished writing your HEX file. Instead of writing the command over & over into the command prompt, a simple batch file can be made. For that, write the command into notepad & select File> Save As & Save it as filename.BAT. Now, double click the created batch file to start writing the HEX file.

 

ATMega16A Running Blink Program burned using Arduino as ISP:

AVR

 

Always be careful with fuses! I’ve tinkered with an LFuse & ended up frying the above ATMega16! Now i can’t do anything! Think i’d have to buy a new one 🙁 You can make a dedicated USB programmer for AVR chips easily with minimum number of parts or you can buy one. They’re really cheap nowadays!