View Full Version : Flip Flop
tazntex
- 13th November 2008, 17:13
The little light bulb went off in my head this morning about using a flip flop to turn on or off a pin on portb.1. Ok, I thought I could push a momentary button one time on portb.0 and portb.1 would go high, pushing the button once more and releasing it, portb.1 would go low. Hmmm, sounds easy, so off to my keyboard:
flipflop VAR BYTE
flipflop=%00000000
loop:
IF portb.0 = 1 && flipflop.0 = 0 THEN
flipflop.0=1
portb.1=1
ENDIF
PAUSE 100
IF portb.0 = 1 && flipflop.0 = 0 THEN
flipflop.0=1
portb.1 = 0
ENDIF
IF portb.0 = 0 THEN 'With this the button would have to be release so it would
flipflop = flipflop ^ %00000001 'not continue to toggle
ENDIF
goto loop
Would this be the way to do this?
Thanks for your suggestions.
END
peterdeco1
- 13th November 2008, 18:04
There has to be an easier way. I'm assuming portb.0 is held high with a pullup. Try something like this:
START:
IF PORTB.0 = 0 THEN TOGGLE PORTB.1 : GOTO WAITFORRELEASE
GOTO START
WAITFORRELEASE:
PAUSE 50
IF PORTB.0 = 0 THEN WAITFORRELEASE
GOTO START
mister_e
- 13th November 2008, 19:24
Not sure to understand the whole thing... but if you just want to Toggle the state of a pin with a single button... i would suggest
IF PORTB.0 = 0
TOGGLE PORTB.1 ' or PORTB.1=PORTB.1 ^1
WHILE PORTB.0=0 : WEND ' wait 'till button is released
PAUSE 50 ' debounce delay
ENDIF
Still doable in a ISR...
Melanie
- 13th November 2008, 20:24
I'd put the debounce BEFORE the nested While:Wend as so...
[code]
IF PORTB.0 = 0
TOGGLE PORTB.1 ' or PORTB.1=PORTB.1 ^1
PAUSE 50 ' debounce delay
WHILE PORTB.0=0 : WEND ' wait 'till button is released
ENDIF
code
Melanie
- 13th November 2008, 20:24
I'd put the debounce BEFORE the nested While:Wend as so...
IF PORTB.0 = 0
TOGGLE PORTB.1 ' or PORTB.1=PORTB.1 ^1
PAUSE 50 ' debounce delay
WHILE PORTB.0=0 : WEND ' wait 'till button is released
ENDIF
Otherwise you're not debouncing the original Button-Press and risk running straight through the button-release code.
mister_e
- 13th November 2008, 20:53
;) yeah but noise on switches/push-buttons appear on both rising and falling edges???
But yeah, here it could be safer to debounce before, as the TOGGLE will execute pretty fast, and the code next to the IF might be long enough to act as Rising edge debouncing.. who know ;)
I'm not a fan of reading push button directly in many cases... Timer interrupt do the whole thing in background and also do the debouncing... why doing simple when you can do it 'complicated' :D
Powered by vBulletin® Version 4.1.7 Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.