From cd8984fa29a0059605512402e876f70d615d9227 Mon Sep 17 00:00:00 2001 From: Maix0 <39835848+Maix0@users.noreply.github.com> Date: Mon, 13 Apr 2026 14:19:59 +0200 Subject: [PATCH] feat(ex03): full ex03 --- ex03/Makefile | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++ ex03/main.c | 27 ++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 ex03/Makefile create mode 100644 ex03/main.c diff --git a/ex03/Makefile b/ex03/Makefile new file mode 100644 index 0000000..d0f7e1f --- /dev/null +++ b/ex03/Makefile @@ -0,0 +1,53 @@ +AVR_LIBC_DIR=/tmp/avr-libc + +# Makefile +MCU=atmega328p +F_CPU=16000000 +CC=avr-gcc +OBJCOPY=avr-objcopy +CFLAGS=-nostdlib -std=c99 -Wall -Wextra -g -Os -mmcu=$(MCU) -DF_CPU=$(F_CPU) -I. -I$(AVR_LIBC_DIR)/include +TARGET=main +SERIAL=-P /dev/ttyUSB0 -b 115200 + + +SRC_DIR=. +OBJ_DIR=build + +SRC_FILES=main.c +OBJ_FILES=$(patsubst %.c,%.o,$(SRC_FILES)) + +SRC=$(addprefix $(SRC_DIR)/,$(SRC_FILES)) +OBJ=$(addprefix $(OBJ_DIR)/,$(OBJ_FILES)) + +all: flash + +re: fclean all + +fclean: clean +clean: + rm -rf $(OBJ_DIR) + rm -f $(TARGET).hex + +hex: $(TARGET).hex + +flash: hex + avrdude -p $(MCU) -c arduino -U flash:w:$(TARGET).hex:i $(SERIAL) + +$(OBJ_DIR)/$(TARGET).bin: $(OBJ) + $(CC) $(CFLAGS) $(OBJ) -o $@ + +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c $(AVR_LIBC_DIR)/.CACHETAG + mkdir -p $(shell dirname $@) + $(CC) $(CFLAGS) -c $< -o $@ + + +$(TARGET).hex: $(OBJ_DIR)/$(TARGET).bin + $(OBJCOPY) -j .text -j .data -O ihex $< $@ + +$(AVR_LIBC_DIR)/.CACHETAG: + rm -rf $(AVR_LIBC_DIR) + git clone --depth=1 https://github.com/avrdudes/avr-libc/ $(AVR_LIBC_DIR) + cd $(AVR_LIBC_DIR) && ./bootstrap + cd $(AVR_LIBC_DIR) && ./configure --build=`./config.guess` --host=avr + cd $(AVR_LIBC_DIR) && make -j + touch $(AVR_LIBC_DIR)/.CACHETAG diff --git a/ex03/main.c b/ex03/main.c new file mode 100644 index 0000000..fab1d0b --- /dev/null +++ b/ex03/main.c @@ -0,0 +1,27 @@ +#include +#include + +#include + +#define LED_PORT PORTB0 +#define BTN_PORT PIND2 + +int main(void) { + // set pin to output + DDRB |= (_BV(LED_PORT)); + + bool led_state = false; + bool prev = (PIND & _BV(BTN_PORT)); + while (true) { + bool cur = (PIND & _BV(BTN_PORT)); + if (prev && !cur) { + led_state = !led_state; + if (led_state) + PORTB |= (_BV(LED_PORT)); + else + PORTB &= ~(_BV(LED_PORT)); + } + prev = cur; + _delay_ms(20.f); + } +}