PDA

View Full Version : Low frequency PWM



Charlie
- 16th February 2011, 11:41
I'm building a simple touch controlled pulse width modulator with a 12F615. Everything is working happily, except the hardware PWM frequency has a lower limit of about 245 Hz with the onboard 4 MHz clock. I need something between 100 Hz and 125 Hz.
-I don't have a spare pin to connect an external reference to drop the clock to 2 MHz.
-Using the ADC to detect touch is a time sensitive operation, so using the PWM overflow interrupt to load new values and hence extend the PWM cycle messes up the touch sensors.
-The software PWM is too low frequency, and simply writing to the pin is too slow, mostly because I have too much to do between state changes.
Does anyone have any ideas, or do I simply need to find a different part?
Even then... SW PWM seems to top out at a few 10's of Hz, while hardware PWMs bottom out about 250 Hz. Maybe dropping the clock speed (hence a package with more pins) is the ONLY answer?

cncmachineguy
- 16th February 2011, 12:27
Well heres my thoughts:

If writing to a pin will cause too much delay, then software PWM is out anyway I think. I may be wrong, but it seems to me that is like a pause. What I mean is program waits for the pwm to finish before going further.

I am not sure of the above, someone PLEASE correct me if I am wrong!!

you say you want 100 to 125 Hz. Does that mean you can tolorate variation between the 2? ie: 112 Hz plus or minus 12 Hz?

You can get twice as much time to do things if you run the pic at 8MHz. Double the instructions executed in the same time.

Use timer 2 with a prescale set to 16, tmr2 output would then be 500K (@8Mhz). set the postscale to 16 and you will get a flag at 122Hz on TMR2IF with PR set to 255.

Just poll TMR2if in your main if you don't want to use interupts.

You haven't given any info as for the resolution you want for the duty cycle but I think you can play with PR to get your duty cycle. but I need to think about that some more.

Charlie
- 16th February 2011, 13:43
I can tolerate variation in the absolute frequency (say 112 +/- 12 or even a little more) as long as the on / off ratio is stable. I have flexibility in the resolution too - I've been targetting 11 (ish) ratio positions including 0%, 10%, 20%, ... 100%. I need full off and full on.

Polling TMR2 is an interesting idea I have not explored - I'll play with that for a bit and see where I get to. Thanks for a new lead!

cncmachineguy
- 16th February 2011, 14:31
Using the polling method the "stability" will be dependent on the duration of the main loop.

Absolute on and off is easy, set or clear the output

if you use a PR value so TMR2IF sets 10 times per period, you can use a counter to decide when to turn the output on/off

Again accuracy will depend on loop duration, but running at 8Mhz will more then likely be plenty fast enough to give you a barely noticable error.

Ok some math and assumptions:

lets assume you have 1000 ASM lines to process for each loop. That would take .5msec.
If you output is 100 Hz, thats 10 msec. So with these numbers, I guess you could be off by 5% on the duty cycle. Hmmm... Not so good. But 1000 lines is HUGE. Also you could check the count in 5 evenly spaced places throughout your main, then the possible error would only be 1%. In case I have stated this wrong, I mean to say 10% duty could be 11%, or 90% could be 91%. So I guess I don't mean duty cycle % error, I really mean the time it takes to get to the next counter check.

Hopefully I have not confused you or anyone else too badly.:confused:

If you need utmost accuracy, use interupts :)