-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTimer.cpp
More file actions
185 lines (160 loc) · 3.86 KB
/
Timer.cpp
File metadata and controls
185 lines (160 loc) · 3.86 KB
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
#include "Arduino.h"
extern HardwareSerial *cmd_port;
unsigned long g_milliTimeoutBegin = 0;
bool g_timeoutEnable = true;
void setTimeoutBegin(void)
{
g_milliTimeoutBegin = millis();
}
bool isTimeoutMilliSec(unsigned long milliSecTimeout)
{
unsigned long currentMilliSec;
if(!g_timeoutEnable)
return false;
currentMilliSec = millis();
if( (currentMilliSec-g_milliTimeoutBegin)>=milliSecTimeout )
{
return true;
}
else
{
return false;
}
}
void TimerInit(unsigned char timer, unsigned long microSec, unsigned short ocrna, unsigned int tccrnb)
{
//noInterrupts()
switch(timer)
{
case 1:
TIMSK1 &= ~(1<<OCIE1A); //disable timer compare interrupt
TCCR1A = 0;
TCNT1 = 0;
OCR1A = ocrna;
TCCR1B = tccrnb;
TIMSK1 |= (1<<OCIE1A); //enable timer compare interrupt
break;
/*
case 3:
TIMSK3 &= ~(1<<OCIE3A); //disable timer compare interrupt
TCCR3A = 0;
TCNT3 = 0;
OCR3A = ocrna;
TCCR3B = tccrnb;
TIMSK3 |= (1<<OCIE3A); //enable timer compare interrupt
break;
*/
default:
break;
}
//interrupts();
}
void TimerInit(unsigned char timer, unsigned long microSec)
{
/*
(2 x prescaler) / 16MHz <= T <= (65535 x prescaler) / 16MHz
1 prescaler
0.125 micro sec <= T <= 4.0959375 milli sec
8 prescaler
1 micro sec <= T <= 32.7675 milli sec
64 prescaler
8 micro sec <= T <= 262.140 milli sec
256 prescaler
32 micro sec <= T <= 1.048560 sec
1024 prescaler
128 micro sec <= T <= 4.19424 sec
*/
//noInterrupts()
switch(timer)
{
case 1:
TIMSK1 &= ~(1<<OCIE1A); //disable timer compare interrupt
TCCR1A = 0;
TCNT1 = 0;
//OCR1A output compare register 16MHz*sec/256 = 16*microSec/256
if( microSec<=4095 )
{//use 1 prescaler
OCR1A = (unsigned short)(microSec*16);
TCCR1B = (1<<WGM12) | (1<<CS10); //CTC mode , 1 prescaler
}
else if( microSec<=32767 )
{//use 8 prescaler
OCR1A = (unsigned short)(microSec*16/8);
TCCR1B = (1<<WGM12) | (1<<CS11); //CTC mode , 8 prescaler
}
else if( microSec<=262140 )
{//use 64 prescaler
OCR1A = (unsigned short)(microSec*16/64);
TCCR1B = (1<<WGM12) | (1<<CS11) | (1<<CS10); //CTC mode , 64 prescaler
}
else if( microSec<=1048560 )
{//use 256 prescaler
OCR1A = (unsigned short)(microSec*16/256);
TCCR1B = (1<<WGM12) | (1<<CS12); //CTC mode , 256 prescaler
}
else if( microSec<=4194240 )
{//use 1024 prescaler
OCR1A = (unsigned short)(microSec*16/1024);
TCCR1B = (1<<WGM12) | (1<<CS12) | (1<<CS10); //CTC mode , 1024 prescaler
}
else
{
}
TIMSK1 |= (1<<OCIE1A); //enable timer compare interrupt
break;
case 3:
TIMSK3 &= ~(1<<OCIE3A); //disable timer compare interrupt
TCCR3A = 0;
TCNT3 = 0;
//OCR3A output compare register 16MHz*sec/256 = 16*microSec/256
if( microSec<=4095 )
{//use 1 prescaler
OCR3A = (unsigned short)(microSec*16);
TCCR3B = (1<<WGM32) | (1<<CS30); //CTC mode , 1 prescaler
}
else if( microSec<=32767 )
{//use 8 prescaler
OCR3A = (unsigned short)(microSec*16/8);
TCCR3B = (1<<WGM32) | (1<<CS31); //CTC mode , 8 prescaler
}
else if( microSec<=262140 )
{//use 64 prescaler
OCR3A = (unsigned short)(microSec*16/64);
TCCR3B = (1<<WGM32) | (1<<CS31) | (1<<CS30); //CTC mode , 64 prescaler
}
else if( microSec<=1048560 )
{//use 256 prescaler
OCR3A = (unsigned short)(microSec*16/256);
TCCR3B = (1<<WGM32) | (1<<CS32); //CTC mode , 256 prescaler
}
else if( microSec<=4194240 )
{//use 1024 prescaler
OCR3A = (unsigned short)(microSec*16/1024);
TCCR3B = (1<<WGM32) | (1<<CS32) | (1<<CS30); //CTC mode , 1024 prescaler
}
else
{
}
TIMSK3 |= (1<<OCIE3A); //enable timer compare interrupt
break;
default:
break;
}
//interrupts();
}
void TimerDeinit(unsigned char timer)
{
switch(timer)
{
case 1:
TIMSK1 &= ~(1<<OCIE1A); //disable timer compare interrupt
break;
/*
case 3:
TIMSK3 &= ~(1<<OCIE3A); //disable timer compare interrupt
break;
*/
default:
break;
}
}