PDA

View Full Version : PWM not working?



Russ Kincaid
- 25th October 2010, 16:14
This program runs (produces pulses of varying width):


REM DEVICE = 12F675
CMCON = 7 'SETS DIGITAL MODE
ANSEL = 0 'GPIO.0 TO GPIO.3 SET AS DIGITAL
TRISIO = 0 'ALL OUTPUTS BUTGP3 (PIN4) IS INPUT ONLY
VRCON.7 = 0 'TURN OFF VOLTAGE REFERENCE TO SAVE POWER
DEFINE OSCCAL_1K 1 'TO SAVE OSCILLATOR CALIBRATION
tme var word 'BEGINNING NUMBER OF PULSE CYCLES(COUNT DOWN)
d var byte 'DUTY CYCLE OF PULSE, 127=50%
j var word 'NUMBER OF CYCLES OF PULSE
START:

FOR d = 50 TO 250
PWM GPIO.0,d,1
PAUSEUS 500
NEXT D

GOTO START
end

This program does not:


REM DEVICE = 12F675
CMCON = 7 'SETS DIGITAL MODE
ANSEL = 0 'GPIO.0 TO GPIO.3 SET AS DIGITAL
TRISIO = 0 'ALL OUTPUTS BUTGP3 (PIN4) IS INPUT ONLY
VRCON.7 = 0 'TURN OFF VOLTAGE REFERENCE TO SAVE POWER
DEFINE OSCCAL_1K 1 'TO SAVE OSCILLATOR CALIBRATION
tme var word 'BEGINNING NUMBER OF PULSE CYCLES(COUNT DOWN)
d var byte 'DUTY CYCLE OF PULSE, 127=50%
j var word 'NUMBER OF CYCLES OF PULSE
START:

FOR d = 250 TO 50
PWM GPIO.0,d,1
PAUSEUS 500
NEXT D

GOTO START
end

Can anyone explain why this does not run (produces no pulses).

LinkMTech
- 25th October 2010, 16:25
Hi Russ,
Try this:


FOR d = 250 TO 50 STEP -1

Acetronics2
- 25th October 2010, 16:45
Hi, Russ

One thing hurts me in your program ...

you write :


produces pulses of varying width


in fact the PWM command produces pulses BURSTS whose total duration match the requested "duty" ratio ...

understand signal is not a nice pulse with one High level and one low level, but some kind of garbage.

No problem, if you drive a lamp or generate a voltage through a Low Pass filter ...
but not so good if driving some kind of inductive load ( motor i.e. ):o

in this last case ... you should create some Software PWM by regularly toggling a pin ... or use a 12F683 that has a HPWM module.

Alain

Russ Kincaid
- 26th October 2010, 01:10
Hi Russ,
Try this:


FOR d = 250 TO 50 STEP -1


I knew that! Bonk! Bonk! Bonk!