PDA

View Full Version : Why is this code not working(pwm on a 16f628a)



Bigandrewgold
- 3rd May 2012, 02:05
So I am testing out pbp and am trying to get an led to fade on, stay on, then fade off. Here is my current code


STARTMAIN:
if button=0 then
For Duty=0 to 255 Step 1
PWM LED4,Duty,Cycle
Next
led4=1
pause 1000
For Duty=255 to 0 Step -1
PWM LED4,Duty,Cycle
Next
LED4=0

endif
LED4=0
GOTO STARTMAIN
What happens is that when I press the button, the led fades on, then turns off for a second, then fades off. Why is this happening?

Demon
- 3rd May 2012, 03:02
How is the LED connected to the PIC?

Robert

EDIT: And what did you use as CONFIG and TRIS settings?

Bigandrewgold
- 3rd May 2012, 03:28
How is the LED connected to the PIC?

Robert

EDIT: And what did you use as CONFIG and TRIS settings?positive end is connected to the pin, negative end is grounded


@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_OFF & _MCLRE_OFF & _BODEN_OFF & _LVP_OFF

Define OSC 4


LED4 VAR PortA.1
button VAR PortA.2
CMCON = 7
VRCON = 0
PortA=0
PortB = 000100
T1CON = 000011
TRISA = 000100
TRISB = 000000



and this code works though, which is really confusing me

new:
if button=0 then
led4=1
pause 500
led4=0
endif


goto new

Demon
- 3rd May 2012, 03:34
Totally wild guess, confirm your programmer is setting the low voltage programming fuse properly:
http://www.picbasic.co.uk/forum/showthread.php?t=16462&p=113491&highlight=#post113491

(still looking at datasheet)

Robert

EDIT: Darn, I remember reading another thread that was missing one more config to disable analog, can't find it now.

Demon
- 3rd May 2012, 03:44
It must be something with the PWM, either config or syntax.

What if you use variables like this?

http://www.picbasic.co.uk/forum/showthread.php?t=3044&highlight=16f628a+pwm

Robert

gadelhas
- 3rd May 2012, 04:21
Try this code insted




STARTMAIN:
if button=0 then
For Duty=0 to 255
PWM LED4,Duty,Cycle
Next
high led4
pause 1000

For Duty=255 to 0 Step -1
PWM LED4,Duty,Cycle
Next
endif

low led4
GOTO STARTMAIN

If you read the PBP Manual you will see that the PWM command after execution turn the pin input.

aratti
- 3rd May 2012, 21:47
Did you declare the variable CYCLE? Did you give a value to such a variable?

If not then PWM command will not work.

Cheers

Al.

Bigandrewgold
- 4th May 2012, 02:02
Try this code insted




STARTMAIN:
if button=0 then
For Duty=0 to 255
PWM LED4,Duty,Cycle
Next
high led4
pause 1000

For Duty=255 to 0 Step -1
PWM LED4,Duty,Cycle
Next
endif

low led4
GOTO STARTMAIN

If you read the PBP Manual you will see that the PWM command after execution turn the pin input.thanks, so high and low still work when the pin is a input, interesting.

mister_e
- 4th May 2012, 08:56
not interesting, it is explained in the manual...