-
Notifications
You must be signed in to change notification settings - Fork 20
/
pwm.c
131 lines (96 loc) · 1.88 KB
/
pwm.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
/*
Project: 1Sheeld Firmware
File: pwm.cpp
Compiler: avr-gcc 3.4.2
Author: Integreight
Date: 2014.5
*/
#include "config.h"
#include "pwm.h"
void initPwm(uint8_t timer)
{
switch(timer)
{
// make 64 prescalar for all timers except timer3=16 and timer 2=32
case TIMER_00 :
TCCR0|=(1<<WGM00)|(1<<WGM01)|(1<<CS00)|(1<<COM01)|(1<<CS01);
break;
case TIMER_1A:
TCCR1A|=(1<<WGM10)|(1<<COM1A1);
TCCR1B|=(1<<WGM12)|(1<<CS10)|(1<<CS11);
break;
case TIMER_1B:
TCCR1A|=(1<<WGM10)|(1<<COM1B1);
TCCR1B|=(1<<WGM12)|(1<<CS10)|(1<<CS11);
break;
case TIMER_02:
TCCR2|=(1<<WGM20)|(1<<WGM21)|(1<<CS21)|(1<<CS20)|(1<<COM01);
break;
case TIMER_3A:
TCCR3A|=(1<<WGM30)|(1<<COM3A1);
TCCR3B|=(1<<WGM32)|(1<<CS32)|(1<<CS31);
break;
case TIMER_3B:
TCCR3A|=(1<<WGM30)|(1<<COM3B1);
TCCR3B|=(1<<WGM32)|(1<<CS32)|(1<<CS31);
break;
default:
break;
}
}
void setPwmDutyCycle(uint8_t dutyCycle, uint8_t timerNo )
{
switch(timerNo)
{
case TIMER_00 :
OCR0 = dutyCycle;
break;
case TIMER_1A:
OCR1A = dutyCycle;
break;
case TIMER_1B:
OCR1B = dutyCycle;
break;
case TIMER_02:
OCR2 = dutyCycle;
break;
case TIMER_3A:
OCR3A = dutyCycle;
break;
case TIMER_3B:
OCR3B = dutyCycle;
break;
default:
break;
}
}
void turnOffPWM(uint8_t timer)
{
switch(timer)
{
case TIMER_00 :
TCCR0&=3;//for millis
break;
case TIMER_1A:
CLR_BIT(TCCR1A ,COM1A1);
TCCR1B|=(1<<WGM12)|(1<<CS10);
break;
case TIMER_1B:
CLR_BIT(TCCR1A ,COM1B1);
TCCR1B|=(1<<WGM12)|(1<<CS10);
break;
case TIMER_02:
TCCR2 &= 0x03;
break;
case TIMER_3A:
CLR_BIT(TCCR3A ,COM3A1);
TCCR3B|=(1<<WGM32)|(1<<CS30);
break;
case TIMER_3B:
CLR_BIT(TCCR3A ,COM3B1);
TCCR3B|=(1<<WGM32)|(1<<CS30);
break;
default:
break;
}
}