Flip Flop


Closed Thread
Results 1 to 6 of 6

Thread: Flip Flop

  1. #1

    Default Flip Flop

    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

  2. #2


    Did you find this post helpful? Yes | No

    Default

    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

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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
    Code:
    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...
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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

  5. #5
    Join Date
    Jul 2003
    Posts
    2,358


    Did you find this post helpful? Yes | No

    Default

    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
    Otherwise you're not debouncing the original Button-Press and risk running straight through the button-release code.

  6. #6
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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'
    Last edited by mister_e; - 13th November 2008 at 20:55.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Toggled Pic flip flop Help Please
    By g7jiq in forum General
    Replies: 2
    Last Post: - 22nd March 2009, 10:09
  2. 16-Channel LED Drivers
    By Sponge Bob in forum mel PIC BASIC Pro
    Replies: 11
    Last Post: - 16th February 2009, 04:48
  3. Optimizing DIV
    By skimask in forum mel PIC BASIC Pro
    Replies: 41
    Last Post: - 22nd September 2008, 04:58
  4. flip flop for driving latching solenoids
    By kitcat in forum Schematics
    Replies: 3
    Last Post: - 4th February 2006, 22:34
  5. need hex code to make 12c509 a flip flop,
    By bruce3mn in forum General
    Replies: 1
    Last Post: - 15th November 2004, 03:23

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts