forked from TheDIYGuy999/PWMFrequency
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PWMFrequency.h
250 lines (222 loc) · 8.3 KB
/
PWMFrequency.h
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
//
// -----------------------
// PWM FREQUENCY PRESCALER
// -----------------------
// Written by Kiwisincebirth 2014
// Revised by MacTester57 (aka TheDIYGuy999) to allow correct PWM frequencies (confirmed with oscilloscope). January 2015
// - prescale variable: replaced uint8_t with uint16_t data type (fixes bug, which did not allow frequencies < 492Hz)
// - mode variable: replaced hex format with binary to make it more readable, if compared with bit tables in the 32U4 manual
// - added comments
// Revised by arufl
// - added ATMega 4809 support
//
#ifndef PWMFrequency
#define PWMFrequency
#if ARDUINO >= 100
#include <Arduino.h> // Arduino 1.0
#else
#include <WProgram.h> // Arduino 0022
#endif
#if defined __AVR_ATmega32U4__
/**
* Divides a given PWM pin frequency by a divisor.
*
* Arduino Leonardo AtMega 32u4 specific
*
* Sets the Prescaler (Divisor) for a given PWM pin. The resulting frequency
* is equal to the base frequency divided by the given divisor:
* - Base frequencies:
* o The base frequency for pins 3 and 11 is 64,500 Hz.
* o The base frequency for pins 5,6,9,10,13 is 31,250 Hz.
* - Divisors:
* o The divisors available on pins 3, 5, 9, 10 and 11 are: 1, 8, 64, 256, and 1024.
* o The divisors available on pins 6 and 13 are all powers of two between 1 and 16384
*
* PWM frequencies are tied together in pairs of pins. If one in a
* pair is changed, the other is also changed to match:
* - Pins 3 and 11 are paired on timer0 8bit (Default prescale=64, Freq=977Hz)
* - Pins 9 and 10 are paired on timer1 16bit (Default prescale=64, Freq=490Hz)
* - Pins 5 is exclusivly on timer3 16bit (Default prescale=64, Freq=490Hz)
* - Pins 6 and 13 are paired on timer4 10bit (default prescale=64, Freq=490Hz)
*
* Note: Pins 3 and 11 operate on Timer 0 changes this pins will
* affect the user of the main time millis() functions
*
* Thanks to MacTester of the TonyMacX86 forums for his work in defining
* the timings and testing this library
*/
void setPWMPrescaler(uint8_t pin, uint16_t prescale)
{
byte mode;
if(pin==3 || pin==5 || pin==9 || pin==10 || pin==11) {
switch(prescale) {
case 1: mode = 0b001; break;
case 8: mode = 0b010; break;
case 64: mode = 0b011; break;
case 256: mode = 0b100; break;
case 1024: mode = 0b101; break;
default: return;
}
} else if(pin==6 || pin==13) {
switch(prescale) {
case 1: mode = 0b0001; break;
case 2: mode = 0b0010; break;
case 4: mode = 0b0011; break;
case 8: mode = 0b0100; break;
case 16: mode = 0b0101; break;
case 32: mode = 0b0110; break;
case 64: mode = 0b0111; break;
case 128: mode = 0b1000; break;
case 256: mode = 0b1001; break;
case 512: mode = 0b1010; break;
case 1024: mode = 0b1011; break;
case 2048: mode = 0b1100; break;
case 4096: mode = 0b1101; break;
case 8192: mode = 0b1110; break;
case 16384: mode = 0b1111; break;
default: return;
}
}
if(pin==3 || pin==11) {
TCCR0B = TCCR1B & 0b11111000 | mode;
} else if (pin==9 || pin==10) {
TCCR1B = TCCR1B & 0b11111000 | mode;
} else if (pin==5) {
TCCR3B = TCCR3B & 0b11111000 | mode;
} else if (pin==6 || pin==13) {
TCCR4B = TCCR4B & 0b11110000 | mode;
}
}
#elseif defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
/**
* Divides a given PWM pin frequency by a divisor.
*
* Sets the Prescaler (Divisor) for a given PWM pin. The resulting frequency
* is equal to the base frequency divided by the given divisor:
* - Base frequencies:
* o The base frequency for pins 5 and 6 is 62500 Hz.
* o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
* - Divisors:
* o The divisors available on pins 5, 6, 9, 10 are: 1,8,64,256,1024.
* o The divisors available on pins 3, 11 are: 1,8,32,64,128,256,1024.
*
* PWM frequencies are tied together in pairs of pins. If one in a
* pair is changed, the other is also changed to match:
* - Pins 5 and 6 are paired on timer0 (Default prescale=64, Freq=977Hz)
* - Pins 9 and 10 are paired on timer1 (Default prescale=64, Freq=490Hz)
* - Pins 3 and 11 are paired on timer2 (Default prescale=64, Freq=490Hz)
*
* Note that this function will have side effects on anything else
* that uses timers:
* - Changes on pins 5, or 6 may cause the delay() and
* millis() functions to stop working. Other timing-related
* functions may also be affected.
* - Changes on pins 9 or 10 will cause the Servo library to function
* incorrectly.
*
* Thanks to macegr of the Arduino forums for his documentation of the
* PWM frequency divisors. His post can be viewed at:
*
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235060559/0#4
*/
void setPWMPrescaler(uint8_t pin, uint16_t prescale) {
byte mode;
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
switch(prescale) {
case 1: mode = 0b001; break;
case 8: mode = 0b010; break;
case 64: mode = 0b011; break;
case 256: mode = 0b100; break;
case 1024: mode = 0b101; break;
default: return;
}
} else if(pin == 3 || pin == 11) {
switch(prescale) {
case 1: mode = 0b001; break;
case 8: mode = 0b010; break;
case 32: mode = 0b011; break;
case 64: mode = 0b100; break;
case 128: mode = 0b101; break;
case 256: mode = 0b110; break;
case 1024: mode = 0b111; break;
default: return;
}
}
if(pin==5 || pin==6) {
TCCR0B = TCCR0B & 0b11111000 | mode;
} else if (pin==9 || pin==10) {
TCCR1B = TCCR1B & 0b11111000 | mode;
} else if (pin==3 || pin==11) {
TCCR2B = TCCR2B & 0b11111000 | mode;
}
}
#else
/**
* Divides a given PWM pin frequency by a divisor.
* Arduino Nano Every (ATMega4809) specific by arufl
*
* Sets the Prescaler (Divisor) for a given PWM pin. The resulting frequency
* is equal to the base frequency divided by the given divisor:
* - Base frequencies:
* o The base frequency for all pins is 62500 Hz.
*
* - Divisors:
* o The divisors for pins 5,9 and 10 available are: 1,2,4,8,16,64,256,1024.
* o The divisors for pins 3 and 6: 1,2 and TCA0's divisor in use
*
*
* Note that this function will have side effects on anything else
* that uses timers:
* - Changes on pins 5,9 and 10 may cause the delay() and
* millis() functions to stop working. Other timing-related
* functions may also be affected.
*
* Thanks to plymdiver of the Arduino forums for his explaination on how the
* ATmega4809 timers works and which timer His post can be viewed at:
*
* https://forum.arduino.cc/index.php?topic=626736.msg4503396#msg4503396
*
*/
void setPWMPrescaler(uint8_t pin, uint16_t prescale) {
byte mode;
if(pin == 5 || pin == 9 || pin == 10) {
TCA0.SINGLE.CTRLA &= ~TCA_SINGLE_ENABLE_bm;
TCA0.SINGLE.CTRLA &= ~TCA_SINGLE_CLKSEL_gm;
switch(prescale) {
case 1: TCA0.SINGLE.CTRLA |= TCA_SINGLE_CLKSEL_DIV1_gc; break;
case 2: TCA0.SINGLE.CTRLA |= TCA_SINGLE_CLKSEL_DIV2_gc; break;
case 4: TCA0.SINGLE.CTRLA |= TCA_SINGLE_CLKSEL_DIV4_gc; break;
case 8: TCA0.SINGLE.CTRLA |= TCA_SINGLE_CLKSEL_DIV8_gc; break;
case 16: TCA0.SINGLE.CTRLA |= TCA_SINGLE_CLKSEL_DIV16_gc; break;
case 64: TCA0.SINGLE.CTRLA |= TCA_SINGLE_CLKSEL_DIV64_gc; break;
case 256: TCA0.SINGLE.CTRLA |= TCA_SINGLE_CLKSEL_DIV256_gc; break;
case 1024: TCA0.SINGLE.CTRLA |= TCA_SINGLE_CLKSEL_DIV1024_gc; break;
default: return;
}
TCA0.SINGLE.CTRLA |= TCA_SINGLE_ENABLE_bm;
}
if(pin == 6) {
TCB0_CTRLA &= ~TCB_ENABLE_bm;
TCB0_CTRLA &= ~TCB_CLKSEL_gm;
switch(prescale) {
case 1: TCB0_CTRLA |= TCB_CLKSEL_CLKDIV1_gc; break;
case 2: TCB0_CTRLA |= TCB_CLKSEL_CLKDIV2_gc; break;
case 64: TCB0_CTRLA |= TCB_CLKSEL_CLKTCA_gc; break;
default: return;
}
TCB0_CTRLA |= TCB_ENABLE_bm;
}
if(pin == 3) {
TCB1_CTRLA &= ~TCB_ENABLE_bm;
TCB1_CTRLA &= ~TCB_CLKSEL_gm;
switch(prescale) {
case 1: TCB1_CTRLA |= TCB_CLKSEL_CLKDIV1_gc; break;
case 2: TCB1_CTRLA |= TCB_CLKSEL_CLKDIV2_gc; break;
case 64: TCB1_CTRLA |= TCB_CLKSEL_CLKTCA_gc; break;
default: return;
}
TCB1_CTRLA |= TCB_ENABLE_bm;
}
}
#endif
#endif