3 channel PWM with customize duty cycle


Results 1 to 40 of 57

Threaded View

  1. #20


    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