english german

AVR Tutorial for writing a program in C

This AVR Tutorial guides step by step to the first running C programm on your AVR microcontroller.


Introduction

It's not difficult to make a program running on an AVR microcontroller. If you have all the needed equipment ready, it is possible to learn how to work with an ATmega8 within one evening. This tutorial guides you step by step to find the shortest way to your first working program. We start with the required hardware. Then we install the AVR studio and the AVR GCC compiler (both are free). Then, after getting the most important datasheets, we write our first little program and make it running on an ATmega8.

Click on the picture in this tutorial to watch them larger in a popup window.

Schtepp 1: Required Hardware

Bevore we start you should get the following material:

Computer
Of course we need a computer to program our AVR. It doesn't have to be a powerful machine. Personally, I use an old portable computer with Windows XP. If your machine has a serial port it makes your life a little easier because you don't need an USB to serial adapter. The STK500 developmentboard which we use in this tutorial, requires a computer with serial interface or a USB to serial adapter.

AVR Tutorial C Computer

AVR Tutorial C Adapter

USB to RS232 Adapter
If your computer doesn't have a serial port, you need an USB to serial adapter to load your program on the microcontroller. I've made good experiences with the adapter UC232A manifactured by Aten.

AVR development board STK500
In this tutorial we use the STK500 development board of ATMEL. It's not the cheapest solution but probably the fastest. Because it's made by ATMEL the compatibility to the microcontroller is guaranteed and by using this board, we can be optimistic that we will have no troubles with any kind of wrong wiring or any other possible error sources. A RS232 cable is delivered with the board.

AVR Tutorial C STK500

AVR Tutorial C Adapter

Power supply
Unfortunately, the STK500 is delivered without power adapter. In the delivery scope is only a cable with the DC Connector and two bare ended wires. So you need a 12V power adapter. The best solution is of course an adjustable laboratory power supply.

AVR Microcontroller ATmega8
This tutorial is written for working with the ATmega8. Tough it could be written for any other AVR uC. Because there is no "best AVR". Every AVR has its own specific attributes and features. It depends on the application, which type you use. But the ATmega8 is a nice and simple allrounder which can be used for many projects. It contains the basic peripherals and is probably the microcontroller which is most used in hobby electronics.

It's always good to having a few spare controllers in your drawer. I recommend to having three to five pieces ready. Because everybody gets to a point somewhen where he wants to check if there is a buc in the code or if the controller has been broken.

AVR Tutorial C STK500

Step 2: Download and install the required software

AVR Studio
On Atmels website you can download the "AVR studio 4". Though version 5 and 6 have been released years ago I recommend AVR studio 4 for the following reasons:

AVR Tutorial C AVR Studio 4 Screenshot

AVR Tutorial C Schritt 1 Version 6 is optimized for the STK-600 board which is much more expensive than STK-500.
AVR Tutorial C Schritt 1 In forums you find many reports of people who had troubles with AVR studio 6 and the STK-500.
AVR Tutorial C Schritt 1 Version 4 gives you everything you need in order to develop software for the ATmega8.
AVR Tutorial C Schritt 1 Version 6 comes with uncountable buttons and functions, which you don't need.
AVR Tutorial C Schritt 1 Version 4 is a slim 100MB download, while Version 6 is 800MB
AVR Tutorial C Schritt 1 Version 6 gives you hunderts of code samples but jus a few are for the ATmega8
AVR Tutorial C Schritt 1 Version 4 is better supported by the ATmega8 internet communities. You will get help and advice much faster.

Use the standard settings, when you install the software.

AVR Tutoril AVR Toolchain

AVR Toolchain
The AVR Toolchain is necessary to compile to c code. It can be downloaded free from the Atmel website.

Use the standard settings for the installation.

ATmega8 Datasheet
Enter "ATmega8" at google. The datasheet will usually be the first search match. Or download it form: Atmel.

AVR Tutorial datasheet

Step 3: set up STK-500

Now we set up the STK500

AVR Tutorial C Schritt 1 Insert the ATmega8 into the socket "SCKT3200A2".

AVR Tutorial C Schritt 1 Check right now if you have put it in the right way round!


AVR Tutorial C Schritt 1

AVR Tutorial C Schritt 1 Connect the green socket "SPROG2" and the "ISP6PIN" socket with the 6-pole programmer cable.

AVR Tutorial C Schritt 1 Take care thet the red wire is at pin 1 on both sockets.


AVR Tutorial C Schritt 1

Set the jumpers

AVR Tutorial C Schritt 1 VTARGET
AVR Tutorial C Schritt 1 AREF
AVR Tutorial C Schritt 1 RESET
AVR Tutorial C Schritt 1 XTAL1
AVR Tutorial C Schritt 1 OSCSEL

