PWM frequency

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What is the PWM frequency generated by these codes(Atmega32u/16mhz)?

    TCCR1A = 0;           // undo the configuration done by...
    TCCR1B = 0;           // ...the Arduino core library
    TCNT1  = 0;           // reset timer
    TCCR1A = _BV(COM1A1)  // non-inverted PWM on ch. A
           | _BV(COM1B1)  // same on ch; B
           | _BV(WGM11);  // mode 10: ph. correct PWM, TOP = ICR1
    TCCR1B = _BV(WGM13)   // ditto
           | _BV(CS10);   // prescaler = 1
    ICR1   = 500;         // TOP = 500

I intend to control a motor and I read on the internet that the best frequency would be 500hz.

Last Edited: Thu. Jan 13, 2022 - 11:39 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

If the timer counts from 0 to 500 (probably should be 499 on fact) then back to 0 for one cycle it is 1000 steps. The CS10 doing /1 means the steps are 1/16000000 so the period is 1000/16000000 that is 62.5us so the frequency is 16kHz.

 

You want to use considerably more prescaling than CS10 if you want to be down near 500Hz. Try /32 instead. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Reading the datasheet, I didn't find a reference to 32, the closest would be 64.

The Arduino IDE generates 490.
Can the posted code generate this value?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You could go with /64 then set the ICR1 limit to 250 not 500. Alternatively /16 and an ICR1 of 1000