Quick start guide to programming AVR in Ubuntu
Archived post from 2010, recovered from my old blog. The technology described here is probably out of date. Original at the Internet Archive.
This is a quick introduction to programming AVR microcontrollers in Ubuntu for inpatients. If you just got a brand new programmer, a couple of AVR chips and don’t want to study tons of documents, books and man pages then you’re in the right place.
Programmer
First of all you need one of the programmers supported by avrdude for example STK500, STK600, JTAG IC just to name a few. Refer *man page*for a complete list.
Software packages
Install Ubuntu packages required for building modules and programming AVR devices:
sudo apt-get install avrdude avr-libc
Avrdude is used for programming AVR devices.
AVR-LIBC is a great C library coverting lots of common low level functions.
Configure avrdude
To simplify avrdude command create ~/.avrduderc file describing your programmer and a port it is connected to, for example:
default_programmer = “stk500v2″;
default_serial = “/dev/ttyUSB0″;
Write your program
Create a source file myprogram.c:
1
2
3
4
5
6
#include
int main(void) {
DDRD = 0x01;
PORTD = 0x01;
while (1) { /* do nothing */ }
}
The program makes a first pin of port D an output (by writing to Data Direction Register D) and sets logical one on that output. I’m sure you’ll be far more creative.
Compile
Compile the program into ELF file and then covert it to Intel HEX file:
avr-gcc -mmcu=atmega8 -Wall myprogram.c -o myprogram.o
avr-objcopy -j .text myprogram.o myprogram.hex
Once you know how it works you’ll probably write your own Make file or download an example from the Internet to speed up your development.
Program
Write a HEX file into a device:
avrdude -p m8 -U flash:w:myprogram.hex
The command tells your programmer that myprogram.hex should be written into your ATmega8 device connected to your programmer. It’s a good idea to let Make handle this task as well.
Now when you know the tool chain you’re ready to start your development!
References: