PDA

View Full Version : Strangw results when using PWM command



malc-c
- 9th July 2006, 13:09
I've written a simple program to pulse a LED on GPIO.0 of a 12F675, which worked fine. However I didn't save the file, and so I started from scratch again this morning.. but I'm experiencing some strange behaviour.

This is the main body of the code:



led var GPIO.0

i var byte

up:
For i = 1 to 254
Pwm GPIO.0,i,1
if i = 254 then goto down
next i

down:
for i = 254 to 1 step-1
pwm GPIO.0,i,1
if i = 1 then goto main
next i


As you can see its a simple FOR/NEXT loop, that, should i reach a set value, goto either up or down section.

However when programmed, the LED ramps up in brightness but then fails to ramp down. I've tried remming out the up section and all that happens is that the LED flashes fast (as if i is not reducing by the step-1 command)



;up:
For i = 255 to 1 step-1
Pwm GPIO.0,i,1
;if i = 254 then goto down
next i

;down:
;for i = 254 to 1 step-1
;pwm GPIO.0,i,1
;if i = 1 then goto main
;next i


Its as if the PWM command or the step-1 statement is not being observed. And what's bugging me is I know it can be done as it worked before (the chip is now in cornwall so I can't read back the HEX !)

Comments anyone please !

malc-c
- 9th July 2006, 13:21
This is really strange.. its as though the "IF" in the "IF i = 245" is being ignored.

I've just tried the following and it simply ramps up the LED to full brightness, over and over again, with out any pause ! (ie its the same as not having the pause statement there)



Main:
For i = 1 to 254
Pwm GPIO.0,i,1
if i = 254 then pause 500
if i = 254 then down
next i

malc-c
- 9th July 2006, 13:58
Now this is getting silly !



Main:
for i = 1 to 254
Pwm GPIO.0,i,1
if i=253 then goto down
next i

down:
high gpio.0
pause 1000
low gpio.0
goto main


The above test even fails... it simple runs the main loop and doesn't branch off to the down routine !


EDIT:

As does the following


Main:
for i = 1 to 254
Pwm GPIO.0,i,1
if i=253 then t=1
if t=1 then goto down
next i


This is doing my head in

Acetronics2
- 9th July 2006, 14:28
Hi, Malc

Try this ... and do not forget I/Os and clock defining ...

DEFINE OSCCAL_1K 1

ADCON0 = 0
ANSEL = 0

' Define YOUR I/Os Here !!! TRISIO = % ?????1

led var GPIO.0

i var byte

up:
For i = 1 to 254
Pwm GPIO.0,i,1
next i

for i = 254 to 1 step-1
pwm GPIO.0,i,1
next i

GOTO up

END

Alain

mister_e
- 9th July 2006, 14:43
DEFINE OSCCAL_1K 1

ADCON0 = 0
ANSEL = 0

' Define YOUR I/Os Here !!! TRISIO = % ?????1

The define OSCCAL have nothing usefull to do there
Same for ADCON0 as it's the POR default.. but i agree could be safer

Add CMCON=7 as it's a 12F675 and it have internal comparator multiplexed with GP.0



Main:
for i = 1 to 254
Pwm GPIO.0,i,1
if i=253 then goto down
next i

Bad programming method IMHO, i always prefer to validate the Loop condition before getout and if usefull, set a flag wich say i interrupt the loop... something like


Main:
for i = 1 to 254
Pwm GPIO.0,i,1
if i=253 then
i=254
GetOut=1
endif
next i

if Getout then DoSomething


In your case, the main problem is most the analog stuff

malc-c
- 9th July 2006, 14:56
Alain, thank for the reply, but that still has the same effect, ie the LED ramps up in brightness, but doesn't ramp down.

Steve,
I never said my coding was tidy ;)

However isn't



Main:
for i = 1 to 254
Pwm GPIO.0,i,1
if i=253 then
i=254
GetOut=1
endif
next i

if Getout then DoSomething


The same thing as



Main:
for i = 1 to 254
Pwm GPIO.0,i,1
if i=253 then t=1
if t=1 then goto down
next i


in a round about way ? where the condition is replaced with t in my case ?

The thing that is also bugging me if that whilst the loop to increase the brightness works, the one to decrease it doesn't ???

Let me go away and try Steve's suggestion and see if I get any further.

Thanks as always for your inputs

malc-c
- 9th July 2006, 15:07
Steve,

I tried that and it still just ramps up and doesn't seem to want to jump to the down label



@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON

TRISIO.0 = 0 'Set GPIO.0 to output.
ANSEL.0 = 0 'Set GPIO.0 to digital
GPIO.0 = 0
CMCON = 7 ' PortA Digital inputs
VRCON = 0 ' Voltage reference disabled
OPTION_REG.7 = 0


led var GPIO.0
i var byte
getout var bit

main:
for i = 1 to 254
Pwm GPIO.0,i,1
if i=253 then
i=254
GetOut=1
endif
next i

if Getout=1 then goto down

down:
for i = 254 to 1 step -1
Pwm GPIO.0,i,1
if i=2 then
i=1
GetOut=0
endif
next i

if Getout=0 then goto main

malc-c
- 9th July 2006, 15:15
Steve,

I've also tried the following, which seems to flash the LED rather than ramp down in brighness



for i = 254 to 1 step-1
Pwm GPIO.0,i,1
if i=2 then
i=1
GetOut=1
endif
next i

if Getout=1 then goto down


I can't for the life of me work out what's wrong...

Acetronics2
- 9th July 2006, 17:03
Hi, Malc

I had just forgotten it was for your famous Dr Who's light ...

so, it could not work properly with such a soft ...

There was a thread you've already started about that ... you didn't find anything usable in it ???

http://www.picbasic.co.uk/forum/showthread.php?p=21754#post21754

I'll try to make a search ...

Alain

Seems the HPWM module of the 16F628a you saw was doing the job ... but may be the "slow PWM" thread from Darrel could help here !!! or use a 12F683 ...

malc-c
- 9th July 2006, 17:37
Alain,

Yes this is to do with the "Dr Who" thread in a way. I started a new one as the matters raised in that thread (ie the fact you need to mod the inc files) resolved it.

What I can't understand was that I had used a 12F675 and the for next loops with the pwm command to successfully produce a pulsating LED for my mates project. I sent this off to him but he found that he needed to drive a bulb as the LED didn't give him the brightness he needed. I informed him that he could just used a FET and how to use a zenenr diode etc to produce 5v from a 12v battery, however he has never done any electronics, and may of fried the PIC. I then loaded up the PBP program but for some reason it stops running the down loop of the code.. anyway.. I've sent off the PCB so hopefully he hasn't fried his original PIC and he can simply drop that in and get filming :)

Thanks guys

mister_e
- 9th July 2006, 17:38
with the existing stuff, try this one


@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON

ANSEL.0 = 0 ' Set GPIO.0 to digital
CMCON = 7 ' PortA Digital inputs
VRCON = 0 ' Voltage reference disabled
TRISIO.0 = 0 ' Set GPIO.0 to output.
GPIO.0 = 0 '

LED var GPIO.0
I var byte
GetOut var bit

main:
for i = 1 to 254
Pwm GPIO.0,i,1
if i=253 then
i=254
GetOut=1
endif
next i

if Getout=1 then down
goto main

down:
getout=0
for i = 254 to 1 step -1
Pwm GPIO.0,i,1
if i=2 then
i=1
GetOut=1
endif
next i

if Getout=1 then main
goto down


and to make something usefull with the GetOut flag, use the following. Tested and work at treat. OK i'd modified the hardware assignement to fit to one of my already build board here


@ __CONFIG _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON

ANSEL = 0 ' Set GPIO.0 to digital
CMCON = 7 ' PortA Digital inputs
VRCON = 0 ' Voltage reference disabled
TRISIO = $0F ' gpio<3:0> : Input
' gpio<5:4> : Output

GPIO = 0 ' clear all outputs

LED var GPIO.5
PB var GPIO.0
I var byte
GetOut var bit

Pause 50 ' oscillator settle time...
OPTION_REG.7 =0 ' enable internal pull-up
WPU = 1 ' enable pull-up on GPIO.0 only

main:
for i = 1 to 254
Pwm led,i,1
if pb=0 then
i=254
GetOut=1
endif
next i

if Getout=1 then
while pb=0 : wend
pause 50
GETOUT=0
goto down
endif
goto main

down:
for i = 254 to 1 step -1
Pwm led,i,1
if pb=0 then
i=1
GetOut=1
endif
next i

