]> www.fi.muni.cz Git - bike-lights.git/blob - firmware/tmr.c
schematics/.gitignore
[bike-lights.git] / firmware / tmr.c
1 #include <avr/io.h>
2 #include <avr/interrupt.h>
3
4 #include "lights.h"
5
6 volatile uint16_t jiffies;
7 #define PATTERN_DIV 5   // clk/10
8 static unsigned char pattern_div;
9
10 void init_tmr()
11 {
12         TCCR0A = _BV(WGM00);
13         TCCR0B = _BV(CS02) | _BV(CS00); // CLK/1024 = 1 kHz
14         OCR0A = 10; // 100 Hz
15         TIMSK |= _BV(OCIE0A);
16
17         jiffies = 0;
18         pattern_div = PATTERN_DIV;
19 }
20
21 void susp_tmr()
22 {
23         TCCR0A = TCCR0B = 0;
24         TIMSK &= ~_BV(OCIE0A);
25         TIFR = 0;
26 }
27
28 ISR(TIMER0_COMPA_vect)
29 {
30         ++jiffies;
31
32         if (--pattern_div == 0) {
33                 timer_check_buttons();
34                 patterns_next_tick();
35                 pattern_div = PATTERN_DIV;
36         }
37
38         timer_start_adcs();
39 }
40