How To Develop a Project for the AVR Microcontroller

If you're on Windows, WinAVR is a complete solution:

Write a Program

Type the program (in assembler or in C) in an editor of your choice:

  • Emacs
  • WinAVR Programmer's Notepad (CAUTIOUS! It sometimes inserts text without showing it - resulting in errors when trying to compile)

Build it

Compile and Assemble (.o File)

Feed the program through the assembler.

sudo aptitude install gcc-avr binutils-avr
sudo aptitude install avr-libc

If you work with avr-gcc (GCC) 4.1.2 (WinAVR 20070525)

CodeVisionAVR C Compiler for delayms. "Embedded C Programming and the Atmel AVR" by Barnett et. al. makes use of this tool.

Link into a Binary (.out File)

For the curious, "avr-objdump -S" disassembles the binary file and intersperses the source code in the output!

avr-objdump -S demo.out

Generate a .hex File

We have a binary of the application, but how do we get it into the processor? Most (if not all) programmers will not accept a GNU executable as an input file, so we need to do a little more processing.

The next step is to extract portions of the binary and save the information into Intel "hex" files (load files for Flash = Program Memory). The GNU utility that does this is called avr-objcopy.

The ROM contents can be pulled from our project's binary and put into the file rom.hex using the following command:

avr-objcopy -j .text -O ihex demo.out rom.hex

Program the Device

(Re-)Load the .hex file into an ATmega microcontroller on the development board.

sudo aptitude install avrdude

In AVR Studio, "Open" the .hex file. Then, save the Project File (.aps). Select the correct device, and click on "Finish". Select "Tools" -> "Program AVR" -> "Connect", and click on "Connect". Input HEX File into Flash, and ELF File.

XXX "Cal. Internal RC Oscillator - 4 MHz"

  • Flash
  • EEPROM
  • Lock bits
  • Fuse bits

Option: "Verify Chip Writes"

Debug

sudo aptitude install uisp
sudo aptitude install avr-gdb

Automate with a Makefile

Rather than type these commands over and over, they can all be placed in a Makefile.

Makefile maker:

  • WinAVR MFile

Change:

  • "Main file name…" (source of misunderstandings if not correctly set)
  • "MCU"

To build the demo project using make:

make all