AVR Programming in Ubuntu

To begin with, ensure that you have the compiler and programmer installed and updated. In case you do not have a grasp of Linux commands, a good starting point for Linux basics is here.

Lets quickly see which AVR packages are listed in the package manager:

kabhijit@edcn46:~/Documents/ADC-TEST/default$ dpkg -l | grep avr

ii avr-libc 1:1.6.2.cvs20080610-2 Standard C library for Atmel AVR development

ii avrdude 5.5-7 software for programming Atmel AVR microcont

ii avrp 1.0beta3-7 Programmer for Atmel AVR microcontrollers

ii avrprog 0.2.2-2 Programmer for Atmel AVR microcontrollers

ii binutils-avr 2.18-4ubuntu1 Binary utilities supporting Atmel's AVR targ

ii gcc-avr 1:4.3.2-1 The GNU C compiler (cross compiler for avr)

ii gdb-avr 6.4.90.dfsg-2.1 The GNU Debugger for avr

ii simulavr 0.1.2.2-6.1 Atmel AVR simulator

kabhijit@edcn46:~/Documents/ADC-TEST/default$ sudo apt-get install gcc-avr

In the above command dpkg is the command for the package manager. The -l switch invokes a list of all packages. "grep avr" filters the entire list given out by dpkg and prints only the entries having 'avr'. At the moment we are interested in installing gcc-avr (the compiler) and avrdude (the programmer), which is done with the following commands:

sudo apt-get install gcc-avr

sudo apt-get install avrdude

Now create a basic piece of code.

In any text editor, add the following code and save it as main.c

#include <avr\io.h>

/*This header file is needed for the compiler to understand the register and bit names and device specific assignments, such as ports and pins*/

int main()

/*Traditionally, a main program running on micro-controller will never end. It must go on running in an infinite loop. As such, it is immaterial what the return type of main is, but Linux based compilers, like the GCC, complain if it is not 'int'*/

{

MCUCSR |= 0x80;

MCUCSR |= 0x80;

/*The MCUCSR is the MCU Control and Status Register. The most significant bit is the JTAG disable bit(JTD). Since the JTAG hardware shares pins on PORTC, it needs to be disabled before we can use all PORTC pins.*/

DDRC = 0xff;

/*The Data Direction Register (DDR) is an 8 bit register that controls the direction of data flow on each pin of a given port. Thus there are 4 DDR registers - DDRA through DDRD for ports A, B, C and D. A value of '1' in each bit of the data direction register. Setting 0xff, therefore, causes the entire port to be set as output.*/

PORTC = 0xff;

/*The PORT register sets the actual value (level) on the PORT pins. Each of the 8 bits in this register correspond to one pin on the respective port. A value of 0xFF sets all the pins high, and will cause both the LEDs to glow.*/

while(1);

}

Save and close it.

Now, in a terminal, go to the directory where you stored this file and compile it using the following command:

avr-gcc -Wall -O2 -mmcu=atmega16 -Wl,-Map, main.map -o main main.c

avr-objcopy -j .text -j .data -O ihex main main.hex

avr-objdump -S main > main.lst

If all works well, and there are no compilation errors, you will get a hex file at the end. Instead of painfully typing out all these commands, you may want to save the following as a batch file (say compile.bat) [1].

avr-gcc -Wall -O2 -mmcu=atmega16 -Wl,-Map, $1.map -o $1 $1.c

avr-objcopy -j .text -j .data -O ihex $1 $1.hex

avr-objdump -S $1 > $1.lst

Save this file in your project directory, and to compile, simply use the command ./compile.bat main

Note that this scheme works well for single files only - where there is just one source file and perhaps a few header files. In case of even a modest project where there are two or more C source files, one would have to run the compiler manually a ridiculous number of times to ensure that everything were up to date. To avoid this, makefiles and make utilities should be used, as described later.

Now to load the program:

Connect the cable and power up the board. In a terminal, run the following: [2]

avrdude -c dapa -p m32 -U flash:w:main.hex

This will flash the hex file that has just been compiled onto the chip.

[1] This Batch file is from the Phoenix project of IUAC, New Delhi - http://www.iuac.res.in/~elab/phoenix/

[2] more detailed explanation of avrdude and its working is on the AVR Programming in Windows page