-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer.c
216 lines (196 loc) · 3.62 KB
/
timer.c
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
/*
* timer.c
*
* Created: 5/7/2020 03:58:09
* Author: Hassan
*/
#include <avr/io.h>
#include "macros.h"
#include "types.h"
#define __INTR_ATTRS used, externally_visible
void(*timer0_ctc_isr)(void);
void(*timer1a_ctc_isr)(void);
void(*timer1b_ctc_isr)(void);
void(*timer2_ctc_isr)(void);
void timer_vid_set_isr_timer0_ctc (void (*callback_function) (void))
{
timer0_ctc_isr=callback_function;
}
void timer_vid_set_isr_timer1a_ctc (void (*callback_function) (void))
{
timer1a_ctc_isr=callback_function;
}
void timer_vid_set_isr_timer1b_ctc (void (*callback_function) (void))
{
timer1b_ctc_isr=callback_function;
}
void timer_vid_set_isr_timer2_ctc (void (*callback_function) (void))
{
timer2_ctc_isr=callback_function;
}
void __vector_10 (void) __attribute__ ((signal,__INTR_ATTRS)) ;
void __vector_10 (void)
{
timer0_ctc_isr();
}
void __vector_7(void) __attribute__ ((signal,__INTR_ATTRS)) ;
void __vector_7 (void)
{
timer1a_ctc_isr();
}
void __vector_8(void) __attribute__ ((signal,__INTR_ATTRS)) ;
void __vector_8 (void)
{
timer1b_ctc_isr();
}
void __vector_4(void) __attribute__ ((signal,__INTR_ATTRS)) ;
void __vector_4 (void)
{
timer2_ctc_isr();
}
void timer_vid_init0_ctc (u16 prescalar, u8 top, u8 interrupt)
{
/*CTC MODE*/
setBit(TCCR0, WGM01);
clrBit(TCCR0, WGM00);
/*SETTING PRESCALAR*/
switch (prescalar) {
case 1:
TCCR0 = TCCR0| 0b00000001;
break;
case 8:
TCCR0 = TCCR0| 0b00000010;
break;
case 64:
TCCR0 = TCCR0| 0b00000011;
break;
case 256:
TCCR0 = TCCR0| 0b00000100;
break;
case 1024:
TCCR0 = TCCR0| 0b00000101;
break;
default:
break;
}
/*SETTING THE TOP VALUE*/
OCR0 = top;
/*ENABLE INTERRUPT*/
if (interrupt) {
/*ENABLE GLOBAL INTERRUPT*/
setBit(SREG, 7);
/*ENABLE TIMER0 INTERRUPT*/
setBit(TIMSK, OCIE0);
}
else {
clrBit(TIMSK, OCIE0);
}
}
void timer_vid_init1_ctc (u16 prescalar, u16 top, u8 interrupt, u8 channel)
{
/*CTC MODE*/
clrBit(TCCR1A, WGM10);
clrBit(TCCR1A, WGM11);
setBit(TCCR1B, WGM12);
clrBit(TCCR1B, WGM13);
/*SETTING PRESCALAR*/
switch (prescalar) {
case 1:
TCCR1B = TCCR1B| 0b00000001;
break;
case 8:
TCCR1B = TCCR1B| 0b00000010;
break;
case 64:
TCCR1B = TCCR1B| 0b00000011;
break;
case 256:
TCCR1B = TCCR1B| 0b00000100;
break;
case 1024:
TCCR1B = TCCR1B| 0b00000101;
break;
default:
break;
}
if (prescalar == 1)
/*SETTING THE TOP VALUE*/
switch (channel) {
case 'A':
OCR1AL = top;
OCR1AH = top>>8;
break;
case 'B':
OCR1BL = top;
OCR1BH = top>>8;
break;
default:
break;
}
/*ENABLE INTERRUPT*/
if (interrupt) {
/*ENABLE GLOBAL INTERRUPT*/
setBit(SREG, 7);
/*ENABLE TIMER0 INTERRUPT*/
switch (channel) {
case 'A':
setBit(TIMSK, OCIE1A);
clrBit(TIMSK, OCIE1B);
break;
case 'B':
clrBit(TIMSK, OCIE1A);
setBit(TIMSK, OCIE1B);
break;
default:
break;
}
}
else {
clrBit(TIMSK, OCIE1A);
clrBit(TIMSK, OCIE1B);
}
}
void timer_vid_init2_ctc (u16 prescalar, u8 top, u8 interrupt)
{
/*CTC MODE*/
setBit(TCCR2, WGM21);
clrBit(TCCR2, WGM20);
/*SETTING PRESCALAR*/
switch (prescalar) {
case 1:
TCCR2 = TCCR2| 0b00000001;
break;
case 8:
TCCR2 = TCCR2| 0b00000010;
break;
case 32:
TCCR2 = TCCR2| 0b00000011;
break;
case 64:
TCCR2 = TCCR2| 0b00000100;
break;
case 128:
TCCR2 = TCCR2| 0b00000101;
break;
case 256:
TCCR2 = TCCR2| 0b00000110;
break;
case 1024:
TCCR2 = TCCR2| 0b00000111;
break;
default:
break;
}
/*SETTING THE TOP VALUE*/
OCR2 = top;
/*ENABLE INTERRUPT*/
if (interrupt) {
/*ENABLE GLOBAL INTERRUPT*/
setBit(SREG, 7);
/*ENABLE TIMER0 INTERRUPT*/
setBit(TIMSK, OCIE2);
}
else {
clrBit(TIMSK, OCIE2);
}
}