-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
timer.c
106 lines (103 loc) · 2.12 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
#include "main.h"
void _InitTimer0(void)
{
// OPTION_REG
T0CS = 0; // chon che do TIMER
PSA = 0; // dinh bo ti le cho TIMER
PS2 = PS1 = PS0 = 0; // chon ti le 1:2
TMR0IE = 1; // cho phep ngat TIMER0
GIE = 1; // cho phep ngat toan cuc
}
void _SETUP_TIMER_0(char source_signal, char prescaler)
{
switch (source_signal)
{
case INTERNAL:
{
T0CS = 0; // chon che do TIMER
PSA = 0; // dinh bo ti le cho TIMER
break;
}
case EXTERNAL:
{
T0CS = 1; // chon che do COUNTER
PSA = 1; // dinh bo ti le cho COUNTER
break;
}
default: // khi co dieu kien nao duoc thoa man
break;
}
switch (prescaler)
{
case DIV2:
{
// chon ti le 1:2
PS2 = 0;
PS1 = 0;
PS0 = 0;
break;
}
case DIV4:
{
// chon ti le 1:4
PS2 = 0;
PS1 = 0;
PS0 = 1;
break;
}
case DIV8:
{
// chon ti le 1:8
PS2 = 0;
PS1 = 1;
PS0 = 0;
break;
}
case DIV16:
{
// chon ti le 1:16
PS2 = 0;
PS1 = 1;
PS0 = 1;
break;
}
case DIV32:
{
// chon ti le 1:32
PS2 = 1;
PS1 = 0;
PS0 = 0;
break;
}
case DIV64:
{
// chon ti le 1:64
PS2 = 1;
PS1 = 0;
PS0 = 1;
break;
}
case DIV128:
{
// chon ti le 1:128
PS2 = 1;
PS1 = 1;
PS0 = 0;
break;
}
case DIV256:
{
// chon ti le 1:256
PS2 = 1;
PS1 = 1;
PS0 = 1;
break;
}
default: // khi khong co dieu kien nao duoc thoa man
break;
}
}
void _SET_TIMER0(unsigned char value)
{
TMR0 = value; // ghi gia tri vao thanh ghi TIMER0
}