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.


// 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

OCR2A = 127;


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$$

  • \$\begingroup\$ What happens exactly when it "doesn't work"? What MCU is it? And what was the expected behavior as per you? The question needs a bit of clarity please. \$\endgroup\$ – Anindo Ghosh Nov 20 '12 at 23:06
  • \$\begingroup\$ Sorry, updated with clarifications. \$\endgroup\$ – Pablo Nov 20 '12 at 23:11
  • \$\begingroup\$ Worth to mention that Fast PWM (0xFF) mode works fine(WGM22=0). \$\endgroup\$ – Pablo Nov 20 '12 at 23:15

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.


To solve your controller problem, try this:

I just used the source code example of


and it worked. But thanks to your explanations, I understand why it works ;-)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.