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,617


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    Hi,
    Then it's either a problem with how you write the value to the dutycycle register or a problem with the simulator. I can't see any problem with the code so I'm leaning towards the simulator....

    I've tried the following here:
    Code:
    Duty1 VAR WORD
    Duty2 VAR WORD
     
    For Duty1 = 0 to 44
      LookUp DUTY1, [0,18,35,53,70,87,104,120,135,150,164,177,190,201,211,221,229,236,_
      243,247,251,254,255,254,251,247,243,236,229,221,211,201,189,_
      177,164,150,135,120,104,87,70,53,35,18,0],DUTY2
     
      HSEROUT [#Duty2,13] 
    
      Pause 10
    NEXT
     
    Pause 100
    END
    As expected this outputs the Duty2 value just fine which proves that the lookup part works. So again, the problem is either with the code that writes the value to the dutycycle register or with the simulator.

    /Henrik.

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    hi,

    thanks for your reply.
    after checking its turn out the value in my lookup was abit wrong due to miscalculation.
    before i starts calculating back my lookup table, can you explain abit about the timer prescale,
    as you can see from my coding i decide to use timer prescale 16 instead of 4 or 1 stated in the datasheet, but i dont really understand what is timer prescale for.

    FYI, i wan to set my PWM to 5kHz using osc 20MHz

    please help,
    tq

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


    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.

  4. #4


    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

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


    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.

  6. #6


    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

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


    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.

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