Shop OBEX P1 Docs P2 Docs Learn Events
SW Exp #14: Scanning and Deboucing Multiple Inputs — Parallax Forums

SW Exp #14: Scanning and Deboucing Multiple Inputs

playinmyblues_nsccplayinmyblues_nscc Posts: 38
edited 2013-03-01 10:57 in BASIC Stamp
Hi. I am hoping somebody can explain a section of program in StampWorks Experiment #14, page 94 of the book. I have an edited program in addition to the original program. In both programs I do not understand why the buttons have to be held down for the whole period of the FOR NEXT loop. I understand that that is what happens. In my program, I wrote a preceding loop to let me know when the time was approaching where I have to hold down the button. I also extended the time that the button must be held down so I can experiment with the time it is held down. For example, the first loop lets me know when the hold-down time is approaching so I hold down the button. The FOR NEXT loop counts for the debounce. I hold the button down prior to the loop and when the count is part way through I release the button and press it again before the time is up and I hold the button down until the loop is over.

The effect is I know for sure that the original program functions as it should and my program functions as it should. However, I do not understand why. The reason I do not understand why is that at the end of the count for which the button must be pressed, the boolean expression: btns = btns & ~BtnBus would equal a 1 or 1's at the appropriate place for the last count, or so I would think. Why is this not true?

Original program:
' {$STAMP BS2}
' {$PBASIC 2.5}

' -- Program: SW21-EX14-Debounce.bs2
' -- Purpose: This program demonstrates the simultaneous debouncing of multiple inputs. The input subroutine is easily adjusted to handle
' any number of of inputs.

' -- I/O Definitions --

BtnBus    VAR    INA ' four inputs, P0 - P3


' -- Variables --

btns      VAR    Nib ' deboucned inputs
idx       VAR    Nib ' loop counter

' -- Program Code --

Main:
  DO
    GOSUB Get_Buttons                    ' get deboucned inputs
    DEBUG HOME, "Inputs = ", IBIN4 btns  ' display in binary mode
    PAUSE 50
  LOOP


' -- Subroutines --

Get_Buttons:
  btns = %1111            ' enable all four inputs
  FOR idx = 1 TO 5
    btns = btns & ~BtnBus ' test inputs
    PAUSE 5
  NEXT
  RETURN
******************************************************************
My program:
' {$STAMP BS2}
' {$PBASIC 2.5}

' -- Program: SW21-EX14-Debounce-Edit8Btns.bs2
' -- Purpose: This program demonstrates the simultaneous debouncing of multiple inputs. The input subroutine is easily adjusted to handle
' any number of of inputs.

' -- I/O Definitions --

BtnBus    VAR    INL ' four inputs, P0 - P7


' -- Variables --

btns      VAR    Byte ' deboucned inputs
idx       VAR    Byte ' loop counter
flip      VAR    Bit ' a bit sized counter to flip from 1 to 0 to 1 to alternate a DEBUG statement
idy       VAR    Word  ' variable used to count the PAUSE

' -- Program Code --

' -- Initialize Variable --

flip = 0

Main:
  DO
    GOSUB Get_Buttons                    ' get deboucned inputs
    DEBUG HOME, "Inputs = ", IBIN8 btns, CR  ' display in binary mode

    flip = flip + 1
    IF (flip = 1) THEN
      DEBUG "Paws1", CR
    ELSEIF (flip = 0) THEN
      DEBUG "Paws2", CR
    ENDIF

'    PAUSE 5000
    FOR idy = 1 TO 500       ' This FOR NEXT loop is used to let the user know when the next opportunity to press the button is
      DEBUG CRSRXY, 0, 3     ' coming up. Otherwise, the user cannot know when to time the button press as the time is too short
      DEBUG DEC4 idy         ' unless the FOR NEXT loop below is extended to something like 100.
    NEXT
    idy = 0

  LOOP


' -- Subroutines --

Get_Buttons:
  btns = %11111111        ' enable all eight inputs
  FOR idx = 1 TO 50        ' the FOR loop is the debounce mechanism. It allows for the mechanical bouncing of the contacts to stop
                          ' and then it exits the loop and returns to the DEBUG statement in the Main part of the program.
                          ' Using a higher value for the upper limit of the FOR NEXT loop allows the person to experiment with
                          ' timing of the button press. If the button is released before the end of the loop occurs, the button
                          ' will show as not being pressed.

    btns = btns & ~BtnBus ' test inputs
'    DEBUG CRSRXY, 0, 1, "idx: ", DEC1 idx
    DEBUG CRSRXY, 4, 4, DEC2 idx       ' this line gives a number that changes in one position on the DEBUG terminal
    PAUSE 5
  NEXT
  RETURN
Sorry for the poor code posting - recently fixed.

Comments

  • playinmyblues_nsccplayinmyblues_nscc Posts: 38
    edited 2013-02-28 17:56
    While you're at it can you tell me how to post code properly so it shows up in its own window, properly indented?
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2013-02-28 18:05
    PhiPi's help link:
    attachment.php?attachmentid=78421&d=1297987572
  • Tracy AllenTracy Allen Posts: 6,662
    edited 2013-02-28 18:14
    Dissecting,
    [SIZE=1][FONT=courier new]btns VAR Nib
    idx VAR Nib
    Get_Buttons:
         btns = % 1111     ' assume all four pressed
         FOR idx = 1 TO 5
             btns = btns & ~inA  ' test 4 inputs on inA
             PAUSE 5
         NEXT
         RETURN[/FONT][/SIZE]
    

    When a button is pressed, its bit in inA goes to 0. The ~inA operator inverts that, a pressed button comes out as 1. The % 1111 starts by assuming all buttons are 1=pressed. If a button stays pressed for all 5 scans, then its bit stays at 1&1&1&1&1 = 1. But if a button is let up even once during the scan, then the result is, e.g. 1&1&0&0&1 = 0. Debounced result is, "not pressed". Does that help?
  • playinmyblues_nsccplayinmyblues_nscc Posts: 38
    edited 2013-02-28 18:18
    Yes, it does. Thank you. It was a simpler explanation than I thought it would be.
  • playinmyblues_nsccplayinmyblues_nscc Posts: 38
    edited 2013-02-28 18:19
    Also, thanks for the link to posting code!
  • playinmyblues_nsccplayinmyblues_nscc Posts: 38
    edited 2013-03-01 10:57
    My intention was to write another reply saying that while your explanation was simple, it did not really explain what was going on. However, I read through the program and explanation in the book again and see what you had written, Tracy, was essentially the same as what is written in the book. I think I was too tired last night to see it. Once there is a 0 introduced into the btns = btns & ~BtnBus boolean expression, that 0 will carry all the way through to the end of the loop.

    Thanks again!
Sign In or Register to comment.