From 5114887f1c89095a1ee0b133a46726833137996a Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 28 Jun 2022 02:50:01 +0200 Subject: [PATCH] Refactored timer functions. Change baud of serial connection. Some minor IO changes. --- src/commands.c | 4 ++-- src/include/timer.h | 16 +++++++--------- src/inverter.c | 13 +++++++------ src/timer.c | 35 +++++++++-------------------------- 4 files changed, 25 insertions(+), 43 deletions(-) diff --git a/src/commands.c b/src/commands.c index 2a60f69..2b948e7 100644 --- a/src/commands.c +++ b/src/commands.c @@ -179,9 +179,9 @@ int cmd_handle_switch_timer(void) static int timer1_on = 1; if (timer1_on) - timer_disableT1(); + tmr_disableT1(); else - timer_enableT1(); + tmr_enableT1(); timer1_on = !timer1_on; diff --git a/src/include/timer.h b/src/include/timer.h index 6b18585..b70df7e 100644 --- a/src/include/timer.h +++ b/src/include/timer.h @@ -8,15 +8,13 @@ typedef struct int index; } timer_notify_t; -void initOverflowTimer1(void); -void timer_initT1(void); +void tmr_initT1(void); -void initCtcTimer0(void); -void initOverflowTimer0(void); +void tmr_initT0(void); -void timer_subToT1Overflow(void (*)()); -void timer_enableT1(void); -void timer_disableT1(void); -void timer_setT1CompareMatch(uint16_t cm); -uint16_t timer_getT1Top(void); \ No newline at end of file +void tmr_subToT1Overflow(void (*)()); +void tmr_enableT1(void); +void tmr_disableT1(void); +void tmr_setT1CompareMatch(uint16_t cm); +uint16_t tmr_getT1Top(void); \ No newline at end of file diff --git a/src/inverter.c b/src/inverter.c index 4546183..58b206e 100644 --- a/src/inverter.c +++ b/src/inverter.c @@ -1,5 +1,4 @@ #include -#include #include #include @@ -13,13 +12,15 @@ int main() { sei(); - /* With this MCU frequency (8MHz), 12 is a baudrate of 38.4k baud */ - uart_init(12); + /* With this MCU frequency (16 MHz), 25 is a baudrate of 38.4k baud */ + uart_init(25); pwm_init(); - timer_initT1(); + tmr_initT1(); + tmr_enableT1(); - DDRB |= (1 << DDB5); - DDRD |= (1 << DDD7); + /* Arduino Leonardo has a LED that we turn off here */ + DDRC |= (1 << DDC7); + unsetPin(&PORTC, PIN7); printf("Up and running.\n\r"); diff --git a/src/timer.c b/src/timer.c index 4c65015..c28082d 100644 --- a/src/timer.c +++ b/src/timer.c @@ -14,7 +14,7 @@ timer_notify_t timer1 = }; -void timer_initT1(void) +void tmr_initT1(void) { /* * We are using Timer 1 Compare Match Unit B. @@ -36,36 +36,34 @@ void timer_initT1(void) TIMSK1 |= (1 << TOIE1); /* TOP value */ - OCR1A = 50; + OCR1A = 40000; /* Compare Match with OCR1B */ - OCR1B = 25; - - timer_enableT1(); + OCR1B = 20000; } -void timer_enableT1(void) +void tmr_enableT1(void) { /* Clock prescaler of 8 */ TCCR1B |= (1 << CS11); } -void timer_disableT1(void) +void tmr_disableT1(void) { TCCR1B &= ~((1 << CS10) | (1 << CS11) | (1 << CS12)); } -void timer_setT1CompareMatch(uint16_t cm) +void tmr_setT1CompareMatch(uint16_t cm) { OCR1B = cm; } -uint16_t timer_getT1Top(void) +uint16_t tmr_getT1Top(void) { return OCR1A; } -void initCtcTimer0(void) +void tmr_initT0(void) { /* Initialize counter 0 */ TCNT0 = 0; @@ -85,21 +83,6 @@ void initCtcTimer0(void) OCR0A = 255; } -void initOverflowTimer0(void) -{ - /* Prescaler = 0 */ - TCCR0B |= (1<