Starting Atmel AVR C Programming Tutorial 1

In this two series of tutorial, we will provides you with the information on the tools and the basic steps that are involved in using the C programming language for the Atmel AVR microcontrollers. It is intended for people who are new to this type of microcontrollers. The AVRJazz Mega168 board will be use in this tutorial, however this information could be applied to other AVR family as well.

1. Which tools should I use

To start C programming language on Atmel AVR Microcontroller you need to download these following tools:

  • Down load the latest Atmel AVR Studio which provide you with the complete IDE (integrated development environment) for managing project, program editing, compiling, debugging and downloader for all Atmel AVR Microcontroller series.
  • The Atmel AVR Studio only provide you with native microcontroller language (assembler), so you need to down load the WinAVR Project which provide you with AVR GCC (GNU C Compiler for AVR Microcontroller) base compiler and library for window environment. This AVR GCC is fully integrated and supported by Atmel AVR Studio.

After downloading, first install the Atmel AVR Studio and just follow all the default setting and secondly install the WinAVR, again follow the default setting.

2. My first AVR Project

To create your first AVR project go to Start -> All Programs -> Atmel AVR Tools -> AVR Studio 4, this will launch the AVR Studio 4 application and the Welcome to AVR Studio 4 form appear, click on the New Project button and it will show the Create new project screen as follow:

avrvs_01

On Project Type choose AVR GCC, enter myfirstc for the project name and you could also change the project location directory as you like,  then click on the Next>> button, you will be asked for debugging platform on the following screen:

avrvs_02

Choose AVR Simulator for debug platform and ATmega168 for the device, then click on the Finish button. This will create all the necessary files and directories needed for this project. Now the AVR Studio 4 IDE is ready and display the empty myfirstc.c file.

avrvs_03

Enter or cut and paste this source program bellow to the program code windows:

/***************************************************************************
//  File Name    : myfirstc.c
//  Version      : 1.0
//  Description  : Learning Lessons 1: Hello World
//  Author(s)    : RWB
//  Target(s)    : AVRJazz Mega168 Board
//  Compiler     : AVR-GCC 4.3.0; avr-libc 1.6.2 (WinAVR 20080610)
//  IDE          : Atmel AVR Studio 1.14
//  Last Updated : 21 March 2008
//***************************************************************************
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
    DDRD=0xFF;                   // Set all the PORTD bit (8 bit) for Output
    for(;;) {                    // Loop Forever
      PORTD=0xFF;                // Turn On all the PORT D bit
      _delay_ms(100);            // Delay 100 millisecond
      PORTD=0x00;                // Turn Off all the PORT D bit
      _delay_ms(100);            // Delay 100 millisecond
    }
    return 0;                    // Standard Return Code
}

The program start with the standard comment (all the statement start with double slash is consider as comment in standard C syntax) that give the information about the program, version and compiler version, etc. The next two include statement tell the C compiler to include the AVR microcontroller specific library <avr/io.h> and the delay library <util/delay.h> when compiling the program.

Inside the main program, first we instruct the AVR microcontroller (i.e. Mega168) to enable it’s 8 bit PORT-D input/output port for output by setting the data direction register (DDRD) on PORT-D to 0xFF.

Next inside the blank for loops statement (it’s mean we loop continuously) we simple instruct the AVR microcontroller to turn on and off all it’s 8 bit PORT-D by assigning the value 0xFF (on) and 0x00 (off) to the PORT-D register (PORTD). The 100 millisecond delay is put there so our eye could catch the process, otherwise the process is so fast that we see the port is always on.

3. Building the HEX Files

Before compiling and building the HEX files we have to tell the AVR Studio about our board device, by clicking the menu Project -> Configuration Options the Configuration options form will appear as the following picture:

avrvs_04

Make sure the device selected is atmega168 and the frequency is set to 11059200 hz (this is the AVRJazz Mega168 board default frequency). Leave the other setting to it’s default then click the Ok button.

The next step is to building the program by clicking the menu Build -> Rebuild All, if there is no error the myfirstc.hex file will be produced and the status message will show Build succeeded with 0 Warning:

avrvs_05

If there are error messages, check and recheck again your C code. Most often, they are caused by some typos or syntax errors. In our next tutorial we will show you how to debug the code and download it to the AVRJazz Mega168 Board.

16 thoughts on “Starting Atmel AVR C Programming Tutorial 1

  1. @ taher

    we need to provide MCU processing frequency say 16 MHZ or 4 MHZ according to the processor.
    Include the following command to avoid warning msg;
    #define F_CPU 4000000UL if using ATMEL168

  2. hello every one, previously i was using bascom, so i am very new to AVR studio. on compiling the above code, i am getting the following errors:
    ../ports.c:5: error: ‘DDRD’ undeclared (first use in this function)
    ../ports.c:5: error: (Each undeclared identifier is reported only once
    ../ports.c:5: error: for each function it appears in.)
    ../ports.c:7: error: ‘PORTD’ undeclared (first use in this function)
    ../ports.c:8: warning: implicit declaration of function ‘_delay_ms’
    make: *** [ports.o] Error 1
    Build failed with 6 errors and 1 warnings…

    please suggest

Leave a reply to Vishnu Mawandia Cancel reply