Skip to content

Commit 1dbe9fe

Browse files
authored
Minimal sketch in C with features and Makefile
1 parent 65ad698 commit 1dbe9fe

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* ©arteknix : template for avr programming
2+
list of includes you might consider
3+
tow functions for illustration
4+
initLed and toggleLed
5+
this sketch needs avr/io.h for pins
6+
and util/delay.h for _delay_ms
7+
NB: normally you would use interrupt of TIMER overflow.
8+
*/
9+
//#include <stdint.h>
10+
//#include <stdlib.h>
11+
//#include <stdio.h>
12+
//#include <avr/interrupt.h>
13+
#include <avr/io.h>
14+
//#include <avr/pgmspace.h>
15+
//#include <inttypes.h>
16+
#define F_CPU 16000000
17+
#include <util/delay.h>
18+
19+
#define LED_PIN PB5 //PIN
20+
#define LED_PORT PORTB //PORT
21+
#define LED_DDR DDRB //IO config
22+
//======== FUNCTIONS ===================
23+
void initLed(){
24+
LED_DDR |= _BV(LED_PIN); // LED_PIN as OUTPUT
25+
}
26+
void toggleLed(){
27+
LED_PORT ^= _BV(LED_PIN); // invert LED_PIN
28+
}
29+
//======== MAIN ========================
30+
int main(void){
31+
initLed();
32+
while(1){
33+
toggleLed();
34+
_delay_ms(300);
35+
toggleLed();
36+
_delay_ms(700);
37+
}
38+
return 0;
39+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# @arteknix: Atmel AVR: compile - build - upload - list symbols
2+
# rather complete MAKEFILE for AVR using debian Linux file paths
3+
MCU = atmega328p #avr-gcc and AVRdude
4+
PART = m328p #AVRdude alternative
5+
TARGET_ARCH = -mmcu=$(MCU)
6+
TARGET = blink
7+
CC = avr-gcc
8+
CPPFLAGS = -mmcu=$(MCU)
9+
CFLAGS = -Os -g -Wall -I./includes, -DF_CPU=16000000
10+
LDFLAGS = -g -mmcu=$(MCU) -lm -Wl,--gc-sections -Os
11+
#PRGMER = -c wiring -b 115200 -P /dev/ttyUSB0
12+
PRGMER = -c arduino -b 115200 -P /dev/ttyACM1
13+
PRGMERISP = -c avrispv2 -P /dev/ttyUSB0
14+
#DUDE = /usr/bin/avrdude -v -V -p $(MCU)
15+
DUDE = /usr/bin/avrdude -V -p $(MCU)
16+
17+
C_SRCS = $(wildcard *.c)
18+
OBJ_FILES = $(C_SRCS:.c=.o)
19+
20+
all: $(TARGET).hex
21+
22+
clean:
23+
rm -f $(TARGET).elf *.o *.hex
24+
@echo $@ done
25+
26+
%.o: %.c
27+
@echo $@
28+
@echo $<
29+
$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@
30+
31+
$(TARGET).elf: $(OBJ_FILES)
32+
$(CC) $(LDFLAGS) -o $@ $(OBJ_FILES)
33+
34+
$(TARGET).hex: $(TARGET).elf
35+
avr-objcopy -j .text -j .data -O ihex $(TARGET).elf $(TARGET).hex
36+
avr-objcopy -j .eeprom --set-section-flags=.eeprom="alloc,load" \
37+
--change-section-lma .eeprom=0 -O ihex $(TARGET).elf eeprom.hex
38+
39+
upload: $(TARGET).hex size
40+
$(DUDE) $(PRGMER) -i 8 -U flash:w:$(TARGET).hex
41+
42+
size: $(TARGET).elf
43+
avr-size -C --mcu=$(MCU) $(TARGET).elf
44+
45+
sizeofhex: $(TARGET).hex
46+
avr-size $(TARGET).hex
47+
48+
symbols: $(TARGET).elf
49+
@echo "\n---" headers and symbols for $< "---\n"
50+
avr-readelf -hs $(TARGET).elf | less

0 commit comments

Comments
 (0)