AVR microcontrollers has many categories or we will say family. Family basically a group or category based on similiar characteristic and properties. There are six family and fortunately, AVR microcontrollers have same architecture. Thus programming in one family can be guaranteed work on other family. It is only limited to physical limitation such as different amount of flash storage, etc. AVR’s merit is about ease of porting.
As one of widely used microcontroller, there are lot of communities in internet. We can easily find various options can while developing microcontroller project.
This article will be covering preparation and setting up environment in Linux to develop AVR program.
What you will need:
- A programmer (will be described later)
- AVR Microcontroller, with it’s development board if any, in this case I use AVR ATMega32
Program in Microcontroller
Before I start, let’s talk about program. What is program? A program is simply set of instructions written using machine language so it can be executed by machine. It is also called “binary” which mean to be not human-readable file (but can be understood by machine). For microcontrollers, binary files have type .hex (Intel Hex Format). You can imagine it as .exe in windows application. There are other forms, but in this article we will focus on .hex.
AVR has a small amount of memory in it. It is a storage where program is actually saved. And yes it is small compared by computer. Just some Kilobytes. When the chip starts up, microcontroller will start running whatever program written in flash.
Compiler and Cross Compiling
Compilation for microcontroller is basically similar to compiling program for PC. However, the difference between those two is in term of product of compilation. Compiler for AVR is run in our PC (developer machine), compile source code, and then produce machine language which is specific for the microcontroller (not our PC). This process is called cross compiling.
There are many cross compiler for AVR. I use AVR-GCC for this purpose (in this article and other similar articles, unless told otherwise).
Other tools we need are collectively known as avr-binutils. The binutils packages provide all the low-level utilities needed in building and manipulating object files. In this package, we have AVR assembler (
avr-as), linker (
avr-ld), and librarian (
avr-ranlib). In addition, we get tools which extract data from object files (
avr-objcopy), dissassemble object file information (
avr-objdump), and strip information from object files (
The last thing to mention is the libc or standard c library. This is library designed for AVR microcontroller, or in other word it is different with our library used for PC programing.
Check your package management for avr-gcc and avr-binutils and install them.
For Debian and Ubuntu based:
sudo apt-get install gcc-avr sudo apt-get install binutils-avr sudo apt-get install gdb-avr sudo apt-get install avr-libc
For Red Hat and Fedora based:
yum install avr-gcc yum install avr-binutils yum install avr-libc yum install avr-gdb
Now invoke these to test the tools:
Download / Burn the Program to the Chip
To program microcontroller we need a downloader. It is a special chip that can connect your microcontroller with our computer and write our program into our chip. Every AVR has a set of pins that are programming pins. You have to verify you chip pins and connect the programmer to those pins in the right order.
So what programmer can be used? In this article I used USBasp. Of course you can have other programmers, but I will only talk about this programmer. For information about USBasp, you can visit this link http://www.fischl.de/usbasp/
Now we have know our programmer (I mean USBasp) but then, how to write our program to microcontroller? Relax! We will cover now.
We will use avrdude, a small application to do that. Install it if you have not install.
For Debian and Ubuntu based:
sudo apt-get install avrdude
For Red Hat and Fedora based:
yum install avrdude
Let’s take a peek of avrdude by give this command on our terminal:
Let’s see what we have got. You will get something like this:
Usage: avrdude [options] Options: -p <partno> Required. Specify AVR device. -b <baudrate> Override RS-232 baud rate. -B <bitclock> Specify JTAG/STK500v2 bit clock period (us). -C <config-file> Specify location of configuration file. <strong>-c <programmer> Specify programmer type.</strong> -D Disable auto erase for flash memory -i <delay> ISP Clock Delay [in microseconds] -P <port> Specify connection port. -F Override invalid signature check. -e Perform a chip erase. -O Perform RC oscillator calibration (see AVR053). -U <memtype>:r|w|v:<filename>[:format] Memory operation specification. Multiple -U options are allowed, each request is performed in the order specified. -n Do not write anything to the device. -V Do not verify. -u Disable safemode, default when running from a script. -s Silent safemode operation, will not ask you if fuses should be changed back. -t Enter terminal mode. -E <exitspec>[,<exitspec>] List programmer exit specifications. -x <extended_param> Pass <extended_param> to programmer. -y Count # erase cycles in EEPROM. -Y <number> Initialize erase cycle # in EEPROM. -v Verbose output. -v -v for more. -q Quell progress output. -q -q for less. -? Display this usage. avrdude version 5.10, URL: <http://savannah.nongnu.org/projects/avrdude/>
Woah, that’s too much. But our interest is only in few options. I’ve bold the text which will get our attention. We only interests in -p, -U, and -c options as this is a minimum command we need to invoke along with avrdude.
- -c : We specify the programmer we type. We are using usbasp so can be replaced with usbasp. Of course if you are using other programmer you must specify. For a complete list about supported programmer, invoke command: avrdude -c asdf. Because asdf is not a valid programmer, avrdude would complaint and print out list of supported programmer to you. Read it and verify your programmer is supported.
- -U :r|w|v:[:format]: This is the most important command. is either flash or eeprom which is destination where our program will be stored. Next is r (read), w (write), or v (verify) option that you must choose one (only one). Next is which you have to replace with your program path and name. Next is optional format flag [:format] that specify our program format.
- -p : Specify microcontroller series. Or simply what microcontroller we willl program. For example I will use ATMega32 so i will replace with atmega32.
With these three, we can program in minimal. In this case when I want to program ATMega32 using USBasp with filename avr_leds.hex, the command I use will be:
avrdude -c usbasp -U flash:w:avr_leds.hex -p atmega32
Wait until process is finish and then you can see you microcontroller is programmed now.
Next time I will write some actual writing program for AVR.