Change PWM Frequency on Micro (or Leonardo)


My goal is to set the highest possible frequency of phase-correct PWM on pins 9, 10, 11 on an Arduino MICRO.

The following line fails to compile:

    TCCR2B = _BV(CS00); // change the PWM frequencey to 31.25 kHz  - pins 3 & 11 timer 2

The error message is:

error: ‘TCCR2B’ was not declared in this scope

There is no error if I try to set TCCR0B, TCCR1B, TCCR3B, TCCR4B, but 2,5,6,7,8,9 all produce the undefined error.

I have no idea what I am doing here, but my code worked perfectly on the Arduino Uno.

Any ideas or pointers of where to look would be great !

I am only trying to max out the PWM rates for phase-correct PWM on any 3 pins, they do not have to be pins 9,10,11. Those were the easiest to use on the UNO, but perhaps that is not the case on the Micro (or Leonardo) since they use the same CPU.

Any help would be great!

Thanks, Bob

The ATmega32u4 used on the Micro and Leonardo doesn't have a Timer2.

@James 4CS


I found these definitions in leonardo_pins.h

    TIMER0B,        /* 3 */
    TIMER3A,        /* 5 */
    TIMER4D,        /* 6 */
    TIMER1A,        /* 9 */
    TIMER1B,        /* 10 */
    TIMER0A,        /* 11 */
    TIMER4A,        /* 13 */

I tired to "transpose" the code from the UNO to the MICRO without having the slightest idea what any of this means. I saw that in the "PWM cheatsheet" and "Secrets of", only the "B" timer has its bit set to CS00 so I did not try to do it to the "A".

   // timer 1B: pin 9 & 10
    TCCR1B = _BV(CS00); // change the PWM frequencey to 31.25kHz   - pins 9 & 10 
    // timer 0B : pin 3 & 11
    TCCR0B = _BV(CS00); // change the PWM frequencey to 31.25 kHz  - pin 3 & 11

Do you know/think/guess if this code will work on the Mirco to set the frequency to max?

Thanks for your help! Ciao, Bob

I know one way to find out.

I assume you mean with an oscilloscope? I may have to order a DSO NANO...

Cheers, Bob