Choosing Period and Duty Cycle (PWM)

Discussion in 'Embedded Systems and Microcontrollers' started by KansaiRobot, Jul 8, 2010.

  1. KansaiRobot

    Thread Starter Active Member

    Jan 15, 2010
    Hello everybody.
    I would like to ask about the period and duty cycle quantities when generating a PWM signal.

    1) First I am using a PIC18F2550 so I suppose that my Fosc = 1MHz. (I read this on the data sheet, is there a way to check this??)

    2) Now I have to set the PWM period or frequency (the inverse).
    How do I set this? What quantities are recommended for PWM period?
    (i am planning to use this to control a motor, but in the meantime I am also trying to control a LED brightness too)

    I heard there are some recommendations for choosing a period so that the motor does not generate a human audible sound?

    I would really much appreciate your thoughts on this, I have read already PLENTY of pages on PWM and I know the concept but the points above are still unclear

    Thanks in advance

  2. jpanhalt


    Jan 18, 2008
    You set the oscillator frequency in the program (OSCCON register). See Section 2 of the datasheet. 1 MHz is an option and is the default for the internal oscillator. For motor control, it doesn't need to be that accurate.

    There can be a lot of debate about the best frequency to use for motor control. I prefer the higher frequencies (e.g., 20KHz) as that avoids most of the annoying whine. The higher frequencies require more switching per second. As the transitions are not instantaneous, heat is produced during each cycle. Thus, the higher frequencies may require better drivers for your mosfets than lower frequencies. You may also need bigger heat sinks.

  3. retched

    AAC Fanatic!

    Dec 5, 2009
    This is what can happen if your motor PWM is in the audible range:

    If you are already filtering a particular frequency later in the system, you can choose that as your PWM frequency. To kill two birds with one stone.

    If you were building a hi-fi record player, and you already have a 24kHz filter before the amp circuits, you can use the 24kHz for the motor, and the existing filter will clean up the motor hum. (Which should be inaudible at that frequency)
  4. Tahmid

    Active Member

    Jul 2, 2008
    Take a look over here:

    The formula is like this:
    Code ( (Unknown Language)):
    1. PR2 = [Fosc / (Fpwm * 4 * N)] - 1
    Fpwm is desired frequency.
    Fosc is system frequency(crystal/resonator/RC).
    N is timer prescaler.
    You won't be able to effectively produce 50Hz with the CCP module. You should be using software to generate this, or an IC.
    Code ( (Unknown Language)):
    1. CCPR1L = (DC/100)*PR2
    DC = duty cycle in percentage

    I would go for 25kHz to avoid the audible range.

    Hope this helped.