PDA

View Full Version : HPWM frequency range



jcsquire
- 15th June 2006, 15:11
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

paul borgmeier
- 15th June 2006, 16:35
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

Blind Al
- 15th June 2006, 20:17
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.

Charles Linquis
- 15th June 2006, 20:26
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

paul borgmeier
- 15th June 2006, 21:56
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

dhouston
- 15th June 2006, 22:49
(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.

jcsquire
- 16th June 2006, 02:11
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