3 channel PWM with customize duty cycle


Closed Thread
Results 1 to 40 of 57

Hybrid View

  1. #1

    Default 3 channel PWM with customize duty cycle

    Hi ppl,

    im working on my PIC basic pro project to generate 3 channel pwm using PIC 16f737 family and i found some coding from previous thread. i test it and it works well.
    but the PWM generated used fixed value of duty cycle and i want to change into customize duty cycle by using lookup command in PICBAsic pro .

    my lookup table is below:

    For DUTY1 = 0 TO 44

    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

    next DUTY1

    and here is my 3 channel coding:

    ClearWDT

    DEFINE OSC 20
    ' Word vars for 10-bit value of each PWM duty cycle
    Duty1 VAR WORD ' Channel #1
    Duty2 VAR WORD ' #2
    Duty3 VAR WORD ' #3

    ' Set CCPx pins to outputs
    TRISC.2=0 ' CCP1 output
    TRISC.1=0 ' CCP2 output (could also be assigned to RB3)
    TRISB.5=0 ' CCP3 output

    ' Set CCP modules to PWM mode
    CCP1CON = %00001100 ' Mode select = PWM
    CCP2CON = %00001100 ' Mode select = PWM
    CCP3CON = %00001100 ' Mode select = PWM

    ' Set period up for 1.22kHz PWM freq
    PR2 = 249

    ' Set TMR2 up for 1:16 prescale & turn it on
    T2CON = %00010101 ' TMR2 ON 1:16 prescale

    Duty1 = 512 ' 50% duty cycle. 1024/2 "10-bit resolution"
    CCP1CON.4 = Duty1.0 ' Setup 10-bit duty cycle as
    CCP1CON.5 = Duty1.1 ' a 10-bit word
    CCPR1L = Duty1 >> 2

    Duty2 = 512
    CCP2CON.4 = Duty2.0
    CCP2CON.5 = Duty2.1
    CCPR2L = Duty2 >> 2

    Duty3 = 512
    CCP3CON.4 = Duty3.0
    CCP3CON.5 = Duty3.1
    CCPR3L = Duty3 >> 2

    End


    and my problem is i cannot combine the lookup command and the 3channel coding to generate the customize duty cycle ??
    can any1 help explaning how?

    thanks
    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,
    Well, if you have the lookup-table in a for-next loop like you are currently showing it will run thru that loop and then continue with the reset of the code. You have to move the NEXT so that it actually updates the dutycycles registers with the value retreived from the table before looking up the next value.

    For duty = 0 to 44
    Lookup.....
    Set dutycycle
    Pause whatever
    Next

    Then, in order to get you three phases you either need to have three lookup tables or you need to index the same lookuptable by offsetting the index by 1/3 for each phase and keep track of when to "roll over" and start at 0 again for each phase.

  3. #3


    Did you find this post helpful? Yes | No

    Unhappy Re: 3 channel PWM with customize duty cycle

    hi henrik, thanks for explaining.
    but i ve try ur method using below coding for 1 channel but the the output ccp wont work. its doesnt come out the correct dutycyle from the lookup table for my PWM.
    below are the example of my coding:

    For DUTY1 = 0 TO 44

    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

    Duty1 = DUTY2 ' 50% duty cycle. 1024/2 "10-bit resolution"
    CCP1CON.4 = Duty1.0 ' Setup 10-bit duty cycle as
    CCP1CON.5 = Duty1.1 ' a 10-bit word
    CCPR1L = Duty1 >> 2
    next DUTY1


    you see i declare the DUTY1 = DUTY2 for my dutycycle but it wont work


    please help assist.

    thanks

  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,
    You have Duty1 as the "index" for the lookup table. Duty1 is therefor expected to count 0, 1, 2, 3, 4...44 but you are overwriting Duty1 with the value of Duty2 so the indexing (the for-next loop) wont work properly.

    Use Duty2 directly when setting the dutycycle register or use another variable for indexing the lookup table (and don't overwrite it in the middle of the loop).

    /Henrik.

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    hi,

    yes i got your point now and correcting my coding now but then i realize how to define my dutycycle value from the lookup above since dutycyle register only accept numbers. example

    For DUTY1 = 0 TO 44

    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 = ?????? ' duty cycle. 1024/2 "10-bit resolution"
    CCP1CON.4 = Duty1.0 ' Setup 10-bit duty cycle as
    CCP1CON.5 = Duty1.1 ' a 10-bit word
    CCPR1L = Duty1 >> 2
    next DUTY1


    what should i define the duty2 value since i want it to read from lookup table?

    photoelectric

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    hi,

    yes i got your point now and correcting my coding now but then i realize how to define my dutycycle value from the lookup above since dutycyle register only accept numbers. example

    For DUTY1 = 0 TO 44

    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 = ?????? ' duty cycle. 1024/2 "10-bit resolution"
    CCP1CON.4 = Duty2.0 ' Setup 10-bit duty cycle as
    CCP1CON.5 = Duty2.1 ' a 10-bit word
    CCPR1L = Duty2 >> 2
    next DUTY1


    what should i define the duty2 value since i want it to read from lookup table?

    photoelectric

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

    Duty2 already contains the value from the lookuptable - that is what your Lookup does, it assignes the value pointed at by Duty1 to the variable Duty2 in this case.

    For each iteration thru the loop Duty2 will contain the value pointed at by Duty1 and since Duty1 counts 0,1,2,3...44 Duty2 will contain 0,18,35,53...0

    /Henrik.

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