AVR Toggle LED: Test Program for AvrUsb500
February 24, 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.
[click to see full size]
[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.- 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.
- 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.
- The _BV macro is "bit value" and is described in the avr-libc documentation.
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.hexThe 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.hexYou 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:
Robotics & Electronics
Copyright © 2004 - 2010 Micah Carrick. All Rights Reserved.
4 Comments about "AVR Toggle LED: Test Program for AvrUsb500"
RSS Feed
I also have a usbasp and to get around this error i just add the -F override. I'm also a noob so I'm not sure if this is the best option rather than addressing whats wrong. Final Command : avrdude -p m8 -c usbasp -e -F -U flash:w:toggle_led.hex and if that throws an error like...
avrdude: Warning: cannot query manufacturer for device: error sending control message: Operation not permitted
avrdude: error: could not find USB device "USBasp" with vid=0x16c0 pid=0x5dc
just sudo the command.
What's your opinion of the USBASP?
Cold you give me a few tips on how to resolve this avrdude problem?
Thanks, cwc
I'm using the USBASP AVR Programmer:
http://www.protostack.com/index.php?main_page=product_info&cPath=23&products_id=24&sid=a6a2aad57de8ad418ace540388dd719f
and I'm having trouble with my Linux and Windows XP Setup. I'm using Gentoo Linux and I believe I might not have things set up correctly. AVRDUDE runs fine.
This is my current error:
cwc@tma ~/avrdude $ avrdude -c usbasp -p atmega8 -U flash:w:test_leds.hex
avrdude: error: programm enable: target doesn't answer. 1
avrdude: initialization failed, rc=-1
Double check connections and try again, or use -F to override
this check
I eventually need to program the M8 for Sumobots. I'm at the Noob beginning stage with AVR.
Leave a Comment about "AVR Toggle LED: Test Program for AvrUsb500"