Sample code and its Makefile for Arduino Uno using the AVR library
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <stdint.h> | |
#include <avr/io.h> | |
#include <util/delay.h> | |
#define LED_DDR DDRB | |
#define LED_PIN PINB | |
#define LED PB5 | |
int main(void) | |
{ | |
LED_DDR |= _BV(LED); | |
while (1) | |
{ | |
LED_PIN |= _BV(LED); | |
_delay_ms(50); | |
} | |
return(0); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
MCU = atmega328p | |
TARGET_ARCH = -mmcu=$(MCU) | |
TARGET = main | |
CC = avr-gcc | |
CPPFLAGS = -mmcu=$(MCU) | |
CFLAGS = -Os -g -Wall -I. -DF_CPU=16000000 | |
LDFLAGS = -g -mmcu=$(MCU) -lm -Wl,--gc-sections -Os | |
PGMER = -c arduino -b 115200 -P /dev/ttyACM0 | |
PGMERISP = -c avrispv2 -P /dev/ttyACM0 | |
DUDE = /usr/bin/avrdude -V -p $(MCU) | |
C_SRCS = $(wildcard *.c) | |
OBJ_FILES = $(C_SRCS:.c=.o) | |
all: $(TARGET).hex | |
clean: | |
rm -f $(TARGET).elf *.o *.hex | |
%.o: %.c | |
$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@ | |
$(TARGET).elf: $(OBJ_FILES) | |
$(CC) $(LDFLAGS) -o $@ $(OBJ_FILES) | |
$(TARGET).hex: $(TARGET).elf | |
avr-objcopy -j .text -j .data -O ihex $(TARGET).elf $(TARGET).hex | |
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" --change-section-lma .eeprom=0 -O ihex main.elf eeprom.hex | |
upload: $(TARGET).hex | |
$(DUDE) $(PGMER) -U flash:w:main.hex | |
size: main.elf | |
avr-size --format=avr --mcu=$(MCU) main.elf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment