Previous: Installing the GNU tools (avr-gcc) for AVRs
Next: Setting up the AvrUsb500 and avrdude

AVR Toggle LED: Test Program for AvrUsb500

February 24th, 2006

This is just a quick, simple little program for an ATMega8 (can easily be adapted to any AVR) that I've setup to test the installation of avr-gcc and the GNU toolchain, avrdude, and my AvrUsb500 USB programmer from tuxgraphics.org.

This article is written with that in mind. If you aren't either getting started with avr-gcc or the AvrUsb500 programmer, this information might not be all that interesting to you-- that is, unless you're new to AVR programming and couldn't find any of the other hundreds of thousands of LED toggling examples out on the internet. I'm not going to discuss the code or circuit in detail in this article.

The Test Circuit

Below is the schematic of the test circuit as well as a photo of the circuit in action on my breadboard (I pay the extra money for blue LEDs because they're so damn sexy). The LED connected to PC0 of the ATMega8 simple blinks on and off. The schematic shows the connector for the AvrUsb500 and there is a table below which shows the color codes for the AvrUsb500 programmer cable. This allows us to program the chip while it's in the circuit (on the breadboard). When we do program the chip, we'll simply issue the avrdude command and the LED should start to blink.

Schematic of toggle_led.c circuit
[click to see full size]

toggle_led.c circuit on breadboard
[click to see full size]

AvrUsb500 Programmer Cable
Grey GND
Brown SCK
White MISO
Yellow MOSI
Green RESET

The Test Program

The file toggle_led.c contains a very basic program which toggles the LED on Port C of an ATMega8 AVR. Now, the point of this article isn't to teach C programming by any means. It's to test the installation of avr-gcc and the programmer. So, the program should be pretty self-explanitory. However, if you are new to developing C code for microcontrollers, here's a couple of important notes.

  1. The statement #define F_CPU 1000000UL specifies the clock frequency of the target microcontroller. This is necessary so that the delay routine from util/delay.h knows how long each instruction takes. In this case, it's based on the ATMega8's factory default setting of an internal RC oscillator at 1MHz.
  2. You'll see code on the internet quite often that uses sbi and cbi macros to set and clear bits respectively. This is an obsolete method. You should instead use standard C methods to set and clear bits.
  3. The _BV macro is "bit value" and is described in the avr-libc documentation.

I'll likely be discussing topics related to programming in C for AVRs in a later article.

Compiling the Program with GCC

So, we wanted to test the installation of the gnu compiler (and utilities), the binary utilities, and the avr-libc C library. These installations were described in my article: Installing the GNU tools for AVRs.

Typically, when developing our AVR programs with the GNU toolchain, we use a Makefile to build our project. However, since we're just getting started here, we're going to manually enter the commands to compile our code.

avr-gcc -mmcu=atmega8 -Wall -o toggle_led.elf toggle_led.c
avr-objcopy -j .text -O ihex toggle_led.elf toggle_led.hex

The first command we're issuing, avr-gcc, is the GNU C Compiler for AVR's. This will output a binary file in ELF format. The -Wall option turns on "Show all warnings" and the -mmcu=atmega8 specifies the target microprocessor.

The next command, avr-objcopy, creates an Intel HEX format file from the ELF format file. The resulting file, toggle_led.hex, is the file we'll upload to the AVR microcontroller.

Uploading the Program with avrdude

If you're using the AvrUsb500 programmer, you should have installed avrdude and edited the avrdude.conf file as instructed in my article: Setting up the AvrUsb500 and avrdude (or use the CDROM that comes with the AvrUsb500). To "burn" the toggle_led.hex program into the ATMega8 chip, we issue the avrdude command as follows:

avrdude -p m8 -c avrusb500 -e -U flash:w:toggle_led.hex

You specify the AvrUsb500 programmer with the -c avrusb500 opeion (assuming you configured your avrdude.conf file as such). I'm not going to discuss using avrdude in detail. Again, I'm just verifying that my Linux AVR development system is up and running. Avrdude should output something similar to that shown below (the circuit is powered on):

avrdude: AVR device initialized and ready to accept instructions

Reading | ################################################## | 100% 0.05s

avrdude: Device signature = 0x1e9307
avrdude: erasing chip
avrdude: reading input file "toggle_led.hex"
avrdude: input file toggle_led.hex auto detected as Intel Hex
avrdude: writing flash (3486 bytes):

Writing | ################################################## | 100% 2.00s

avrdude: 3486 bytes of flash written
avrdude: verifying flash memory against toggle_led.hex:
avrdude: load data flash data from input file toggle_led.hex:
avrdude: input file toggle_led.hex auto detected as Intel Hex
avrdude: input file toggle_led.hex contains 3486 bytes
avrdude: reading on-chip flash data:

Reading | ################################################## | 100% 1.19s

avrdude: verifying ...
avrdude: 3486 bytes of flash verified

avrdude: safemode: Fuses OK

avrdude done. Thank you.

With that, the little LED starts blinking it's little heart out and I clap my hands and try to explain to my roomate why a blinking blue light is so monumental.



Categories
AVR Microcontrollers Robotics/Electronics

Related Posts


Technorati Tags

Leave a Reply