-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimer0.c
190 lines (147 loc) · 4.12 KB
/
timer0.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
/*
* timer.c
*
* Created: 02-Mar-2018
* Author: Alsayed Alsisi
* Contact Details:
- (+2)01066769510
*/
/*----------------------------------------------------------------
--------------------- File Inclusions ----------------------------
----------------------------------------------------------------*/
#include <avr/io.h>
#include <stdint.h>
#include <avr/interrupt.h>
#include "timer0.h"
/*----------------------------------------------------------------
--------------------- Public Function Definitions ----------------
----------------------------------------------------------------*/
void timer0_mode_set(timer_mode_t timer_mode)
{
switch (timer_mode)
{
case TIMER0_NORMAL_MODE:
TCCR0 &= ~((1<<WGM01)|(1<<WGM00));
break;
case TIMER0_CTC_MODE:
TCCR0 |= (1<<WGM01);
TCCR0 &= ~(1<<WGM00);
break;
case TIMER0_FAST_PWM_MODE:
TCCR0 |= ((1<<WGM01)|(1<<WGM00));
TCCR0 |= (1<<COM01); //Non-inverted fast PWM mode.
TCCR0 &= ~(1<<COM00);
DDRB |= (1<<3); //setting OC0 as output to output the PWM signal
break;
default:
break;
}
}
void timer0_prescalar_set(timer_prescalar_t timer_prescalar)
{
switch (timer_prescalar)
{
case TIMER0_NO_CLK:
TCCR0 &= ~((1<<CS02)|(1<<CS01)|(1<<CS00));
break;
case TIMER0_NO_PRESCALAR:
TCCR0 &= ~((1<<CS02)|(1<<CS01));
TCCR0 |= (1<<CS00);
break;
case TIMER0_PRESCALAR8:
TCCR0 &= ~((1<<CS02)|(1<<CS00));
TCCR0 |= (1<<CS01);
break;
case TIMER0_PRESCALAR64:
TCCR0 |= ((1<<CS01)|(1<<CS00));
TCCR0 &= ~(1<<CS02);
break;
case TIMER0_PRESCALAR256:
TCCR0 &= ~((1<<CS01)|(1<<CS00));
TCCR0 |= (1<<CS02);
break;
case TIMER0_PRESCALAR1024:
TCCR0 |= ((1<<CS02)|(1<<CS00));
TCCR0 &= ~(1<<CS01);
break;
case TIMER0_EXTERNAL_CLOCK_FALLING_EDGE:
TCCR0 |= ((1<<CS02)|(1<<CS01));
TCCR0 &= ~(1<<CS00);
break;
case TIMER0_EXTERNAL_CLOCK_RISING_EDGE:
TCCR0 |= ((1<<CS02)|(1<<CS01)|(1<<CS00));
break;
default:
break;
}
}
void timer0_interrupt_enable(timer_mode_t timer_mode)
{
switch(timer_mode)
{
case TIMER0_NORMAL_MODE:
TIMSK |= (1<<TOIE0);
sei();
break;
case TIMER0_CTC_MODE:
TIMSK |= (1<<OCIE0);
sei();
break;
default:
break;
}
}
void timer0_interrupt_disable(void)
{
TIMSK &= ~((1<<TOIE0)|(1<<OCIE0));
}
void timer0_TCNT0_load(uint8_t value_to_load)
{
TCNT0 = value_to_load;
}
void timer0_OCR0_load( uint8_t value_to_load)
{
OCR0 = value_to_load;
}
void timer0_create_delay_us(uint8_t value_to_load_ocr0, timer_prescalar_t timer_prescalar)
{
TIMSK &= ~((1<<TOIE0)|(1<<OCIE0)); //disable the timer interrupts.
TCNT0 = 0; //to make sure you are starting clean.
OCR0 = value_to_load_ocr0; //loading ocr0 register with the desired value.
switch (timer_prescalar) //setting the timer clock prescalar.
{
case TIMER0_NO_PRESCALAR:
TCCR0 &= ~((1<<CS02)|(1<<CS01));
TCCR0 |= (1<<CS00);
break;
case TIMER0_PRESCALAR8:
TCCR0 &= ~((1<<CS02)|(1<<CS00));
TCCR0 |= (1<<CS01);
break;
case TIMER0_PRESCALAR64:
TCCR0 |= ((1<<CS01)|(1<<CS00));
TCCR0 &= ~(1<<CS02);
break;
case TIMER0_PRESCALAR256:
TCCR0 &= ~((1<<CS01)|(1<<CS00));
TCCR0 |= (1<<CS02);
break;
case TIMER0_PRESCALAR1024:
TCCR0 |= ((1<<CS02)|(1<<CS00));
TCCR0 &= ~(1<<CS01);
break;
default:
break;
}
while((TIFR & (1<<OCF0)) == 0); //check compare match flag.
TCCR0 = 0x00; //stop timer.
TIFR |= (1<<OCF0); //clear flag.
}
void timer0_pwm_duty_cycle_set(uint8_t duty_cycle_percentage)
{
OCR0 =( ( (duty_cycle_percentage * 256) / 100) - 1 );
}
/*----------------------------------------------------------------
--------------------- End of File --------------------------------
----------------------------------------------------------------*/