From 231304bc5fa82ab3312bb598b5c2a5fc9524fc70 Mon Sep 17 00:00:00 2001 From: Marco Date: Tue, 31 May 2022 00:10:37 +0200 Subject: [PATCH] Add a lot of functions to timer module. --- src/include/timer.h | 22 ++++++++++++++-- src/timer.c | 64 ++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 77 insertions(+), 9 deletions(-) diff --git a/src/include/timer.h b/src/include/timer.h index 828c4f3..6b18585 100644 --- a/src/include/timer.h +++ b/src/include/timer.h @@ -1,4 +1,22 @@ -void initPwmTimer1(void); +#include + +#define NOTIFY_ARRAY_SIZE 10 + +typedef struct +{ + void (*callOnInterrupt[NOTIFY_ARRAY_SIZE])(); + int index; +} timer_notify_t; + +void initOverflowTimer1(void); + +void timer_initT1(void); + void initCtcTimer0(void); void initOverflowTimer0(void); -void initOverflowTimer1(void); \ No newline at end of file + +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 diff --git a/src/timer.c b/src/timer.c index d0428c1..4c65015 100644 --- a/src/timer.c +++ b/src/timer.c @@ -4,9 +4,17 @@ #include #include -#include "include/timer.h" +#include "timer.h" -void initPwmTimer1(void) + +timer_notify_t timer1 = +{ + .index = 0, + .callOnInterrupt = {0} +}; + + +void timer_initT1(void) { /* * We are using Timer 1 Compare Match Unit B. @@ -18,20 +26,43 @@ void initPwmTimer1(void) TCCR1A |= (1 << WGM11) | (1 << WGM10); TCCR1B |= (1 << WGM13) | (1 << WGM12); - /* Clock prescaler of 64 */ - TCCR1B |= (1 << CS11) | (1 << CS10); - /* Set OC1B on compare match, clear OC1B at TOP */ TCCR1A |= (1 << COM1B1); /* Enable Counter1 Compare Match B Interrupt */ TIMSK1 |= (1 << OCIE1B); + /* Enable Counter1 Overflow Interrupt */ + TIMSK1 |= (1 << TOIE1); + /* TOP value */ - OCR1A = 125; + OCR1A = 50; /* Compare Match with OCR1B */ - OCR1B = 62; + OCR1B = 25; + + timer_enableT1(); +} + +void timer_enableT1(void) +{ + /* Clock prescaler of 8 */ + TCCR1B |= (1 << CS11); +} + +void timer_disableT1(void) +{ + TCCR1B &= ~((1 << CS10) | (1 << CS11) | (1 << CS12)); +} + +void timer_setT1CompareMatch(uint16_t cm) +{ + OCR1B = cm; +} + +uint16_t timer_getT1Top(void) +{ + return OCR1A; } void initCtcTimer0(void) @@ -68,3 +99,22 @@ void initOverflowTimer1(void) TCCR1B |= (1<