Arduino mega 2560的PWM调整

参考:http://sobisource.com/arduino-mega-pwm-pin-and-frequency-timer-control/

Arduino mega pins and hardware Timers

Pin Timer
46 OC5B
45 OC5B
44 OC5B
13 OC0B //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 *there’s alot of misinfo about Pin-13 but after checking 13
timer 1 —– pin 11, 12, 13 it is in fact on Timer 0 “Thx Max K.”
timer 2 —– pin 9, 10
timer 3 —– pin 2, 3, 5
timer 4 —– pin 6, 7, 8
timer 5 —– pin 44, 45, 46

Timer与PWM的对应关系

TIMER 0 (Pin 4, 13)

Value Divisor Frequency
0x01 1 62.5035 KHz
0x02 8 7.8125 KHz
0x03 64 976.5 Hz // default
0x04 256 244.1 Hz
0x05 1024 61.0 Hz

Code: TCCR0B = (TCCR0B & 0xF8) | value ;

TIMER 1 (Pin 11, 12)

Value Divisor Frequency
0x01 1 31.374 KHz
0x02 8 3.921 KHz
0x03 64 490.1 Hz // default
0x04 256 122.5 Hz
0x05 1024 30.63 Hz

Code: TCCR1B = (TCCR1B & 0xF8) | value ;

TIMER 2 (Pin 9, 10)

Value Divisor Frequency
0x01 1 31.374 KHz
0x02 8 3.921 KHz
0x03 32 980.3 Hz
0x04 64 490.1 Hz // default
0x05 128 245 hz
0x06 256 122.5 hz
0x07 1024 30.63 hz
Code: TCCR2B = (TCCR2B & 0xF8) value ;

TIMER 3 ( Pin 2, 3, 5)

Timers 3, 4, 5 Oddly do not use the divisors the data sheet says: Below are actual measured values!

Value Divisor Frequency
0x01 1 31.374 KHz
0x02 8 3.921 Khz
0x03 64 490.1 Hz // default
0x04 256 122.5 Hz
0x05 1024 30.63 Hz
Code: TCCR3B = (TCCR3B & 0xF8) value ;

TIMER 4 (Pin 6, 7, 8)

Value Divisor Frequency
0x01 1 31.374 KHz
0x02 8 3.921 Khz
0x03 64 490.1 Hz // default
0x04 256 122.5 Hz
0x05 1024 30.63 Hz

Code: TCCR4B = (TCCR4B & 0xF8) | value ;

TIMER 5 (Pin 44, 45, 46)

Value Divisor Frequency
0x01 1 31.374 KHz
0x02 8 3.921 Khz
0x03 64 490.1 Hz // default
0x04 256 122.5 Hz
0x05 1024 30.63 Hz

Code: TCCR5B = (TCCR5B & 0xF8) | value ;

change

void setup() {
  ...
  int fff = 6;  
  TCCR2B = (TCCR2B & 0xF8) | fff;
  int fff1 = 4;  
  TCCR4B = (TCCR4B & 0xF8) | fff1 ;
  ....
}

你可能感兴趣的:(Arduino mega 2560的PWM调整)