]> git.donarmstrong.com Git - tmk_firmware.git/blob - timer.c
add "Build your own firmware" and "Features" section to README.
[tmk_firmware.git] / timer.c
1 #include <avr/io.h>
2 #include <avr/interrupt.h>
3 #include <stdint.h>
4 #include "timer.h"
5
6 uint16_t timer_count = 0;
7
8 // Configure timer 0 to generate a timer overflow interrupt every
9 // 256*1024 clock cycles, or approx 61 Hz when using 16 MHz clock
10 // This demonstrates how to use interrupts to implement a simple
11 // inactivity timeout.
12 void timer_init(void)
13 {
14     TCCR0A = 0x00;
15     TCCR0B = 0x05;
16     TIMSK0 = (1<<TOIE0);
17 }
18
19 inline
20 void timer_clear(void)
21 {
22     cli();
23     timer_count = 0;
24     sei();
25 }
26
27 inline
28 uint16_t timer_read(void)
29 {
30     uint8_t _sreg = SREG;
31     uint16_t t;
32
33     cli();
34     t = timer_count;
35     SREG = _sreg;
36
37     return t;
38 }
39
40 inline
41 uint16_t timer_elapsed(uint16_t last)
42 {
43     uint8_t _sreg = SREG;
44     uint16_t t;
45
46     cli();
47     t = timer_count;
48     SREG = _sreg;
49
50     return TIMER_DIFF(t, last);
51 }
52
53 // This interrupt routine is run approx 61 times per second.
54 // A very simple inactivity timeout is implemented, where we
55 // will send a space character and print a message to the
56 // hid_listen debug message window.
57 ISR(TIMER0_OVF_vect)
58 {
59     timer_count++;
60 }
61