Electronic – Can’t set to Fast PWM OCRA mode

atmegaavrcinterruptstimer

Trying to have Fast PWM mode when TOP == ORCA.

Works fine with OCA toggle (measuring correct frequency), but doesn't work in non-inverting mode.

cli();

// Fast PWM Mode 
TCCR2A = 0;
TCCR2A |= _BV(WGM20) | _BV(WGM21) | _BV(COM2A0) | _BV(COM2A1); // Inverting mode fails
//TCCR2A |= _BV(WGM20) | _BV(WGM21) | _BV(COM2A1); // Non-Inverting mode fails
//TCCR2A |= _BV(WGM20) | _BV(WGM21) | _BV(COM2A0); // Toggle mode OK

// Prescaler set to 1024 (p.162)
TCCR2B = 0;
TCCR2B |= _BV(CS20) | _BV(CS21) | _BV(CS22) | _BV(WGM22);

// Enable interrupt on counter match
TIMSK2 |= _BV(OCIE2A);

OCR2A = 127;

sei();

Update with more information:

MCU=Atmega328. In non-inverting mode I just got steady 1 on output and inverting mode gives logical 0. I was expecting to see the frequency

$$F = 16*10^6 / (1024 * 127) = 123Hz$$

Best Answer

I will answer myself. OC2A pin is not generating frequency, because OCR2A is busy holding TOP value. It can hold either TOP value or output compare value. So I should use OCR2B.