3 channel PWM with customize duty cycle


Closed Thread
Results 1 to 40 of 57

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,627


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    If you're using a 20Mhz oscillator the internal clock runs at 5Mhz (FOsc/4) and this is what "ticks" the timer that is used for the PWM generation. The prescalers job is to divide this 5Mhz frequency by whatever ratios are available for the particular PIC and/or timer used (normaly 1, 4 and 16 for TMR2).

    If you set the prescaler to 4 the timer is incremented at 5/4=1.25Mhz instead of 5Mhz.

    The datasheet for the PIC you're using has a section dedicated to the CCPModule in which they explain in great detail how it works and how to calculate the PWM period:

    PWM Period = (PR2 + 1) * 4 * TOsc * Prescaler

    In order to calculate the PR2 value for a specific frequency you have to rewrite the formula a bit and solve for PR2+1:

    PR2+1 = 1 / (TOsc * 4 * Prescaler * PWM Frequency)

    If you settle for a prescaler of 4 then this becomes:

    PR2+1 = 1 (0.00000005 * 4 * 4 * 5000) = 1/0.004 = 250 so in order to get a PWM frequency of 5kHz with a prescaler of 4 you should set PR2 to 249.

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    thank you for the explaning,
    so, can i conclude that it doesnt matter to set prescale 1,4 or 16 since the value of PR2 will be change??

    for instance,
    prescale = 16 ,PWM freq =5kHz for 20M Fosc
    from equation PR2+1 = 1 / (TOsc * 4 * Prescaler * PWM Frequency)
    my PR2 should be 63.5

    is it any different using prescale=4 with PR2=249 compare to the prescale=16 with PR2=63.5 because it turns out my PWM waveform doesnt seem to be same when i compare both

    photoelectric

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,627


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    Hi,
    It won't make any difference for the PWM frequency, it'll be 5kHz in both cases except you can't really set PR2 to 63.5 so you won't get exactly 5kHz.

    However, because the dutycycle is defined as the number of timer "ticks" the output is high in relation to the number of "ticks" it is low the prescaler setting also has an effect on the resolution because it makes the timer run slower so the total number of "ticks" in one period is lower.

    So setting the dutycycle register to x won't give you the same dutycycle output if you switch prescaler setting. If you want the highest possible resolution you should use the lowest possible prescaler value.

  4. #4


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    tq again,

    got your point now.
    let say i use prescaler 1 for maximum resolution, my PR2+1 should be 1000.
    So , let say i want to put 50% duty cycle in my lookup, my duty2 value should be:

    based on the below eqn:
    CCPR3L:CCP3CON<5:4>=(PR2+1)*4*Duty%
    my duty2 should be 1000*4*0.5=2000
    right?
    correct me if im wrong.

    photoelectric

  5. #5
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,627


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    Hi,
    That WOULD be correct but you can't put 1000 in PR2 since it's only 8 bits wide. This means that you can't get 5kHz with a prescaler of 1 so you'll have to use 4.

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    ok i got the idea right now.
    what about value in the lookup table? is it 8 bit wide or more because
    from my calculation of duty cycle:
    let say 50%
    prescaler 4
    PR2+1 =250

    so duty2 = 250*4*0.5 = 500

    and if the value of constant in lookup table only 8bit wide , what should i do?
    change to prescaler 16?? that would decrease my resolution..
    please help assist

    photoelectric

  7. #7
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,627


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    Hi,
    Have you had a look in the PBP manual?

    It says: The LOOKUP statement can be used to retreive values from a table of 8-bit constants.

    So, for LOOKUP you have to stick to 8bits in the table. You can do that by either use prescaler of 16 or stick with a prescaler of 4 and multiply the retreived value by 4 before putting it in the dutycycle register. Obviosuly both options reduces the effective resolution.

    If you need more resolution then take a look at the LOOKUP2 statement, it's also described in the manual.

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts