How To Program an AVR Chip With The AVRISP mkII

by Miguel on May 11, 2011

in AVR

In this post I will walk you through the steps required to program an AVR chip using the AVRISP mkII.

AVR chip, programmer, breadboard and power supply

AVR chip, programmer, adapter, breadboard and power supply

Hardware and Software Required

Here is a list of things you’ll need

Hardware

Software

Circuit Set Up

Simply find the pins in your programmer, match them to the pins in your chip and connect them. Also connect the resistor from pin 7 to power.

AVRISP MKII pins

AVRISP MKII pins


ATmega328p pins

ATmega328p pin out

Now because the programmer doesn’t have one squared hole, or at least mine doesn’t, it has many, let me show how how that looks in real life, and yes it is like a mirror image of the image above (weird right?).

AVRisp mkII cable connection

AVRisp mkII holes

Now that we have everything connected…

AVRISP mkII, breadboard and atmega328p circuit

AVRISP mkII: circuit to program


..let’s move on to the software. Check out the picture at the very top of this page to see how much neater your circuit will be with the programming adapter.

Your First AVR Program

Let me go ahead and walk you through the steps of using AVR Studio 5.

Once you have opened AVR Studio click on File ->New -> Project.

avr studio 5 new project menu

Creating a new project in AVR Stduio 5


Name your project, I named mine firstprogram. Now click the OK button.

new project window

naming your project

Next you will be greeted with an almost blank C file.

a blank avr studio 5 c file

AVR Studio blank template C file


So what software are we going to write? how about we make an led blinker. Connect an LED to port C5 of your microcontroller through the 1k resistor and then to ground.

atmega328p microcontroller with red led

red led connected to port C5 (pin 28) of the ATmega328p

Now back to AVR Studio. Replace the text in the file with the following.

#include <avr/io.h>
#include <avr/delay.h> // has some delay functions you can use

int main(void)
{
	DDRC=0xff; // all C ports as output
    while(1)
    {
        //TODO:: Please write your application code 
		
		PORTC=0xff; // turn on all C porst
		_delay_ms(200); // wait 200 milliseconds
		PORTC=0x00; // turn off all c ports
		_delay_ms(200); // wait 200 milliseconds
		
		
    }
}

The code that gets uploaded to the microcontroller is actually a HEX file, but we have to tell AVR Studio to make this file. To do that right click on your project’s name on the right in the solution explorer window and click on properties.

avr studio solution explorer window

AVR studio solution explorer window

In the new window that will open check the box that says hex and save (CTRL+S)

hex file checkbox

hex file checkbox, make sure it's checked.

Now to actually generate the hex file click Build->Build Solution

avr studio build menu

this will compile the program and generate the hex file we need.

You should not get any errors, as shown below.

program compilation errors

no compilation errors

Upload The Code to The Microcontroller

At last, we are done with all the software developing stuff, uploading is just as easy.
Click on Tools->AVR Programming

avr studio tools menu

avr studio tools menu

You’ll get the programming window. Make sure that your device is selected.

avr programming window

the programming window

Now in the following order, click on the Apply button, then the voltage button (recycling/refresh looking icon) will become active now click it, you should get some value close to 5 volts. Then click on the Read button. If you didn’t get any error window you are good to go.

Clicking the Read and Voltage buttons is not necessary actually, but the Apply button is.

avr studio programming window: 5 volts and device id

programming window after clicking buttons

lastly click on Memories and select your hex file then click Program, the programmer will start blinking and stop when your program is done uploading.

avr studio memories window, hex file ready to upload

hex file ready for upload

For The Non-Believers: Proof That This Works

And of course here is the program in action. The big bright light is a ghost…just kidding is the light coming from the the super bright led in my power supply (thanks Sparkfun :P)

Related posts:

blog comments powered by Disqus

Previous post:

Next post: