Tuesday, April 8, 2008

avr-as Usage Tutorial

It's very embarrassing to know that most of AVR newbies in Linux get stuck with the avr-assembler / avr-studio (which is purely written for MS-Windows). Plus, it's more annoying that nobody seems care enough to write about how to use avr-as, as replacement of the atmel avr-assembler.

I'll show you a bit of how to use avr-as in Linux. I'm using Ubuntu 6.06 while writing this blog and doing my experiment with avr-as.

# Installing avr-as
avr-as is part of avr-binutils package, and if you have installed avr-gcc in your Linux before it means you've installed this software too.

If you haven't install avr-gcc, these are the links to help you:
  • Building AVR GCC The process is applicable for most of Linux distribution.
  • For debian / ubuntu:
    • apt-get install avr-binutils avr-gcc avr-libc avrdude
  • For SLED 10 / OpenSUSE:
    • Use Yast to find avr-gcc. For SLED 10, you need to install OpenSUSE repo first
# avr-as Demo Program
The demo is simple: Set PORTB0 mode to output, and set its value to HIGH. Here's the demo program:

.equ PORTB,0x18
.equ DDRB, 0x17

.org 0x00
reset:
rjmp main;
rjmp defaultInt;
rjmp defaultInt;
rjmp defaultInt;
rjmp defaultInt;
rjmp defaultInt;
rjmp defaultInt;
rjmp defaultInt;
rjmp defaultInt;
rjmp defaultInt;
rjmp defaultInt;
rjmp defaultInt;
rjmp defaultInt;
rjmp defaultInt;
rjmp defaultInt;

defaultInt:
reti;

main:
sbi DDRB, 0;
cbi DDRB, 0;

end:
rjmp end;


The .equ syntax assign PORTB and DDRB to 0x18 and 0x17. The syntax makes the code easier to read. The rest of the program is the standard avr assembly. You can view the datasheet for the complete assembly syntax and its meaning.

# Compiling Demo Program
  1. Save into *.s file (in this demo we call it: simpleIo.s)
  2. Assembly the file using:
    avr-as -mmcu=attiny15 -o simpleIo.o simpleIo.s
  3. Link the file using:
    avr-ld -o simpleIo.elf simpleIo.o
# Using Demo Program
  1. Generate hex file:
    avr-objcopy --output-target=ihex simpleIo.elf simpleIo.ihex
  2. Program the hex file

No comments: