Search This Blog

Sunday, January 25, 2009

Linux Atmel AVR Tutorial


The Atmel AVR family is a set of powerful microcontrollers that were designed to be used with the C programming language. You can now compile a C program for the AVRs with gcc, then load the program onto the chip using an inexpensive programmer and free tools. For Linux users who want to work on digital electronics, robotics, and automation, these chips are the way to go. This page is a step-by-step tutorial that presents the simplest, cheapest way to start experimenting with AVRs.

Specifically, on this page you'll find instructions for assembling and programming the AVR ATmega32 using a AVR-P40-USB prototyping board from Olimex (available at Sparkfun) and an AVR programmer that can be purchased from Digi-key. The ability to install programs on your Linux computer is the only skill required. It will cost you about $60 and 50M of disk space, and you'll be up and running about three hours after you unpack the electronics.
1. Order electronic parts

Order the AVR-P40-USB from Sparkfun; buy it with the USB extension cable - this will be used for power and serial communication (total price: $37.50). Also order the ATAVRISP-ND programmer for $29 from Digi-key. As long as you are putting in Digi-key order, you'll want to get some miscellaneous electronics for future experimentation. That is however beyond the scope of this tutorial.
picture of assembly
2. Plug in

After familiarizing yourself with your new electronics, attach the rubber feet to the prototyping board, connect the usb cable to your computer, and attach the programmer via serial port as shown. The LED on the programmer will cycle through several colors and stop on green if everything is okay.
3. Install software

You'll need a computer running linux to get gcc running; you could probably also set this stuff up under cygwin. I used Slackware 10, but the same instructions should work on any modern linux system.

Get binutils-2.16 from Untar the archive and make a separate directory in which to build it. From this directory, install with

../binutils-2.16/configure --target=avr --prefix=/usr/local/atmel
make install

Get gcc-core-4.0.1 from Again make a separate directory and install with

export PATH=/usr/local/atmel/bin:$PATH
../gcc-4.0.1/configure --target=avr --prefix=/usr/local/atmel --enable-languages=c
make install

Get the latest avr-libc.

./configure --build=`./config.guess` --host=avr --prefix=/usr/local/atmel
make install

Get the latest Uisp, the program you will use to access the AVRISP. Install with

make install

Try a simple program

Save this code as blink.c:

#define F_CPU 10000000UL

void delayms(uint16_t millis) {
uint16_t loop;
while ( millis ) {

int main(void) {
DDRB |= 1< while(1) {
PORTB &= ~(1< delayms(100);
PORTB |= 1< delayms(900);
return 0;

And save this as Makefile:

CFLAGS=-g -Os -Wall -mcall-prologues -mmcu=atmega32

program : $(TARGET).hex
$(UISP) -dprog=stk500 -dserial=/dev/ttyS1 --erase -dpart=atmega32
$(UISP) -dprog=stk500 -dserial=/dev/ttyS1 --upload -dpart=atmega32 \
if=$(TARGET).hex -v=2
%.obj : %.o
$(CC) $(CFLAGS) $< -o $@

%.hex : %.obj
$(OBJ2HEX) -R .eeprom -O ihex $< $@

clean :
rm -f *.hex *.obj *.o

With the programmer plugged in, type make. Your LED should now start to blink!
Further reading

* The avr-gcc homepage contains essential documentation on the software base you need to use to write programs for the ATmega32. This is where to look for the definition of predefined functions like sbi.
* The offical Atmel page on ATmega32 has the complete manual.
* The gcc documentation has lots of help on compiler options.
* The AVR Freaks page on ATmega32 links to lots of information about the chip and related software and hardware.

1 comment:

Anonymous said...