-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.c
More file actions
39 lines (29 loc) · 696 Bytes
/
Copy pathtimer.c
File metadata and controls
39 lines (29 loc) · 696 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include "timer.h"
#include <avr/io.h>
#include <avr/interrupt.h>
#define TIMER_PERIOD 100 // 0.1 ms
#define TIMER_PRELOAD 256 - TIMER_PERIOD
unsigned char stimer;
void timer_init()
{
cli();
TCCR0 = (1 << CS01);
TCNT0 = TIMER_PRELOAD;
TIMSK |= (1 << TOIE0);
wdt_reset();
WDTCR |= (1 << WDTOE) | (1 << WDE); // Включить сторожевой таймер
WDTCR = (1 << WDE) | (1 << WDP2) | (1 << WDP0); // 512K циклов (~0,5 с)
stimer = 0;
sei();
}
ISR(TIMER0_OVF_vect)
{
stimer++;
TCNT0 = TIMER_PRELOAD;
}
unsigned char get_stimer()
{
unsigned char result = stimer;
stimer = 0;
return result;
}