Using the Arduino Mega 2560 as a regular MCU with avrdude


There's a number of reason why you'd want to program your Arduino device as a regular AVR MCU. For me it was having to do some tests I'd later have to run on a normal AVR board and only having an Arduino board available. Turns out the built-in STK500 ISP on the Mega 2560 is supported by avrdude.

The whole compiling/flashing shebang can be done with the following Makefile (slightly modified from the one found here).

You'll probably have to modify the DEVICE variable if you're using something other than OSX and the SOURCES one if you're using C++ instead of C.

NAME := main
HEX := $(NAME).hex
OUT := $(NAME).out
MAP := $(NAME).map
SOURCES := $(wildcard *.c)
HEADERS := $(wildcard *.h)
OBJECTS := $(patsubst %.c,%.o,$(SOURCES))

MCU := atmega2560
MCU_AVRDUDE := m2560
PARTNO := stk500v2
DEVICE := /dev/tty.usbmodem*
BAUDRATE := 115200
AVRDUDE_FLAGS := -F -V -D -v

CC := avr-gcc
OBJCOPY := avr-objcopy
SIZE := avr-size -A

CFLAGS := -Wall -pedantic -mmcu=$(MCU) -std=c99 -g -Os -DF_CPU=16000000UL

all: $(HEX)

clean:
    rm -f $(HEX) $(OUT) $(MAP) $(OBJECTS)

flash: $(HEX)
    avrdude $(AVRDUDE_FLAGS) -c $(PARTNO) -p $(MCU_AVRDUDE) -P $(DEVICE) -b $(BAUDRATE) -U flash:w:$(HEX)

$(HEX): $(OUT)
    $(OBJCOPY) -R .eeprom -O ihex $< $@

$(OUT): $(OBJECTS)
    $(CC) $(CFLAGS) -o $@ -Wl,-Map,$(MAP) $^
    @echo
    @$(SIZE) $@
    @echo

%.o: %.c $(HEADERS)
    $(CC) $(CFLAGS) -c -o $@ $<

%.pp: %.c
    $(CC) $(CFLAGS) -E -o $@ $<

%.ppo: %.c
    $(CC) $(CFLAGS) -E $<

With the Makefile in the same folder as your main.c just run:

make
make flash

And you should have you program running in the Arduino.

To know the pin mappings from the board to the actual ATMega2560 pins, you can check this useful resource.