feat(ex01): should be done

This commit is contained in:
Maix0 2026-04-17 23:01:35 +02:00
parent 8345043fac
commit 8d79cc7812
5 changed files with 43 additions and 26 deletions

View file

@ -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 $< $@

View file

@ -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)

View file

@ -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);

View file

@ -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) {
}
}

View file

@ -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);
}
*/