Search code examples
cavr

Why timer0 of atmega16a have frequency of 46.86 kHz instead of 12 MHz?


Recently bought a new USB oscilloscope and tried to measure the frequency of avr timer0. There is 12 MHz crystal oscillator connected to atmega and timer0 set up to fast PWM mode without prescaler. Here is the code:

#include <avr/io.h>

int main(void)
{
    DDRB = 0x08;
    TCCR0 |= (1<<WGM00)|(1<<WGM01)|(1<<COM01)|(1<<CS00);
    OCR0 = 128;
    while(1){

    }
}

But on oscilloscope frequency equals 46.90 kHz. enter image description here

Fuse bits are on the next image: enter image description here

But if I blink a LED with a delay of 1 ms, I got frequency around 2 MHz.

So, please explain to me, what's going on, and why timer works wrong (or where I'm wrong).


Solution

  • The frequency seems correct to me. If your clock is 12MHz and you have an 8 bit PWM your PWM frequency is actually 12MHz/256 = 46.875kHz.