37 lines
602 B
C
37 lines
602 B
C
|
#include "timers.h"
|
||
|
|
||
|
#include <avr/io.h>
|
||
|
#include <avr/interrupt.h>
|
||
|
|
||
|
extern void systick_cb();
|
||
|
|
||
|
volatile uint16_t timer[TIMERS_COUNT];
|
||
|
|
||
|
void timers_init() {
|
||
|
// Configure Systick
|
||
|
TCCR1B = (1 << CS11) | (1 << CS10) | (1 << WGM12); // CLKio / 64, CTC
|
||
|
OCR1A = 250 * SYSTICK_MS;
|
||
|
TIMSK = (1 << OCIE1A);
|
||
|
}
|
||
|
|
||
|
uint8_t block_for(uint8_t channel, uint16_t time_systicks)
|
||
|
{
|
||
|
if (timer_expired(channel)) {
|
||
|
timer_set(channel, time_systicks);
|
||
|
return 1;
|
||
|
}
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
ISR (TIMER1_COMPA_vect)
|
||
|
{
|
||
|
uint8_t i;
|
||
|
for (i = 0; i < TIMERS_COUNT; ++i) {
|
||
|
if (timer[i] != 0) {
|
||
|
timer[i]--;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
systick_cb();
|
||
|
}
|