Skip to content

Instantly share code, notes, and snippets.

@gordonjcp
Last active August 21, 2023 16:11
Show Gist options
  • Save gordonjcp/8158896 to your computer and use it in GitHub Desktop.
Save gordonjcp/8158896 to your computer and use it in GitHub Desktop.
How to build for arduino with avr-gcc using a Makefile
# simple makefile for avr-gcc projects
# what is the final binary called
PROGRAM = ddsvfo
# flags to pass to the C compiler
# -mmcu should be set to the CPU type
# -DF_CPU should be the clock speed in Hz
# you can add additional -D statements which work just like #define in the code
CFLAGS = -Wall -I. -g -Os -mmcu=atmega168 -DF_CPU=16000000
# Any other files that aren't C source, that trigger a rebuild
DEPS = lcd.h
# These are the object files that gcc will create, from your .c files
# you need one for each of your C source files
OBJ = vfo.o lcd.o analogue.o dds.o
# magic happens below here
# "make all" creates a burnable hex file
all: $(PROGRAM).hex
# this turns the .elf binary into .hex for avrdude
$(PROGRAM).hex: $(PROGRAM).elf
avr-objcopy -O ihex $< $@
# this builds and links the .o files into a .elf binary
$(PROGRAM).elf: $(OBJ)
avr-gcc $(CFLAGS) -o $@ $^
# this compiles the .c files into .o files
%.o: %.c $(DEPS)
avr-gcc $(CFLAGS) -o $@ -c $<
# this calls the first macro we defined to create a .hex file
# then it runs avrdude to burn it to your Arduino
burn: $(PROGRAM).hex
avrdude -F -V -c arduino -p ATMEGA168 -P /dev/ttyUSB0 -b 19200 -U flash:w:$(PROGRAM).hex
clean:
rm *.o
rm *.elf
rm *.hex
// vim: set noexpandtab ai ts=4 sw=4 tw=4:
// vfo.c
// part of ddsvfo Copyright (C) 2013 Gordon JC Pearce MM0YEQ
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <avr/io.h>
#include <avr/interrupt.h>
// that's all you're getting. You probably need at least avr/io.h for the pins to work
// as if by magic, -mmcu=atmega168 causes the includes to give you the correct definitions for that chip
// the vim modeline at the top is not important, it's just the way I like to use gedit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment