Electronic – AVR PWM timer frequency

attinyavrcpwm

I'm learning how to use an Attiny84. I've generated a fast pwm signal. I managed to understand how to do this:

#include <avr/io.h>

int main(void)
{
  // setup timer0
  TCCR0B |= _BV(CS00);  // use clk_i/O without prescalers as clock source.
                        // also enables TIMER0

  // set fast PWM mode
  TCCR0A |= _BV(WGM01) | _BV(WGM00);  // Mode 3
  TCCR0A |= _BV(COM0A1) | _BV(COM0A0);  // set OC0A on compare match, clear on BOTTOM

  OCR0A = 0xff / 2;  // 50% PWM

  // set PB2 as output. This should enable OC0A
  DDRB |= _BV(PB2);

  for (;;) {
  }
}

However, I don't understand why I got only ~30kHz on the scope. I disabled the CKDIV8 fuse and sets my CKSEL to "Calibrated Internal RC Oscillator 8MHz".

I though I could have at least 8MHz PWM frequency. I can't find in the documentation how clk_i/o and clk_cpu are related. Is there any way to gain a higher PWM frequency ?

N.B.: I don't need to have a particular PWM frequency, I'm just testing the chip and trying to understand its boundaries.

Best Answer

You have a 8MHz clock, but 8-bit PWM. This results in a 8MHz/256=31.25kHz output frequency. If you need a higher output frequency then you can use mode 7 instead, at the cost of PWM signal depth.