I tried to use timer interrupt. Start setting frequency 1kHZ on Timer 2B. It is working fine.
But then I tried to change timer interrupt frequency and results are strange.
Changing TCCR2B value work as expected, but changing OCR2B do nothing.
First I observed only values in Serial.print(), next I added digital.Write() and connected oscilloscope to D6.
Detailed frequency values shown in comment below code.
Please check my code, and tell me what I do wrong.
Please check my comments below code, I expected that if I change OCR2B value from 124 to 24 I will have 5x higher frequency. For example TCCR2B = 0x03 (divider /32) and OCR2B = 124 - I expected frequency 8MHz/(2 * 32 * (124+1)) = 1kHz and it was OK. But with TCCR2B = 0x03 OCR2B = 24 I expected 8MHz/(2 * 32 * (24+1)) = 5kHz, but I still get 1kHz.
The same with TCCR2B = 0x01 (/1). OCR2B = 124, frequency 8MHz/(2 * 1 * (124+1)) = 32kHz - OK, TCCR2B = 0x01, OCR2B = 24, still 32kHz, not 80kHz as I expected.
Why?
Just these comments show me that you confused OCR2B with something else. Please name the timer mode you are using and which registers determine the output frequency.
Suppose that you run it on atmega328.
You didn't set the Timer mode, so the timer still in Normal one. In the Normal mode the counter run from 0 to 0xFF. The frequency doesn't depend on OCR2B value.
Yes, Nick Gammon's coverage of the subject is worth reading and is possibly overlooked.
I particularly liked his comprehensive table of values for setting a wide range of frequencies for PWM.
You can always be certain that Nick's code will work.