Duplicating timer 2 behaviour using timer 0

Hello forum, I have read all of the Arduino help pages I could find, as well as the microcontroller datasheet, but I'm still having trouble wrapping my head around how all of this works. Long story short for reasons I won't go into I'm forced to use the pins 5 and 6 instead of 3 and 11, ergo I have to use timer 0 for our peripheral PWM control instead of timer 2. But... I'm having a hard time making timer 0 behave the same way as timer 2 did. I've been writing to the registers directly before using the code:

// Setup
TCCR2A |= _BV(COM2A1) | _BV(COM2B1);

// Control
OCR2A = value1;
OCR2B = value2;

Now I'm using this code, compiled using the help pages and some trial and error:

// Setup
TCCR0A |= _BV(COM0A1) | _BV(COM0B1) | _BV(WGM00);
TCCR0B = _BV(CS02);

// Control
OCR0A = value1;
OCR0A = value2;

I noticed that my peripheral devices (motors) are twice as sensitive to the values I feed into the registers, which I figured means that the frequency is double as well. However I'm having the hardest time halving it or otherwise fixing the code to make it function the same as before.

Maybe I've just spent too much time on this and my brain is fried :astonished::confused: I bet the solution is really simple but I'm just not seeing it. Any and all help will be much appreciated. Thank you!

When you are using Timer2, you are using the default prescaler of 64, and the default pwm mode which is phase correct 8 bit, where the timer counts up to 255 and back down again. If you work out the total counts(510) and the prescaler(64) and the 16MHz clock tick of .0625 us, you can see why the default frequency is 490 Hz.

When you switch to timer0, you have to set it up the same way.

You are correct in setting the two output pins for behavior on compare match, but your waveform and prescaler are incorrect.

TCCR0B = _BV(CS02);

This is setting the timer0 prescaler to 256. It's confusing because the prescaler settings are different between timer0 and timer2 and copying the timer2 prescaler sent you wrong. To set timer0 to prescaler 64 you need

TCCR0B = _BV(CS01) | _BV(CS00);

You were correct in setting the waveform mode WGM00, but because of the register setup with |= the default was not cleared, you were still in fast pwm mode.

TCCR0B = _BV(CS01) | _BVCS(00);
TCCR0A = _BV(COM0A1) | _BV(COM0B1) | _BV(WGM00);

// Control
OCR0A = value1;
OCR0B = value2; //fix typo

Oh man you are a life saver, it works perfect! You saved me a ton of desperate trial and error with the different register configurations, cheers!