ABOUT PROJECTS RESUME
avr-gcc

This page describes how to program an Atmega8 with avr-gcc on MacOSX. The programmer I use is from tuxgraphics.org but any stk500 (version 2) programmer will work.

Installing the avr-gcc tool chain can be tricky. The easy way out is to install the Arduino programming IDE and use the binaries that are packaged with it. Also note that if you rather not use command line tools you might want to stop reading and exclusively use Arduino. It is a very nice platform for beginners.

There are three steps to make this work.

  1. Install the Arduino IDE (http://arduino.cc)
  2. Install the FTDI drivers (see Arduino documentation)
  3. Copy and edit the <arduino>/tools/avr/etc/avrdude.conf
    1. set default_serial = "/dev/tty.usbserial-0000101D"; where "/dev/tty.usbserial-0000101D" is the name under which the usb programmer shows up in /dev
    2. save the file in: /usr/local/etc/avrdude.conf

EXAMPLE PROJECT

  1. Creade a new directory.
  2. Copy the following Makefile to it.
  3. Create a blink.c file with the following content.
  4. Say "make"
  5. Conect your programmer to the USB port and to the Atmega8.
  6. Say "make load"
  7. Watch the LED blink.

TROUBLESHOOTING

  • Edit the Makefile and make sure the FILE variable at the top is assigned with the name as the c-file you are trying to compile. Also check the ARDUINO_TOOL variable.
  • If the "make" command cannot be found install the
  • Apple Deeveloper Tools

Makefile

FILE=blink
#MCU=atmega8
MCU=atmega168
ARDUINO_TOOLS=/Applications/Arduino.app/Contents/Resources/Java/hardware/tools/avr
CFLAGS=-g -mmcu=$(MCU) -Wall -Wstrict-prototypes -Os -mcall-prologues
#DUDEOPTS=-c usbtiny -p m8   #atmega8
DUDEOPTS=-c usbtiny -p m168  #atmega168
 
 
all: $(FILE).hex
 
help: 
	@echo "Say \"make\" to compile (create .hex file)."
	@echo "Say \"make load\" to transfer program to microcontroller."	
	@echo "Say \"make clean\" to delete all build files."	
 
$(FILE).hex : $(FILE).out 
	$(ARDUINO_TOOLS)/bin/avr-objcopy -R .eeprom -O ihex $(FILE).out $(FILE).hex 
$(FILE).out : $(FILE).o 
	$(ARDUINO_TOOLS)/bin/avr-gcc $(CFLAGS) -o $(FILE).out -Wl,-Map,$(FILE).map $(FILE).o 
$(FILE).o : $(FILE).c 
	$(ARDUINO_TOOLS)/bin/avr-gcc $(CFLAGS) -Os -c $(FILE).c
 
 
 
load: $(FILE).hex
	$(ARDUINO_TOOLS)/bin/avrdude $(DUDEOPTS) -C $(ARDUINO_TOOLS)/etc/avrdude.conf -e -U flash:w:$(FILE).hex
 
 
crystalexternal:
    #this is atmega8 specific
	$(ARDUINO_TOOLS)/bin/avrdude $(DUDEOPTS) -C $(ARDUINO_TOOLS)/etc/avrdude.conf -u -v -U lfuse:w:0xee:m
	$(ARDUINO_TOOLS)/bin/avrdude $(DUDEOPTS) -C $(ARDUINO_TOOLS)/etc/avrdude.conf -u -v -U hfuse:w:0xd9:m
 
crystalinternal:
    #this is atmega8 specific
	$(ARDUINO_TOOLS)/bin/avrdude $(DUDEOPTS) -C $(ARDUINO_TOOLS)/etc/avrdude.conf -u -v -U lfuse:w:0xe4:m
        # 0xe1=1Mhz; 0xe2=2Mhz; 0xe3=4Mhz; 0xe4=8Mhz;  
	$(ARDUINO_TOOLS)/bin/avrdude $(DUDEOPTS) -C $(ARDUINO_TOOLS)/etc/avrdude.conf -u -v -U hfuse:w:0xd9:m
 
clean:
	rm -f *.o *.map *.out *.hex

Blinking LED

/*
* Blinking LED
*
*/
 
#include <avr/io.h>
#include <inttypes.h>
#define F_CPU 1000000UL  // 1 MHz
#include <avr/delay.h>
 
 
void delay_ms(unsigned int ms)
{
	while(ms){
		_delay_ms(0.96);
		ms--;
	}
}
 
 
int main(void)
{
    //Init port C for output
    DDRC = 255;
 
    while (1) {
        //All pins on port C to high
        PORTC = 0;
        delay_ms(100);
        //All pins on port C to low
        PORTC = 255;
        delay_ms(1000);
    }
	  return(0);
}
Show pagesource Old revisions Backlinks Index Recent changes
avr-gcc.txt · Last modified: 2011/01/31 16:17 by stefan