Atmega328p Fast PWM. Getting unexpected frequency output.

I’m trying to follow a guide on setting up fast PWM on an Atmega328p. The guide is here: PWM On The ATmega328 - QEEWiki

I’ve put in the example code witch should output a frequency of 8Mhz (16Mhz/8/256=7812.5Khz). I’m measuring only 976Hz as if there was an extra divider or larger prescaler than was specified. I’m using a sound card scope to measure the frequency.

I reburned the bootloader and even verified with the board detector sketch that the fuse settings are correct. Fuses are set to FF DE FD as specified in boards.txt

I have this code in the IDE:

void setup() 
    DDRD |= (1 << DDD6);
    OCR0A = 128;
    // set PWM for 50% duty cycle

    TCCR0A |= (1 << COM0A1);
    // set none-inverting mode

    TCCR0A |= (1 << WGM01) | (1 << WGM00);
    // set fast PWM Mode

    TCCR0B |= (1 << CS01);
    // set prescaler to 8 and starts PWM

    //16MHz/8/256 = 7812.5Khz

void loop() 
  // put your main code here, to run repeatedly:


Why am I not getting the desired frequency?

I'm trying to follow a guide on setting up fast PWM on an Atmega328p.

Looks like you are setting up Timer 0.
This is setup by the Arduino boot for timing, so the registers will already have some bits set.

If you really want to set up Timer0, you will have to write complete register values (not just OR-in in bits)

Also, you probably want to set WGM00, WGM01 and WGM02 (in TCCR0B) for Fast PWM using OCRA.


What pin are you looking at?
Are you using a UNO?

Tom.... :slight_smile:

Ok, I see that if I set TCCR0A and TCCR0B to 0 first then it works as expected.
If the arduino bootloader uses timer0 for its own timing then I will try to use timer1 instead.