if Getout=1 then
while pb=0 : wend
pause 50
GETOUT=0
GOTO main
ENDIF
goto down


HTH

malc-c
- 9th July 2006, 18:34
Steve,

Thanks once again for all you help. I had given up and wrapped up the board to post it to my mate, however I took it out of the jiffy bag and gave it one last try...

I copied and pasted the code in your last post (the first program) and tried that. It compiled fine and loaded into the PIC, however the code does exactly the same as all other attempts. ie it ramps (for want of a better word) up the brightness to full, and then goes off, then repeats the same. It still doesn't ramp down from full brightness to low like it should - I've tried different PICs so it rules out that as being the cause.

I think I'll call it a day on this.. thanks again for your help

mister_e
- 9th July 2006, 18:42
That's really strange... it's working here using the first code of my previous message.

Ramp-up, ramp-down in an endless loop... mmm.

I suspect a hardware problem but damn it's not a really complicated hardware, i removed all the usual capacitor around, nothing on all other i/o, simple LED+300 ohm resistor... work fine. if i raise down the psu voltage... it's working from 2.5V. so uneless your Vdd rail is below or really noisy causing a RESET... i don't see why it shouldn't work.

malc-c
- 9th July 2006, 19:08
Steve,

Rest assured your code works fine.. I've just breadboarded the PIC and it works, yet in the PCB ot only wants to ramp up ????? - strange !!

I would of thought that if there was something wrong with the PCB I made (like a short) then the thing would not work full stop ! The board uses a 5.1 zenner to provide the supply to the PIC, and the output simply from GPIO 0 drives the gate of the FET and the LED - why it ramps up and not down I don't know ?

Thanks mate.. don't waste any more of your time... I've attached the schematic just in case someone might see whats the issue ?

mister_e
- 9th July 2006, 19:21
assuming 12Vin and R4 is really 10K: (12-5)/10K = 0.7mA

No problem when only the PIC run... but when the LED is attach to.. the voltage will really reduce ... 'till the PIC reset.

your problem is there, you don't provide enough current, Zener would need at least few mA just for her +Led+Pic... Repace R4 to 200Ohm

@12Vin : (12-5)/200=35mA
@9Vin : (9-5)/200 = 20mA

@12 Vin : 35mA*35mA*200Ohm = 1/4 Watt

Or better if you replace it for a voltage regulator like 78L05 or any else

malc-c
- 9th July 2006, 19:28
Steve

Thanks mate for your further help,

The reason I opted for using a zenner, was the last time I used a 7805 to run a few LEDs the thing glowed trying to disapate the heat !

I'll see if changing the resistor does the trick

Malc

mister_e
- 9th July 2006, 19:31
7805 is 1Amp rated
78L05 ~100mA

malc-c
- 9th July 2006, 19:37
Steve, went the hole hog and dropped in a 68R and it works a treat !!

Thanks for your help

Cheers

mister_e
- 9th July 2006, 19:44
You're welcome... just make sure that your Zener and resistor are rated to handle it ...
Resistor 68ohm... (12-5)/68=103mA, 103mA*103mA * 68 = 0.72W => 1 watt
Zener 5 Volt : 5*103mA = .515 Watt => could be 1/2 watt... i calculate the worst case, VMax,No Load

Acetronics2
- 10th July 2006, 09:04
Hi, Malc

I just tried this ... good for bulb lamp- like ... an rather simple.

DEFINE OSCCAL_1K 1

ADCON0 = 0
ANSEL = 0
CMCON = 7

TRISIO = %00001000

led var GPIO.0

i var byte
y var byte
Speed var word ' just if speed > 255 ...

Speed = 100

up:

Pause (Speed*2)

For i = 0 to 127

y = SIN i

Led = 1
Pauseus (Speed*y)
Led = 0
Pauseus (Speed*( 127-y ))

next i

GOTO up
END

Alain

Acetronics2
- 10th July 2006, 12:14
DEFINE OSCCAL_1K 1

ADCON0 = 0
ANSEL = 0
CMCON = 7
VRCON = 0


TRISIO = %00001000

led var GPIO.0

i var byte
y var word

up:

Pause 200

For i = 0 to 127

y = SIN i

Led = 1
Pauseus (y*y)
Led = 0
Pauseus ( 16129 -(y*y) )

next i

GOTO up
END