Can't set to Fast PWM TOP=OCRA mode

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

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

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.

  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();

MCU=Atmega328.

...doesn't work in non-inverting mode.

TCCR2A |= _BV(WGM20) | _BV(WGM21) | _BV(COM2A0) | _BV(COM2A1); // Inverting mode fails

Which mode?

Both, inverting and non inverting mode does not work. It was typo in comments.

Apparently I was too terse... Which mode do you want to get working?

Fast PWM when TOP=OCRA. I am trying to get desired frequency on OCA pin.

acid:
Fast PWM when TOP=OCRA. I am trying to get desired frequency on OCA pin.

You stated that was working in Compare Output Mode = Toggle so I guess we're done.

but I need inverting mode and I want to understand why those modes are not working according to documentation.

Excellent. The goal is get inverting output mode working on timer 2 at a frequency of 123 Hz on an ATmega328P processor. Off to the datasheet...

Are you expecting an inverted output on the OC2A pin?

If yes then you will have to adjust your expectations. In Waveform Generation Mode = 7 (Fast PWM, TOP = OCRA) you essentially give up the A channel because the OCRA register is used to specify TOP. The B channel (OC2B pin) will work the way you expect.

Thanks for explanation, but something is confusing for me...

  1. Why COM2A1:0 = 1 (Toggle OC2A Mode) is giving me frequency? In this mode WGM22:0 = 7 and OCR2A also busy with holding TOP value.
  2. Documentation says:

In non-inverting Compare Output mode, the Output Compare (OC2x) is cleared on the compare match
between TCNT2 and OCR2x, and set at BOTTOM. In inverting Compare Output mode, the out-put is set on compare match and cleared at BOTTOM.

This means both, OCR2A and OCR2B can hold TOP values.

acid:

  1. Why COM2A1:0 = 1 (Toggle OC2A Mode) is giving me frequency? In this mode WGM22:0 = 7 and OCR2A also busy with holding TOP value.

The counter increments from zero (BOTTOM) to the value in the OCR2A register (TOP). When the counter reaches the value in the OCR2A register (TOP), the counter is reset to zero (BOTTOM).

When the Compare Output Mode is Toggle, the output pin is toggled when the counter value equals the value in the OCR2A register (which also happens to be TOP).

The counter reaches OCR2A (TOP), the pin is toggled, the counter value is reset.

  1. Documentation says:

In non-inverting Compare Output mode, the Output Compare (OC2x) is cleared on the compare match
between TCNT2 and OCR2x, and set at BOTTOM. In inverting Compare Output mode, the out-put is set on compare match and cleared at BOTTOM.

This means both, OCR2A and OCR2B can hold TOP values.

No. "TOP" is the label used for the value at which the counter is reset. It has nothing to do with the output. Only the OCR2A register is used for the TOP value.

OCR2A and OCR2B registers are used for the value at which the "output action" is performed. This has nothing to do with when the counter is reset.