-
button problems
hello, i wrote a program for pic16F876 to do the following:
press button RB7 and led RB1 wil go on and stay on no matter how long you push RB7.
if you release RB7 the led RB1 will stay on,
if you push the RB7 again, the led will go off again , no matter how long you push RB7
This works perfectly standalone, but when i put it in a SUB and call it from the main, it doesn't work properly anymore,
So push RB7 gosub flipflop, push RB6 gosub onoff (turn led B0 on).
I used pulldown resistors, so that ain't the problem
I'm puzzled:
here is the working program:
trisb=%11110000
yy var byte
yy=2
portb=0
start:
if yy=2 and portb.7=0 then
yy=10
endif
if yy=10 and portb.7=1 then
yy=20
portb.1=1
endif
if yy=20 and portb.7=0 then
yy=30
endif
if yy=30 and portb.7=1 then
yy=40
portb.1=0
endif
if yy=40 and porta.7=0 then
yy=2
endif
goto start
'------------------------------
and now the subroutine version (Can't get it to work!!)
trisb=%11110000
yy var byte
yy=2
portb=0
start:
if portb.7=1 then
gosub flipflop
endif
if portb.6=1 then
gosub onoff
else
portb.0=0
endif
goto start
flipflop:
if yy=2 and portb.7=0 then
yy=10
endif
if yy=10 and portb.7=1 then
yy=20
portb.1=1
endif
if yy=20 and portb.7=0 then
yy=30
endif
if yy=30 and portb.7=1 then
yy=40
portb.1=0
endif
if yy=40 and porta.7=0 then
yy=2
endif
goto start
onoff:
portb.0=1
goto start
'===========================
Who can help me out here ???
greetz, Maus
-
I dont understand what all this stuff with yy is for.
Why not use something like this:
Code:
if portb.7=1 then ' If button is pressed:
toggle portb.1 ' Toggle the LED
loop: if portb.7=1 then goto loop ' Wait until the button is released
endif
-
anyway, the reason for your subroutine not working is becuase this statement will never be true: "if yy=2 and portb.7=0 then", becuase you only jump to the subroutine when portb.7=1.
Instead of this:
if portb.7=1 then
gosub flipflop
endif
Try just this: "gosub flipflop"
Also, there is no return from the subroutine... instead of "goto start", put "return"
-
simple and short
thanx, works like a charm
greetz, Maus