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

    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

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

  3. #3


    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

  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

    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.

  5. #5


    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

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

  7. #7


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    hi,
    i stick with prescaler 4 and alrdy key in my dutycycle in my lookup using command lookup2 since it can support til 10bits wide.
    below is my complete coding for 1 channel PWM:

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

    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 5KHz out

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

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

    LookUp2 duty1, [0,18,34,52,68,85,102,119,137,152,170,187,203,220,2 36,253,267,285,302,318,334,_
    350,367,382,397,413,429,443,459,474,489,504,518,53 3,547,562,575,589,603,616,630,_
    643,655,669,681,693,706,718,729,740,752,764,774,78 5,795,806,815,825,835,844,853,_
    861,870,879,887,895,901,909,916,923,929,935,941,94 6,952,957,962,967,971,974,978,_
    981,984,987,990,992,994,996,997,998,999,999,1000,9 99,999,998,997,997,996,994,992,_
    990,987,984,981,978,974,971,967,962,957,952,946,94 1,935,929,923,916,909,901,895,_
    887,879,870,861,853,844,835,825,815,806,795,785,77 4,764,752,740,729,718,706,693,_
    681,669,655,643,630,616,603,589,575,562,547,533,51 8,504,489,474,459,443,429,413,_
    397,382,367,350,334,318,302,285,267,253,220,203,18 7,170,152,137,119,102,85,68,_
    52,34,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 1 'Use each dutycycle value 10ms before going to the next one.
    Next duty1
    GoTo Main 'And do it all over again

    End


    i compile then use real pic simulator to check the PWM pattern.
    seem like the duty cycle only read up to value 253 in my lookup table and then keep generating same dutycyle.
    did i miss sumthing in my code??

    attached is my screen shot of the pwm pattern. at the red area pwm duty is correct but at the yellow area it starts to generate same duty and not looking up to the value in the lookup2 command.

    please help assist

    photoelectric
    Attached Images Attached Images  

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