Back to main tutorial page

Programming the AVR: hello world

There are essentially three stages here:
  1. Set fuses on a new AVR (usually we only do this once for each new AVR).
  2. Set up ICCAVR to use the USB programmer.
  3. Compile and download a program to the AVR.

For this tutorial you will need an AVR on a breadboard, wired up as explained in the previous tutorial.

You will also need an AVRISP2 AVR programmer, see Fig. 1. If you are in my MECH2401 class you can borrow one of these from the UPSL closed reserve.

Fig. 1 AVRISP2 USB AVR programmer. From http://www.atmel.com/dyn/products/tools_card.asp?tool_id=3808

To program the AVR we must form an electrical connection between the programmer signal lines and the AVR programming input pins. To simplify this process for MECH2401 students I modified the library AVRISP2 devices so they have a "header" termination which is a good match for the pinout of the ATMega8. If you bought your own AVRISP2 and you would like to modify it to match the ATMega8, for convenience, here is a document explaining how to do that.

Fig. 2 Inserting the header terminated plug.

When you insert the programming plug, always check that the coloured wire on the ribbon braid connects to GND as shown in the figure, and that your RESET wire is in place.

Note that the plug has 9 pins but only 6 wires go between the AVR and the AVRISP2. This means that the programming plug will not interfere with the function of PB1 and PB2, for example.

Turn on the power to your breadboard and confirm that your "power" LED is shining.

Plug the AVRISP2 USB cable into your host Windows(TM) computer e.g. one of the lab computers.

Use AVR Studio to set the AVR fuses

The main reason for doing this is to ensure the AVR clock option is the way we want it. You only have to do this ONCE for each fresh AVR. The fuse settings are remembered "forever" until you change them again.

Open AVR Studio, then click "connect".

Fig. 3 AVR Studio Connect.

The following dialog box will appear. Choose:

Fig. 4 Choosing the AVRISP2 as the programmer.

If all is well you will see this dialog:

Fig. 5 STK500 interface: Choose your AVR from the pop-up list.

The dialog of Fig. 5 and several figures to follow is actually the interface of a program called "STK500.exe" which is part of the AVR Studio suite. It is like a sub-program of AVR Studio which is only about communication with the AVR for programming.

You'll see there are several "tabs" along the top of the STK500 dialog. Choose the "board" tab.

Fig. 6 STK500 interface: board tab. Set the speed for communication between your AVR and the AVRISP2 programmer.

The Board tab will also show you your battery or power supply voltage: this is sensed by the AVRISP2 so that it can adapt to whatever voltage you are using.

Now click the Fuses tab.

Fig. 7: STK500 interface: fuses tab

If all is well you will see that some fuses are "checked" and some are not. These fuses are very low-level settings of the AVR.

You will see that the AVR comes from the factory with some settings already made. For example it has the "Int. RC Osc. 1MHz" setting. This means that (by default) the AVR will use its own internal oscillator as the clock source, and that the oscillator will run at about 1MHz. That is OK but we can easily tell the AVR to run faster, so why not. Check the box for "Int. RC Osc. 8 MHz: Start-up time: 6CK + 64ms". Then press Program. If this works you will see encouraging messages at the bottom of the window, and your setting will be shown in the list of fuses.

Although we could twiddle with the other fuses at this point, probably it is better to leave them as they are. Later when you know more about the functions of the AVR you can mess around here more productively. I am guiding you away from this area because it is actually possible to permanently disable your AVR with this tool and I don't want that to happen to you!

Configure ICCAVR

MECH2401 students: the lab computers are already set up correctly (or so I hope). You can probably skip this step. However if you set up ICCAVR on a home computer, you will of course need to configure it.

Close AVRStudio if you wish. Unzip the course code archive to path

c:\\iccavr\course_ATMega8\

Open ICCAVR. Choose "Project/open" and open project:

c:\\iccavr\course_ATMega8\hello.prj

Choose "Project/Options" from the menu. Click the Target tab.

Fig. 8 ICCAVR Project/options/target dialog.

Choose the Tools/In System Programmer menu item. Change all the options shown in red:

Fig. 9 ICCAVR In System Programmer dialog.

When you click on ISP Options... you will see the following. Set the path by typing or use the Browse function to locate STK500.exe. This is how ICCAVR knows where to find the STK500 program, so it can use the USB programmer.

Fig. 10 ICCAVR Environment Options/ISP tab showing path to STK500.exe.

Compile and download a program

At last we have arrived at an activity you may actually be interested in. So far we have just been setting things up. However if you have been able to set the fuses, and ICCAVR is installed, you are most of the way there.

Download course code ZIP achive and extract it to your user area on the hard disk.

Open ICCAVR then use the Project/open menu to open the project

course_ATMega8/hello.prj

The initial appearance of ICCAVR is something like this:

Fig. 11 ICCAVR project window with no source file open.

Double-click on "hello.c" in the right pane. The source file "hello.c" will open and you will see some C language, like this:

Fig. 12 hello.c

Our purpose here is just to get this program compiled and downloaded, so for now there's no need to look too closely at what the program does. Just note that the program mentions PORTC and pin 0. Now refer back to Fig. 2 above and you will see that I have shown an LED and resistor connected from PORTC, pin 0, to GND. If PORTC, pin 0 happens to have 5V on it, the LED will light up. The "hello" program will flash this LED so that we know it is working. It is always nice to have some flashing LED that proves beyond doubt that something is working.

With the AVRISP2 connected as shown in Fig. 2, and with the power on, choose "Project/Make Project" from the menu. After a short delay you should see the LEDs in the AVRISP2 flash a little bit, then you should see confirmation that the compile-download operation was successful, in the lower pane of the project window. Also the LED on PORTC, pin 0 should start to flash.

If it is flashing, CONGRATULATIONS, you are "in the loop".

If it is not flashing, check the LED by connecting it to VCC, with series resistor of course. If that was not the problem, seek help from an experienced AVR user to get you going. Don't give up until that LED is flashing!

Previous tutorial: electrical connections