From 8d79cc7812710e322faedb9762d1dccf09eb9120 Mon Sep 17 00:00:00 2001 From: Maix0 <39835848+Maix0@users.noreply.github.com> Date: Fri, 17 Apr 2026 23:01:35 +0200 Subject: [PATCH] feat(ex01): should be done --- ex01/Makefile | 9 ++++++--- ex01/include/timer1.h | 2 +- ex01/include/timer2.h | 2 +- ex01/src/main.c | 42 +++++++++++++++++++++++++++++------------- ex01/src/uart.c | 14 ++++++-------- 5 files changed, 43 insertions(+), 26 deletions(-) diff --git a/ex01/Makefile b/ex01/Makefile index 1d3acdf..9471541 100644 --- a/ex01/Makefile +++ b/ex01/Makefile @@ -3,8 +3,11 @@ MCU=atmega328p F_CPU=16000000 CC=avr-gcc OBJCOPY=avr-objcopy -CFLAGS=-std=c99 -Wall -Wextra -g -Os -mmcu=$(MCU) -DF_CPU=$(F_CPU) -Iinclude +WFLAGS=-Wall -Wextra +CFLAGS=--std=c99 -g -Os -mmcu=$(MCU) -ffunction-sections -fdata-sections +CPPFLAGS=-DF_CPU=$(F_CPU) -Iinclude IFLAGS= +LDFLAGS=-Wl,--gc-sections TARGET=main SERIAL=-P /dev/ttyUSB0 -b 115200 @@ -38,10 +41,10 @@ flash: hex $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c @mkdir -p $(shell dirname $@) - $(CC) $(CFLAGS) $(IFLAGS) -c $< -o $@ + $(CC) $(CPPFLAGS) $(WFLAGS) $(CFLAGS) $(IFLAGS) -c $< -o $@ $(ELF_FILE): $(OBJ) - $(CC) $(CFLAGS) $(OBJ) -o $@ + $(CC) $(CFLAGS) $(LDFLAGS) $(OBJ) -o $@ $(HEX_FILE): $(ELF_FILE) $(OBJCOPY) -j .text -j .data -O ihex $< $@ diff --git a/ex01/include/timer1.h b/ex01/include/timer1.h index ef1bfa7..4cb1dc6 100644 --- a/ex01/include/timer1.h +++ b/ex01/include/timer1.h @@ -65,7 +65,7 @@ static inline void t1_interrupt(enum e_timer_output output, bool enable) { } } -static inline void t1_set_ocr(enum e_timer_output output, uint8_t value) { +static inline void t1_set_ocr(enum e_timer_output output, uint16_t value) { if (output & TO_A) OCR1A = value; if (output & TO_B) diff --git a/ex01/include/timer2.h b/ex01/include/timer2.h index a76eff4..b26b41b 100644 --- a/ex01/include/timer2.h +++ b/ex01/include/timer2.h @@ -47,7 +47,7 @@ static inline void t2_overflow_interrupt(bool enable) { TIMSK2 &= ~_BV(TOIE2); } -static inline void t0_interrupt(enum e_timer_output output, bool enable) { +static inline void t2_interrupt(enum e_timer_output output, bool enable) { if (output & TO_A) { if (enable) TIMSK2 |= _BV(OCIE2A); diff --git a/ex01/src/main.c b/ex01/src/main.c index 0590e9f..3f9b4e0 100644 --- a/ex01/src/main.c +++ b/ex01/src/main.c @@ -4,31 +4,47 @@ #include "interupt.h" #include "mystd.h" +#include "timer_global.h" #include "uart.h" -#include "uart_t" - #include "timer0.h" #include "timer1.h" +#include "timer2.h" -// interrupt for INT0 (external interrupt) -void __attribute__((signal, used)) __vector_1(void) { - my_cli(); - _delay_ms(20); - PORTB ^= _BV(PB0); - // clear the register "pending" bit - EIFR |= _BV(INTF0); - my_sei(); +void __attribute__((signal, used)) __vector_14(void) { + static uint16_t duty = 0; + static int8_t direction = 1; + + // update duty + duty += direction; + if (duty == 999) { + direction = -1; + // PORTB ^= _BV(PB0) | _BV(PB2); // useful for debug + } else if (duty == 0) { + direction = 1; + // PORTB ^= _BV(PB0) | _BV(PB2); // useful for debug + } + t1_set_ocr(TO_A, duty); } int main(void) { - uart_init(); + // uart_init(); // no uart needed here :D - + DDRB |= _BV(PB0); + // DDRB |= _BV(PB1) | _BV(PD2); + // PORTB |= _BV(PD2); + + t0_init_ctc_2(PRESCALER_64); + t0_set_ocr(TO_A, 250); // 64 * 150 * 1000 = + t0_interrupt(TO_A, true); + + t1_init_fpwm_14(PRESCALER_8); + t1_set_ocr(TO_A, 0); + t1_set_out_mode(TO_A, TOM_10); + t1_set_icr1(999); my_sei(); - while (1) { } } diff --git a/ex01/src/uart.c b/ex01/src/uart.c index 182c08f..805d7b5 100644 --- a/ex01/src/uart.c +++ b/ex01/src/uart.c @@ -16,7 +16,7 @@ void uart_init(void) { UCSR0A |= _BV(U2X0); // Enable transmitter - UCSR0B = _BV(TXEN0) | _BV(RXEN0) | _BV(RXCIE0); + UCSR0B = _BV(TXEN0) | _BV(RXEN0); // Set frame format: 8 data bits, no parity, 1 stop bit UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); @@ -47,7 +47,6 @@ void uart_sendstring(const char* str) { str++; } } -/* void uart_send_u8(uint8_t val) { char buf[4] = {0, 0, 0, 0}; uint8_t idx = 0; @@ -94,12 +93,11 @@ void uart_send_u8_hex(uint8_t val) { } void uart_send_u16_hex(uint16_t val) { - char buf[5] = {0, 0, 0, 0, 0}; + char buf[5] = {0, 0, 0, 0, 0}; - buf[0] = "0123456789abcdef"[(val >> 12) & 0x0F]; - buf[1] = "0123456789abcdef"[(val >> 8) & 0x0F]; - buf[2] = "0123456789abcdef"[(val >> 4) & 0x0F]; - buf[3] = "0123456789abcdef"[(val >> 0) & 0x0F]; + buf[0] = "0123456789abcdef"[(val >> 12) & 0x0F]; + buf[1] = "0123456789abcdef"[(val >> 8) & 0x0F]; + buf[2] = "0123456789abcdef"[(val >> 4) & 0x0F]; + buf[3] = "0123456789abcdef"[(val >> 0) & 0x0F]; uart_sendstring(buf); } -*/