Hi!
I don't know the divisor for 20 KHz but, try this method:
Arduino mega pins and hardware Timers
Pin Timer
46 OC5A
45 OC5B
44 OC5C
13 OC0A //Caution: this one directly effects major timing { i.e delay and millis}
12 OC1B
11 OC1A
10 OC2A
9 OC2B
8 OC4C
7 OC4B
6 OC4A
5 OC3A
4 OC0B //Caution: this one directly effects major timing { i.e delay and millis}
3 OC3C
2 OC3B
All that is really important above is the numbers. They tell you what timers there on.
For example pin 2 is OC3B which is timer 3.
another way to look at this is:
timer 0 —– pin 4, 13
timer 1 —– pin 11, 12, 13
timer 2 —– pin 9, 10
timer 3 —– pin 2, 3, 5
timer 4 —– pin 6, 7, 8
timer 5 —– pin 44, 45, 46
————————————————————————————–
TIMER 0 (Pin 4, 13)
Value Divisor Frequency
0×01 1 62.5035 KHz
0×02 8 7.8125 KHz
0×03 64 976.5 Hz // default
0×04 256 244.1 Hz
0×05 1024 61.0 Hz
Code: TCCR0B = (TCCR0B & 0xF8) | value ;
————————————————————————————-
TIMER 1 (Pin 11, 12)
Value Divisor Frequency
0×01 1 31.374 KHz
0×02 8 3.921 KHz
0×03 64 490.1 Hz // default
0×04 256 122.5 Hz
0×05 1024 30.63 Hz
Code: TCCR1B = (TCCR1B & 0xF8) | value ;
————————————————————————————–
TIMER 2 (Pin 9, 10)
Value Divisor Frequency
0×01 1 31.374 KHz
0×02 8 3.921 KHz
0×03 32 980.3 Hz
0×04 64 490.1 Hz // default
0×05 128 245 hz
0×06 256 122.5 hz
0×07 1024 30.63 hz
Code: TCCR2B = (TCCR2B & 0xF8) | value ;
—————————————————————————————-
Timers 3, 4, 5 Oddly do not use the divisors the data sheet says: Below are actual measured values!
TIMER 3 ( Pin 2, 3, 5)
Value Divisor Frequency
0×01 1 31.374 KHz
0×02 8 3.921 Khz
0×03 64 490.1 Hz // default
0×04 256 122.5 Hz
0×05 1024 30.63 Hz
Code: TCCR3B = (TCCR3B & 0xF8) | value ;
—————————————————————————————-
TIMER 4 (Pin 6, 7, 8)
Value Divisor Frequency
0×01 1 31.374 KHz
0×02 8 3.921 Khz
0×03 64 490.1 Hz // default
0×04 256 122.5 Hz
0×05 1024 30.63 Hz
Code: TCCR4B = (TCCR4B & 0xF8) | value ;
—————————————————————————————-
TIMER 5 (Pin 44, 45, 46)
Value Divisor Frequency
0×01 1 31.374 KHz
0×02 8 3.921 Khz
0×03 64 490.1 Hz // default
0×04 256 122.5 Hz
0×05 1024 30.63 Hz
Code: TCCR5B = (TCCR5B & 0xF8) | value ;
—————————————————————————————-
To set your timer just add it to your sketch:
Here’s and example for Pins 9 and 10 on Timer 2 @ 490.1Hz;
void setup () {
TCCR2B = (TCCR2B & 0xF8) | 0×04;
}
Source:
http://sobisource.com/arduino-mega-pwm- ... r-control/Best regards.
I have a problem to post this message ignore it please.
blablablablablablablablablablablablablablablablablablablablablablablablalablablablablablablablablablalablablablablablablablablablalablablablablablablablablabla