PDA

View Full Version : Decimal PWM Cycle values?



RossWaddell
- 11th April 2012, 01:55
(Very much a self-proclaimed noob so please bear with me)

I'm trying to blink an LED to simulate an old incandescent bulb using PWM but want the fade in/fade out part to be a little bit faster than CYCLE = 1 (in the future, I want to use some pots to be able to control this speed on the fly, but I'm not there yet). Is it possible to use values of CYCLE < 1 with PWM and a 12f629?


DUTY VAR BYTE
CYCLE VAR BYTE


LGHTS_ON_MS CON 1500
LGHTS_OFF_MS CON 500


Cycle = 1


lblLoop:
' Fade in
For Duty = 0 TO 255
PWM LED_0, Duty, Cycle
Next

High LED_0


' Stay on LGHTS_ON_MS
Pause LGHTS_ON_MS


' Fade out
For Duty = 255 TO 0 STEP -1
PWM LED_0, Duty, Cycle
Next

' Stay off for LGHTS_OFF_MS
Pause LGHTS_OFF_MS

GoTo lblLoop


End

HenrikOlsson
- 11th April 2012, 06:08
Hi,
No, you can not produce a PWM signal with a LESS than one cycle - and that's not a PBP limitation. What you can do to make it fade up/down faster is to decrease the number of discrete dutycycles that it "passes thru" by using the STEP directive in the FOR-NEXT loop, like you do when fading down

For Duty = 0 to 255 STEP 5
PWM LED_0, Duty, Clycle
NEXT
Or you could increase the oscillator frequency which in turn will increase the frequency generated by the PWM command.

/Henrik.

RossWaddell
- 11th April 2012, 12:58
Thanks Henrik, I'll give your suggestion a try. My only concern is that I don't want flickering.

BTW, I'm already using a 20Mhz crystal.