according to the picture. Take care that there is no resonator at the "CRYSTAL" socket.


AVR Tutorial C Schritt 1

AVR Tutorial C Schritt 1 Connect the board with the serial port of your computer. Use the connector "RS232 CTRL" not "RS232 SPARE"

AVR Tutorial C Schritt 1 Connect the power supply


AVR Tutorial C Schritt 1

Step 4: Create a new project in AVR Studio

After you run the program, the welcom window appears. To create a new project, you just click on the button New Project .

AVR Tutorial C Schritt 2

In the dialogue, do the following steps:
AVR Tutorial C Schritt 1 Type your project folder at Location.
AVR Tutorial C Schritt 1 Select AVR GCC for the Project Type.
AVR Tutorial C Schritt 1 Enter a Project name on Project Name.
AVR Tutorial C Schritt 1 Remove the Create initial file tick. The field Initial File.
AVR Tutorial C Schritt 1 Click on Finish (not Next)

AVR Tutorial C Schritt 3

The development environment appears. On the left side you find the project explorer. RMB click on the project name on the top. From the drop down menu you chose Edit Configuration Options.

AVR Tutorial C Schritt 4

The project configuration window appears
AVR Tutorial C Schritt 1 Choose "ATmega8" as your Device
AVR Tutorial C Schritt 1 Choose -01 for the Optimization
AVR Tutorial C Schritt 1 Close the window with Ok.

AVR Tutorial C Schritt 5

Schritt 5: Insert C-Code in new C-file.

Lets create a new .c-file. Do a RMB click on Source Files. In the appearing dialog click on Create new Source File.
Enter the name of the file in the appearing window. The file with the main() function you should name main.c. Then click on ok.

AVR Tutorial C Schritt 6

Enter the code from the picture into the file. It's our first test program which reads a switch and sets an LED. You can also copy the code of the following code window.

AVR Tutorial C Schritt 7

#include <avr/io.h>
int main(void)
{

    DDRC = 0x01;
    for(;;)
    {
        if(PIND & 0x01)
            PORTC |= 0x01;
        else
            PORTC &= ~0x01;
    }

    return 0;
}

Step 6: Compile and debug

Then click on Build. Now the program is being compiled.

AVR Tutorial C Schritt 8

In the report window on the bottom you can see if the compilation was finished successfully. If there was an compiling error, you can click on the line in the report and AVR studio shows you in which code line the error has been found.

AVR Tutorial C Schritt 9

Step 7: Connect to STK500 and download the code.

Click on Connect

AVR Tutorial C Schritt 10

The Select AVR Programmer dialogue appears.
AVR Tutorial C Schritt 1 Choose the STK500 as Platform.
AVR Tutorial C Schritt 1 On Port you select "Auto"
AVR Tutorial C Schritt 1 Click on Connect.

AVR Tutorial C Schritt 11

The STK500 ISP Mode with ATmega8 window appears
AVR Tutorial C Schritt 1 Choose the index tab Program

AVR Tutorial C Schritt 1 As input HEX File you choose the .hex File in you projectfolder default. This was created when you compiled the program.

AVR Tutorial C Schritt 1 Click on Program.

Now the program is loaded into the ATmega8

AVR Tutorial C Schritt 1 Check if there is an OK on every line of the report.

AVR Tutorial C Schritt 11


AVR Tutorial C Schritt 1 Choose Fuses

AVR Tutorial C Schritt 1 Set all options as shown in the picture.

AVR Tutorial C Schritt 1 Then click on Program.

Not, the fuses of the ATmega8 are beeing set.

AVR Tutorial C Schritt 1 Check if there is an OK on every line of the report.

AVR Tutorial C Schritt 11

Stepp 8: Connect the interface cable

Connect the LED's of the STK500 with Port C and the switches with Port D. Use the two flat cables as shown on the picture.

AVR Tutorial Interfacekabel

Step 9: Test the program.

The program reads the switch SW0 and shows its state on LED0.

If the switch is not pressed, the LED stays dark.

AVR Tutorial C Schritt 10



When you push the button, the LED lights up.


Congratulation! You finished the tutorial and your first program is running on the AVR.

AVR Tutorial C Schritt 11


Startpage
Circuits Ressistor Code Farbcode Quiz Resistors Servos Standard Transistors Connectors Robot Projects
ATmega8 ADC ATmega8 Timer0 ATmega8 Timer1 ATmega8 Ext. Int. ATmega16 Timer0 ATmega16 Timer1 ATmega16 ADC ATmega16 Ext. Int. ATmega32 Timer0 ATmega32 Timer1 ATmega32 ADC ATmega32 Ext. Int.
CD Laser replacement Servo Hacking AVR Tutorial C
Feedback Cube Eyes