50Khz PWM


Closed Thread
Results 1 to 4 of 4

Thread: 50Khz PWM

  1. #1
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305

    Default 50Khz PWM

    18F2550
    48MHz

    I have set up CCP1 to provide a 50KHz PWM using the code below. However I need to use a 10bit number for the duty cycle. How do I implement that? Do I change
    Code:
    high_duty=(PWM_SIGNAL>>2)
    to
    Code:
    high_duty=(PWM_SIGNAL)
    and also
    Code:
    low_duty=(PWM_SIGNAL<<6)
    to
    Code:
    low_duty=(PWM_SIGNAL<<8)
    ?

    Code:
     PWM_SIGNAL = 255CCP1CON = 111100
    PR2 = 239
    high_duty=(PWM_SIGNAL>>2)        'high 6 bits in CCPR1L
          low_duty=(PWM_SIGNAL<<6)         'low two bits for CCP1CON
          low_duty=(low_duty>>2)           'shift back to CCP1CON<5:4>
          low_duty.3=1                     'PWM configuration bit
          low_duty.2=1                     'PWM configuration bit
    CCPR1L=high_duty
    CCP1CON = $0C
    T2CON = 4
    Thanks.

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


    Did you find this post helpful? Yes | No

    Default Re: 50Khz PWM

    Hi,
    You won't get a full 10bit resolution at that frequency but it'll be pretty close. But you do
    Code:
    Duty VAR WORD
    Duty = 500  'Or whatever
    
    CCP1CON.4 = Duty.0
    CCP1CON.5 = Duty.1
    CCPR1L = (Duty >> 2)
    /Henrik.

  3. #3
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: 50Khz PWM

    Thanks Henrik. I implemented your changes as shown below but this seems to stop the duty cycle at 25% or 256. It then rolls over to 0.

    Code:
    CCP1CON = %00111100
    PR2 = 239
    high_duty=(PWM_SIGNAL>>2)        'high 6 bits in CCPR1L
          low_duty=(PWM_SIGNAL<<6)         'low two bits for CCP1CON
          low_duty=(low_duty>>2)           'shift back to CCP1CON<5:4>
          low_duty.4=0                     'PWM configuration bit
          low_duty.5=1                     'PWM configuration bit
    CCPR1L=high_duty
    CCP1CON = $0C
    T2CON = 4

  4. #4
    Join Date
    Feb 2004
    Location
    Michigan, USA
    Posts
    305


    Did you find this post helpful? Yes | No

    Default Re: 50Khz PWM

    I had PWM_SIGNAL defined as a BYTE not WORD.

    Solved.

Members who have read this thread : 1

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