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

    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

  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

    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.

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: 3 channel PWM with customize duty cycle

    So, i need to define new variable?? let say

    Duty2 = Duty3

    so my code will be like this:

    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 = Duty3
    next DUTY1

    CCP1CON.4 = Duty3.0 ' Setup 10-bit duty cycle as
    CCP1CON.5 = Duty3.1 ' a 10-bit word
    CCPR1L = Duty3 >> 2


    so each of the duty2 value will be store at the ccp1 register?
    am i right?

  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

    No, why would you do that? If you do Duty2 = Duty3 you assingn the value of Duty3 TO Duty2 and overwrites the value Duty2 got from the lookup table.

    I don't know how to explain this in any other way. You want Duty2 to contain the value from the Lookuptable and that's what it does after the lookup command has executed.
    Code:
    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,211,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
    Perhaps I don't understand the problem you're having? Have you actually tried it?

    /Henrik.

  5. #5


    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

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

  7. #7


    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

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