]> git.gir.st - tmk_keyboard.git/blob - timer.c
added copyright and license notice.
[tmk_keyboard.git] / timer.c
1 /*
2 Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <avr/io.h>
19 #include <avr/interrupt.h>
20 #include <stdint.h>
21 #include "timer.h"
22
23 volatile uint16_t timer_count = 0;
24
25 // Configure timer 0 to generate a timer overflow interrupt every
26 // 256*1024 clock cycles, or approx 61 Hz when using 16 MHz clock
27 // This demonstrates how to use interrupts to implement a simple
28 // inactivity timeout.
29 void timer_init(void)
30 {
31 TCCR0A = 0x00;
32 TCCR0B = 0x05;
33 TIMSK0 = (1<<TOIE0);
34 }
35
36 inline
37 void timer_clear(void)
38 {
39 uint8_t sreg = SREG;
40 cli();
41 timer_count = 0;
42 SREG = sreg;
43 }
44
45 inline
46 uint16_t timer_read(void)
47 {
48 uint16_t t;
49
50 uint8_t sreg = SREG;
51 cli();
52 t = timer_count;
53 SREG = sreg;
54
55 return t;
56 }
57
58 inline
59 uint16_t timer_elapsed(uint16_t last)
60 {
61 uint16_t t;
62
63 uint8_t sreg = SREG;
64 cli();
65 t = timer_count;
66 SREG = sreg;
67
68 return TIMER_DIFF(t, last);
69 }
70
71 // This interrupt routine is run approx 61 times per second.
72 // A very simple inactivity timeout is implemented, where we
73 // will send a space character and print a message to the
74 // hid_listen debug message window.
75 ISR(TIMER0_OVF_vect)
76 {
77 timer_count++;
78 }
Imprint / Impressum