]> git.tdb.fi Git - model-railway-devices.git/blob - firmware/timer.c
Strip Id tags and copyright notices from files
[model-railway-devices.git] / firmware / timer.c
1 #include <avr/io.h>
2 #include "timer.h"
3
4 #define BIT(n) (1<<(n))
5
6 static void timer_start(uint8_t num, uint32_t period)
7 {
8         uint8_t cs;
9
10         if(num==0)
11         {
12                 if(period<0x100)
13                 {
14                         cs = BIT(CS00);
15                 }
16                 else if(period<0x800)
17                 {
18                         cs = BIT(CS01);
19                         period /= 8;
20                 }
21                 else if(period<0x4000)
22                 {
23                         cs = BIT(CS01) | BIT(CS00);
24                         period /= 64;
25                 }
26                 else if(period<0x10000)
27                 {
28                         cs = BIT(CS02);
29                         period /= 256;
30                 }
31                 else
32                 {
33                         cs = BIT(CS02) | BIT(CS00);
34                         period /= 1024;
35                         if(period>0xFF)
36                                 period = 0xFF;
37                 }
38                 TCCR0A = BIT(WGM01);
39                 TCCR0B = cs;
40                 OCR0A = period;
41                 TIMSK0 = BIT(OCIE0A);
42         }
43         if(num==1)
44         {
45                 if(period<0x10000)
46                 {
47                         cs = BIT(CS10);
48                 }
49                 else if(period<0x80000)
50                 {
51                         cs = BIT(CS11);
52                         period /= 8;
53                 }
54                 else if(period<0x400000)
55                 {
56                         cs = BIT(CS11) | BIT(CS10);
57                         period /= 64;
58                 }
59                 else if(period<0x1000000)
60                 {
61                         cs = BIT(CS12);
62                         period /= 256;
63                 }
64                 else
65                 {
66                         cs = BIT(CS12) | BIT(CS10);
67                         period /= 1024;
68                         if(period>0xFFFF)
69                                 period = 0xFFFF;
70                 }
71                 TCCR1A = 0;
72                 TCCR1B = BIT(WGM12) | cs;
73                 OCR1AH = period>>8;
74                 OCR1AL = period;
75                 TIMSK1 = BIT(OCIE1A);
76         }
77 }
78
79 void timer_start_hz(uint8_t num, uint32_t freq_p, uint8_t freq_q)
80 {
81         timer_start(num, F_CPU*freq_q/freq_p);
82 }
83
84 void timer_start_us(uint8_t num, uint32_t us)
85 {
86         timer_start(num, F_CPU/1000000*us);
87 }
88
89 void timer_stop(uint8_t num)
90 {
91         if(num==0)
92         {
93                 TCCR0B = 0;
94                 TIMSK0 = 0;
95         }
96         else if(num==1)
97         {
98                 TCCR1B = 0;
99                 TIMSK1 = 0;
100         }
101 }