-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathses_timer.c
183 lines (163 loc) · 4.74 KB
/
ses_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
/** CTC Mode discription: Clear Time on compare Match
* in normal mode timer is cleared when overflow occures
* in CTC mode timer is cleared when the compare match happen
* to SET the value of comperance use OCR register, OCF flag will trigger it.
*/
/* INCLUDES ******************************************************************/
#include "ses_timer.h"
#include <avr/interrupt.h>
/* DEFINES & MACROS **********************************************************/
#define TIMER1_CYC_FOR_5MILLISEC 0x04E2
#define TIMER2_CYC_FOR_1MILLISEC 249
#define TIMER5_CYC_FOR_5MILLISEC 1250 // (0 -> 1249)
#define TIMER4_CYC_FOR_5MILLISEC 1250
volatile static pTimerCallback timerOneCallbackPointer;
volatile static pTimerCallback timerTwoCallbackPointer;
volatile static pTimerCallback timerFourCallbackPointer;
/*FUNCTION DEFINITION ********************************************************/
void timer0_start()
{
// Configure timer mode: Fast PWM, Physical pin: OC0B
// Enable timer 0: (RRR0 is the power reduction register)
PRR0 &= ~(1<<PRTIM0);
TCNT0 = 0; // set timer count = 0
// Wave generation mode: 0x03,Fast PWM, TOP=0xFF
TCCR0A |= (1<<WGM00)| (1<<WGM01);
TCCR0B &= ~(1<<WGM02);
// Inverting mode:(COM0x1:0 bits to 2)
TCCR0A |= (1<<COM0B0);
TCCR0A |= (1<<COM0B1);
// Pre-scaler: Set to 0 (No- prescaler)
TCCR0B &= ~(1<<CS01);
TCCR0B &= ~(1<<CS02);
TCCR0B &= ~(1<<CS00);
}
void timer0_stop()
{
TCCR0B &= ~(1<<CS00);
}
void timer1_setCallback(pTimerCallback cb)
{
timerOneCallbackPointer = cb;
}
void timer1_start()
{
//Time configurations:
// To access time actual value use TCNT2 register and for Timer flags (TIFR2).
//Timer Mode: WGM(Wave from Generation Mode), CTC
TCCR1A &= ~(1<<WGM11);
TCCR1A &= ~(1<<WGM10);
TCCR1B |= (1<<WGM12);
TCCR1B &= ~(1<<WGM13);
OCR1AH = (uint8_t)(TIMER1_CYC_FOR_5MILLISEC>>8); // Clear each 1250(0x04E2) counts == 5 ms
OCR1AL = (uint8_t) (TIMER1_CYC_FOR_5MILLISEC);
//Set Timer mask:
TIMSK1 |= (1<<OCIE1A);
//Prescaler: Clock select bits
TCCR1B &= ~(1<<CS22);
TCCR1B |= (1<<CS21);
TCCR1B |= (1<<CS20); //IMP: when prescaler is set time will count immediatly.
// Time will start count from 0 untill 1250.
}
void timer1_stop()
{
TIMSK1 &= ~(1<<OCIE1A);
TCCR2B &= ~(1<<CS22);
TCCR2B &= ~(1<<CS21);
TCCR2B &= ~(1<<CS20);
}
void timer2_setCallback(pTimerCallback cb)
{
timerTwoCallbackPointer = cb;
}
void timer2_start()
{
//Time configurations:
// To access time actual value use TCNT2 register and for Timer flags (TIFR2).
//Timer Mode: WGM(Wave from Generation Mode), CTC (0x4)"0100"
TCCR2A |= (1<<WGM21);
TCCR2A &= ~(1<<WGM20);
OCR2A = TIMER2_CYC_FOR_1MILLISEC; // Clear each 250 counts
//Set Timer mask:
TIMSK2 |= (1<<OCIE2A);
//Prescaler: Clock select bits
TCCR2B |= (1<<CS22);
TCCR2B &= ~(1<<CS21);
TCCR2B &= ~(1<<CS20); //IMP: when prescaler is set time will count immediatly.
// Time will start count from 0 untill 250.
}
void timer2_stop()
{
//Disable timer Mask:
TIMSK2 &= ~(1<<OCIE2A);
TCCR2B &= ~(1<<CS22);
TCCR2B &= ~(1<<CS21);
TCCR2B &= ~(1<<CS20);
}
void timer4_setCallback(pTimerCallback cb)
{
timerFourCallbackPointer = cb;
}
void timer4_start()
{
//Timer configurations:
//Timer Mode: WGM(Wave from Generation Mode), CTC
TCCR4A &= ~(1<<WGM41);
TCCR4A &= ~(1<<WGM40);
TCCR4B |= (1<<WGM42);
TCCR4B &= ~(1<<WGM43);
// Compare value:
OCR4A = TIMER4_CYC_FOR_5MILLISEC;
TCNT4 = 0; // Reset timer cnt
//Set Timer mask:
TIMSK4 |= (1<<OCIE4A);
///Prescaler: Clock select bits = 64
TCCR4B |= (1<<CS40);
TCCR4B |= (1<<CS41);
TCCR4B &= ~(1<<CS42);
}
void timer4_stop()
{
TIMSK4 &= ~(1<<OCIE4A);
TCCR4B &= ~(1<<CS42);
TCCR4B &= ~(1<<CS41);
TCCR4B &= ~(1<<CS40);
}
void timer5_start()
{
//Timer configurations:
//Timer Mode: WGM(Wave from Generation Mode), CTC
TCCR5A &= ~(1<<WGM51);
TCCR5A &= ~(1<<WGM50);
TCCR5B |= (1<<WGM52);
TCCR5B &= ~(1<<WGM53);
// Compare value:
OCR5A = TIMER5_CYC_FOR_5MILLISEC; // ISR executed each 5 ms
TCNT5 = 0; // Reset timer cnt
//Set Timer mask:
TIMSK5 |= (1<<OCIE5A);
//Prescaler: Clock select bits = 64
//IMP: when prescaler is set time will count immediatly.
TCCR5B |= (1<<CS50);
TCCR5B |= (1<<CS51);
TCCR5B &= ~(1<<CS52);
}
void timer5_stop()
{
TIMSK5 &= ~(1<<OCIE5A);
TCCR5B &= ~(1<<CS52);
TCCR5B &= ~(1<<CS51);
TCCR5B &= ~(1<<CS50);
}
ISR(TIMER1_COMPA_vect)
{
timerOneCallbackPointer();
}
ISR(TIMER2_COMPA_vect)
{
timerTwoCallbackPointer();
}
ISR(TIMER4_COMPA_vect)
{
timerFourCallbackPointer();
}