HPWM frequency range


Closed Thread
Results 1 to 7 of 7
  1. #1
    jcsquire's Avatar
    jcsquire Guest

    Default HPWM frequency range

    Do any of you gurus know what the range of acceptable frequencies are for the HPWM command?

    Using a 628 I'd like to know if I can get the full 8bit precision range at 140kHz on a 4MHz crystal or if I have to move to a 20MHz crystal. The PBP documentation is a little fuzzy on the details...it lists two different minimum frequencies for the HPWM at 4MHz, and says nothing about the maximum. Reason says that as you bump up the frequency that you'll lose precision (e.g. an upper frequency of 400kHz could only be split 10 ways by a 4MHz crystal so you'd have only roughly 3 bits of precision)...is it that simple to figure out that at 140kHz I'd have only log2(4000/140) bits of precision on a 4MHz crystal?

    Also, if I change the duty cycle, does it always force the timer to restart at 0, or can I smoothly sweep the duty cycle?

    I'm sure one of you folks have hit this problem before! Thanks, - Jim

  2. #2
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    Using a 628 I'd like to know if I can get the full 8bit precision range at 140kHz on a 4MHz crystal or if I have to move to a 20MHz crystal.
    With a 4MHz Xtal, the closest you can get to 140kHz is 142,857 Hz. This gives a duty cycle resolution of just under 5 bits.

    With a 20HMz xtal, the closest you can get to 140kHz is 138,888. This gives a duty cycle resolution of just over 7 bits.

    ...is it that simple to figure out that at 140kHz I'd have only log2(4000/140) bits of precision on a 4MHz crystal?
    almost, see equation in datasheet for calculating resolution

    Also, if I change the duty cycle, does it always force the timer to restart at 0, or can I smoothly sweep the duty cycle?
    No, the current cycle completes so you should get a smooth transition.

    See the datasheet for source of my calcs and information. If you need help calculating other options, please reply back.

    Good Luck,

    Paul Borgmeier
    Salt Lake City, Utah
    USA

  3. #3
    Join Date
    Aug 2004
    Location
    Santa Clara Calif. USA Silicon Valley
    Posts
    6


    Did you find this post helpful? Yes | No

    Talking Hpwm Max Frequency

    Hi Folks

    I looked at the 2.45 pbp manual and it says the max HPWM freq using any crystal is 32.767KHZ. Am I misreading the post or has this changed with the newer version? I would love to get HPWM working at 40KHZ for IR remote control apps.

  4. #4
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    The solution is to not use the PBP HWPWM command at all. Simply write to the registers directly. For example:

    PR2 = $1F ' PWM register, 158 Khz@20Mhz, 7 bits
    T2CON = %00000100 ' Prescale 1 - Needed for PWM
    .
    .
    .
    CCPR1L = PWMVal >>2 ' Divide Value by 4
    CCP1CON.5=PWMVal.1
    CCP1CON.4=PWMVal.0
    Charles Linquist

  5. #5
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Smile

    I too, must admit I never use the HPWM command. I write to the registers directly just as Charles suggested. Most (if not all) of the PICs that have hardware PWM have an easy to follow 4 step procedure for setting the registers correctly. (e.g., for the 16F628A, see section 9.3.3). In addition to Charles' example check out MElabs example here

    http://www.melabs.com/resources/samples/pbp/hardpwm.bas

    (Yes, the calcs are a tad confusing at first, but once you get it you will never go back.)

    Paul Borgmeier
    Salt Lake City, Utah
    USA

  6. #6
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by paul borgmeier
    (Yes, the calcs are a tad confusing at first, but once you get it you will never go back.)
    I find the formulas obfuscate things. Using the MElabs example, with OSC=4MHz each instruction cycle takes 1µS. The period for 1kHz is 1/1000=.001=1000µS=1000 instruction cycles. With the prescaler set to 4, 1000 becomes 250 and since it's Base0 it becomes 249. The duty cycle is the number of instruction cycles that the output is high. 20% of 1000 = 200 and 80% of 1000 = 800. All you are doing is defining the period and duty cycle in numbers of instruction cycles. Once you grasp what's involved you can do the calculations in your head.
    Last edited by dhouston; - 15th June 2006 at 23:02.

  7. #7
    jcsquire's Avatar
    jcsquire Guest


    Did you find this post helpful? Yes | No

    Default Thanks a LOT!

    Paul, Charles, dHouston: Thanks so much for not just answering my question but guiding me back to the literature so that I can figure out future questions for myself. I (and I'm sure Blind Al too) consider myself very lucky to have such expertise available.
    - Jim

Similar Threads

  1. HPWM command and oscillator frequency
    By RussMartin in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th March 2009, 22:41
  2. HPWM of the port of two HPWM
    By ja2rqk in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 25th April 2007, 15:05
  3. inaccurate frequency using TMR1 PI18F452
    By nkarpovich in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 14th October 2006, 16:22
  4. sound command & music
    By trying in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 26th May 2006, 14:14
  5. 3 HPWM channels
    By docwisdom in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 4th April 2006, 02:43

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