Why is this code not working(pwm on a 16f628a)
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
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?
Re: Why is this code not working(pwm on a 16f628a)
How is the LED connected to the PIC?
Robert
EDIT: And what did you use as CONFIG and TRIS settings?
Re: Why is this code not working(pwm on a 16f628a)
Quote:
Originally Posted by
Demon
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
Code:
@ __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
Code:
new:
if button=0 then
led4=1
pause 500
led4=0
endif
goto new
Re: Why is this code not working(pwm on a 16f628a)
Totally wild guess, confirm your programmer is setting the low voltage programming fuse properly:
http://www.picbasic.co.uk/forum/show...ht=#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.
Re: Why is this code not working(pwm on a 16f628a)
It must be something with the PWM, either config or syntax.
What if you use variables like this?
http://www.picbasic.co.uk/forum/show...ht=16f628a+pwm
Robert
Re: Why is this code not working(pwm on a 16f628a)
Try this code insted
Code:
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.
Re: Why is this code not working(pwm on a 16f628a)
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.
Re: Why is this code not working(pwm on a 16f628a)
Quote:
Originally Posted by
gadelhas
Try this code insted
Code:
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.
Re: Why is this code not working(pwm on a 16f628a)
not interesting, it is explained in the manual...