In this post I will walk you through the steps required to program an AVR chip using the AVRISP mkII.
Hardware and Software Required
Here is a list of things you’ll need
Hardware
- 4.7K resistor
- 1k resistor
- LED
- AVR Chip: I’ll use the atmega328p
- Programmer: AVRISP mkII
- AVR Programmer Adapter (optional)
- Jumper Wires
- 5V Power supply. You can of course use batteries if you prefer.
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.
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?).
Now that we have everything connected…
..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.
Name your project, I named mine firstprogram. Now click the OK button.
Next you will be greeted with an almost blank 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.
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.
In the new window that will open check the box that says hex and save (CTRL+S)
Now to actually generate the hex file click Build->Build Solution
You should not get any errors, as shown below.
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
You’ll get the programming window. Make sure that your device is selected.
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.
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.
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)