Add a lot of functions to timer module.
This commit is contained in:
parent
8143ae4eaf
commit
231304bc5f
@ -1,4 +1,22 @@
|
||||
void initPwmTimer1(void);
|
||||
#include <stdint.h>
|
||||
|
||||
#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);
|
||||
|
||||
void timer_subToT1Overflow(void (*)());
|
||||
void timer_enableT1(void);
|
||||
void timer_disableT1(void);
|
||||
void timer_setT1CompareMatch(uint16_t cm);
|
||||
uint16_t timer_getT1Top(void);
|
64
src/timer.c
64
src/timer.c
@ -4,9 +4,17 @@
|
||||
#include <math.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#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<<CS11);
|
||||
TIMSK1 |= (1<<TOIE1);
|
||||
}
|
||||
|
||||
ISR(TIMER1_OVF_vect)
|
||||
{
|
||||
for(int i = 0; i < timer1.index; i++)
|
||||
timer1.callOnInterrupt[i]();
|
||||
}
|
||||
|
||||
ISR(TIMER1_COMPB_vect)
|
||||
{
|
||||
PORTD ^= (1 << PD7);
|
||||
}
|
||||
|
||||
void timer_subToT1Overflow(void (*call)())
|
||||
{
|
||||
if (timer1.index < NOTIFY_ARRAY_SIZE) {
|
||||
timer1.callOnInterrupt[timer1.index] = call;
|
||||
timer1.index++;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user