Trying to generate 2MHz PWM with PIC18F25K22


Closed Thread
Results 1 to 5 of 5

Hybrid View

  1. #1
    Join Date
    Jul 2012
    Posts
    5

    Default Trying to generate 2MHz PWM with PIC18F25K22

    Hello,

    This is my first post and I am trying to get the 18F25K22 to generate 2 MHz PWM 50% duty cycle on CCP1 (pin 12). I have followed the seven steps in the datasheet on page 186 by setting the appropriate registers with no output at all. My hardware is a 16MHz crystal and DEFINE OSC = 64 MHz using 4xPLL. I know it is capable of generating 2 MHz because the PicAxe 28x2 (a 18F25K22) does when driven with 64 MHz. I am using the register setup method because the PBP manual says the maximum HPWM frequency at any osc speed is 32,767Hz

    Here is the register setup that I am using:

    TRISC = %00000010 ' Set PWM bit 1 for input during setup
    CCPTMRS0 = %0000000 ' Set CCP1 to use Timer2
    PR2 = %00000111 ' Set Timer2 coumter to 7
    CCP1CON = %00101100 ' Set to PWM mode and 2 LSB bits of duty cycle
    CCPR1L = %00001100 ' Set MSB of duty cycle
    T2CON = %01000101 ' Set Prescaler to 1 & turn on PWM
    TRISC = %00000000 ' Set PWM bit for output to enable PWM

    I am using PBP 3.06 and the weird thing is that can't get the HPWM command to output anything either with just 20Khz.

    I have just started working with the 18F25K22 the last couple of days so I might be missing something dumb, please let know.

    Thanks,

    -Tim

  2. #2
    Join Date
    Jul 2012
    Posts
    5


    Did you find this post helpful? Yes | No

    Default Re: Trying to generate 2MHz PWM with PIC18F25K22

    Update,

    I realized that I was setting the two duty cycle registers wrong. It should be 127 for 50% so I think they should be set like this:

    TRISC = %00000010 ' Set PWM bit 1 for input during setup
    CCPTMRS0 = %0000000 ' Set CCP1 to use Timer2
    PR2 = %00000111 ' Set Timer2 counter to 7
    CCP1CON = %00111100 ' Set to PWM mode and 2 LSB bits of duty cycle
    CCPR1L = %00011111 ' Set MSB of duty cycle
    T2CON = %01000101 ' Set Prescaler to 1 & turn on PWM
    TRISC = %00000000 ' Set PWM bit for output to enable PWM

    With these changes there is still no output at all.

    -Tim

  3. #3
    Join Date
    Jul 2012
    Posts
    5


    Did you find this post helpful? Yes | No

    Default Re: Trying to generate 2MHz PWM with PIC18F25K22

    Ok,

    I found a dumb error. The HPWM is working and I should have been looking at pin 13, not 12. I notice that the PWM frequency is 16X the entered value of the HPWM command at 64MHz, i.e. 10kHz entered yields 160kHz output. This means that the maximum frequency I can get out using the HPWM command is 32767 x 16 = 524,272 Hz. I think I should be able to use the HPWM command to start everthing and then change the Timer2 register to change the frequency. Does anyone know if this is right? I tried this but no luck so far.

    -Tim

  4. #4
    Join Date
    Jul 2012
    Posts
    5


    Did you find this post helpful? Yes | No

    Default Re: Trying to generate 2MHz PWM with PIC18F25K22

    Problem solved,

    I found that I could enter 125000 Hz in the HPWM command if I set the compiler to long words. When the 125kHz gets multiplied by 16 due to the 64 MHz PLL clock I get 2 Mhz output on pin 13, just what I needed!

    Not sure why the register setup method does not work, but I am moving on for now.

    Regards,

    -Tim

  5. #5
    Join Date
    Jul 2012
    Posts
    5


    Did you find this post helpful? Yes | No

    Default Re: Trying to generate 2MHz PWM with PIC18F25K22

    One more follow up,

    I found another oops!, which explains the 16X of the HPWM command. Somewhere along the way my Define OSC 64 got commented out so the compiler did not know the correct clock speed. With the Define OSC 64 now present, and the compiler set to long words I can enter the HPWM 1,127,2000000 and get a nice 2 MHz PWM signal at 50% on pin 13.

    -Tim

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