-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer2.c
More file actions
278 lines (214 loc) · 6.66 KB
/
timer2.c
File metadata and controls
278 lines (214 loc) · 6.66 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
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* timer2.c
*
* Created: 04-Mar-2018
* Copyright: ©2018 Alsayed Alsisi
* Author: Alsayed Alsisi
* License:
You have the complete freedom to use the code in any educational or commercial purposes under the following conditions:
- You don't remove my name under any circumstances.
- You use the code as is. And if you need to edit it before using it you may contact me first.
* Contact Details:
- E-mail: alsayed.alsisi@gmail.com
- Phone Number: +201066769510
*/
/*----------------------------------------------------------------
--------------------- File Inclusions ----------------------------
----------------------------------------------------------------*/
#include <avr/io.h>
#include <stdint.h>
#include <avr/interrupt.h>
#include "timer2.h"
/*----------------------------------------------------------------
--------------------- Private Data Types -------------------------
----------------------------------------------------------------*/
typedef enum{
TIMER2_NORMAL_MODE=0,
TIMER2_PHASE_CORRECT_PWM_MODE,
TIMER2_CTC_MODE,
TIMER2_FAST_PWM_MODE
}timer_mode_t;
typedef enum{
TIMER2_NO_CLK=0, //No clock source (Timer/Counter stopped).
TIMER2_NO_PRESCALAR, //clk / (No prescaling)
TIMER2_PRESCALAR8, //clk / 8
TIMER2_PRESCALAR32, //clk / 32
TIMER2_PRESCALAR64, //clk / 64
TIMER2_PRESCALAR128, //clk / 128
TIMER2_PRESCALAR256, //clk / 256
TIMER2_PRESCALAR1024, //clk / 1024
TIMER2_EXTERNAL_CLOCK_FALLING_EDGE, //
TIMER2_EXTERNAL_CLOCK_RISING_EDGE //
}timer_prescalar_t;
/*----------------------------------------------------------------
--------------------- Private Functions Prototypes ---------------
----------------------------------------------------------------*/
static void timer2_mode_set(timer_mode_t timer_mode);
static void timer2_prescalar_set(timer_prescalar_t timer_prescalar);
static void timer2_interrupt_enable(timer_mode_t timer_mode);
static void timer2_interrupt_disable(void);
static void timer2_TCNT2_load(uint8_t value_to_load);
static void timer2_OCR2_load( uint8_t value_to_load);
static void timer2_create_delay_us(uint8_t value_to_load_ocr2, timer_prescalar_t timer_prescalar);
static void timer2_pwm_duty_cycle_set(uint8_t duty_cycle_percentage);
/*----------------------------------------------------------------
--------------------- Public Function Definitions ----------------
----------------------------------------------------------------*/
void timer2_init(void)
{
timer2_mode_set(TIMER2_CTC_MODE);
timer2_interrupt_enable(TIMER2_CTC_MODE);
}
void timer2_start(void)
{
timer2_prescalar_set(TIMER2_PRESCALAR1024);
}
void timer2_stop(void)
{
timer2_prescalar_set(TIMER2_NO_CLK);
}
void timer2_load_ocr2(uint8_t value_to_load_ocr2)
{
OCR2 = value_to_load_ocr2;
TCNT2 = 0;
}
/*----------------------------------------------------------------
--------------------- Private Function Definitions ----------------
----------------------------------------------------------------*/
static void timer2_mode_set(timer_mode_t timer_mode)
{
switch (timer_mode)
{
case TIMER2_NORMAL_MODE:
TCCR2 &= ~((1<<WGM21)|(1<<WGM20));
break;
case TIMER2_CTC_MODE:
TCCR2 |= (1<<WGM21);
TCCR2 &= ~(1<<WGM20);
break;
case TIMER2_FAST_PWM_MODE:
TCCR2 |= ((1<<WGM21)|(1<<WGM20));
TCCR2 |= (1<<COM21); //Non-inverted fast PWM mode.
TCCR2 &= ~(1<<COM20);
DDRD |= (1<<7); //setting OC2 as output to output the PWM signal
default:
break;
}
}
static void timer2_prescalar_set(timer_prescalar_t timer_prescalar)
{
switch (timer_prescalar)
{
case TIMER2_NO_CLK:
TCCR2 &= ~((1<<CS22)|(1<<CS21)|(1<<CS20));
break;
case TIMER2_NO_PRESCALAR:
TCCR2 |= (1<<CS20);
TCCR2 &= ~((1<<CS22)|(1<<CS21));
break;
case TIMER2_PRESCALAR8:
TCCR2 |= (1<<CS21);
TCCR2 &= ~((1<<CS22)|(1<<CS20));
break;
case TIMER2_PRESCALAR32:
TCCR2 |= ((1<<CS21)|(1<<CS20));
TCCR2 &= ~(1<<CS22);
break;
case TIMER2_PRESCALAR64:
TCCR2 |= (1<<CS22);
TCCR2 &= ~((1<<CS21)|(1<<CS20));
break;
case TIMER2_PRESCALAR128:
TCCR2 |= ((1<<CS22)|(1<<CS20));
TCCR2 &= ~(1<<CS21);
break;
case TIMER2_PRESCALAR256:
TCCR2 |= ((1<<CS22)|(1<<CS21));
TCCR2 = ~(1<<CS20);
break;
case TIMER2_PRESCALAR1024:
TCCR2 |= ((1<<CS22)|(1<<CS21)|(1<<CS20));
break;
default:
break;
}
}
static void timer2_interrupt_enable(timer_mode_t timer_mode)
{
switch(timer_mode)
{
case TIMER2_NORMAL_MODE:
TIMSK |= (1<<TOIE2);
sei();
break;
case TIMER2_CTC_MODE:
TIMSK |= (1<<OCIE2);
sei();
break;
default:
break;
}
}
static void timer2_interrupt_disable(void)
{
TIMSK &= ~((1<<TOIE2)|(1<<OCIE2));
}
static void timer2_TCNT2_load(uint8_t value_to_load)
{
TCNT2 = value_to_load;
}
static void timer2_OCR2_load( uint8_t value_to_load)
{
OCR2 = value_to_load;
}
static void timer2_create_delay_us(uint8_t value_to_load_ocr2, timer_prescalar_t timer_prescalar)
{
TIMSK &= ~((1<<TOIE2)|(1<<OCIE2)); //disable the timer interrupts.
TCNT2 = 0; //to make sure you are starting clean.
OCR2 = value_to_load_ocr2; //loading ocr0 register with the desired value.
switch (timer_prescalar) //setting the timer clock prescalar.
{
case TIMER2_NO_CLK:
TCCR2 &= ~((1<<CS22)|(1<<CS21)|(1<<CS20));
break;
case TIMER2_NO_PRESCALAR:
TCCR2 |= (1<<CS20);
TCCR2 &= ~((1<<CS22)|(1<<CS21));
break;
case TIMER2_PRESCALAR8:
TCCR2 |= (1<<CS21);
TCCR2 &= ~((1<<CS22)|(1<<CS20));
break;
case TIMER2_PRESCALAR32:
TCCR2 |= ((1<<CS21)|(1<<CS20));
TCCR2 &= ~(1<<CS22);
break;
case TIMER2_PRESCALAR64:
TCCR2 |= (1<<CS22);
TCCR2 &= ~((1<<CS21)|(1<<CS20));
break;
case TIMER2_PRESCALAR128:
TCCR2 |= ((1<<CS22)|(1<<CS20));
TCCR2 &= ~(1<<CS21);
break;
case TIMER2_PRESCALAR256:
TCCR2 |= ((1<<CS22)|(1<<CS21));
TCCR2 = ~(1<<CS20);
break;
case TIMER2_PRESCALAR1024:
TCCR2 |= ((1<<CS22)|(1<<CS21)|(1<<CS20));
break;
default:
break;
}
while((TIFR & (1<<OCF2)) == 0); //check compare match flag.
TCCR2 = 0x00; //stop timer.
TIFR |= (1<<OCF2); //clear flag.
}
static void timer2_pwm_duty_cycle_set(uint8_t duty_cycle_percentage)
{
OCR2 =( ( (duty_cycle_percentage * 256) / 100) - 1 );
}
/*----------------------------------------------------------------
--------------------- End of File --------------------------------
----------------------------------------------------------------*/