3 channel PWM with customize duty cycle


Closed Thread
Results 1 to 40 of 57

Hybrid View

  1. #1


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    yes i tried simulate the program using real pic simulator.
    so my codes is below:

    DEFINE OSC 20
    DUTY1 VAR WORD
    DUTY2 VAR WORD ' Duty cycle value (CCPR1L:CCP1CON<5:4>)

    TRISC.2 = 0 ' Set PORTC.2 (CCP1) to output
    CCP1CON = %00001100 ' Set CCP1 to PWM
    T2CON = %00000101 ' Turn on Timer2, Prescale=4

    PR2 = 249 ' Set PR2 to get 1KHz out

    Main:
    'Set up a loop for 45 iterations, Duty1 will count 0,1,2,3,4....44
    For DUTY1 = 0 TO 44

    'Go into the table and retrieve the value pointed at by Duty1 and put that value in Duty2

    LookUp DUTY1, [0,18,35,53,70,87,104,120,135,150,164,177,190,201,2 11,221,229,236,_
    243,247,251,254,255,254,251,247,243,236,229,221,21 1,201,189,_
    177,164,150,135,120,104,87,70,53,35,18,0],DUTY2


    'Duty2 now contains the value from the lookup table that Duty1 points at. First time thru the loop it's 0 second time it's 18 and so on.
    CCP1CON.4 = DUTY2.0 ' Setup 10-bit duty cycle as
    CCP1CON.5 = DUTY2.1 ' a 10-bit word
    CCPR1L = DUTY2 >> 2
    Pause 10 'Use each dutycycle value 10ms before going to the next one.
    Next DUTY1
    GoTo Main 'And do it all over again

    End


    but turns out the duty cycle doesnt come out fully as in the lookup table.
    the PWM generated is not completed.
    maybe i miss something important?
    please help check

    photoelectric

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    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.

  3. #3


    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

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    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.

  5. #5


    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

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,612


    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.

  7. #7


    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

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