67 lines
906 B
C
67 lines
906 B
C
#include <avr/io.h>
|
|
#include "systick.h"
|
|
#include <stdlib.h>
|
|
|
|
uint8_t ctime;
|
|
|
|
static uint8_t tims[NTIM];
|
|
|
|
void systick_init()
|
|
{
|
|
int i;
|
|
|
|
TCCR1A = 0;
|
|
TCCR1B = (1 << CS10) | (1 << CS12); // f_tim = f_clk / 256 -> TCNT1H counts with 8ms / tick
|
|
TCCR1C = 0;
|
|
OCR1A = 0x7FFF;
|
|
|
|
for (i=0; i < NTIM; ++i) {
|
|
tims[i] = 0;
|
|
}
|
|
}
|
|
|
|
void timer_proc()
|
|
{
|
|
uint8_t now;
|
|
uint8_t delta;
|
|
uint8_t i;
|
|
|
|
now = TCNT1L;
|
|
now = TCNT1H;
|
|
|
|
if (now == ctime)
|
|
return;
|
|
delta = now - ctime;
|
|
if (delta > 127)
|
|
delta -= 128;
|
|
|
|
for (i=0; i < NTIM; ++i) {
|
|
if (tims[i] > delta) {
|
|
tims[i] -= delta;
|
|
} else {
|
|
tims[i] = 0;
|
|
}
|
|
}
|
|
|
|
ctime = now;
|
|
}
|
|
|
|
uint8_t timer_expired(uint8_t tim_id)
|
|
{
|
|
return !tims[tim_id];
|
|
}
|
|
|
|
void timer_set(uint8_t tim_id, uint8_t ticks)
|
|
{
|
|
tims[tim_id] = ticks;
|
|
}
|
|
|
|
uint8_t block_for(uint8_t tim_id, uint8_t ticks)
|
|
{
|
|
if (timer_expired(tim_id)) {
|
|
timer_set(tim_id, ticks);
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|