' ========================================================================= ' ' File....... SW20-EX14-Debounce-Adv.BS2 ' Purpose.... Multi-input button scanning and debouncing ' Author..... (C) 2000 - 2005, Parallax, Inc. ' E-mail..... support@parallax.com ' Started.... ' Updated.... 01 SEP 2005 ' ' {$STAMP BS2} ' {$PBASIC 2.5} ' ' ========================================================================= ' -----[ Program Description ]--------------------------------------------- ' ' This program demonstrates the simultaneous debouncing of multiple inputs, ' as well as the detection of a button status change (0 -> 1) between ' scans. Note that the value in "xBtns" is only valid immediately after ' the call to Get_Buttons. ' -----[ I/O Definitions ]------------------------------------------------- BtnBus VAR INA ' four inputs, pins 0 - 3 ' -----[ Variables ]------------------------------------------------------- nBtns VAR Nib ' new buttons oBtns VAR Nib ' old buttons xBtns VAR Nib ' changed, 0 -> 1 idx VAR Nib ' loop counter ' -----[ Program Code ]---------------------------------------------------- Main: DO GOSUB Get_Buttons ' get debounced inputs FOR idx = 0 TO 3 ' loop through inputs DEBUG CRSRXY, 0, idx IF (xBtns.LOWBIT(idx) = 1) THEN ' changed? (0 -> 1) DEBUG "Fire ", DEC (idx + 1) ' yes, show ELSE DEBUG ".", CLREOL ENDIF NEXT PAUSE 250 LOOP ' -----[ Subroutines ]----------------------------------------------------- Get_Buttons: nBtns = %1111 ' enable all four inputs FOR idx = 1 TO 5 nBtns = nBtns & ~BtnBus ' test inputs PAUSE 5 ' delay between tests NEXT xBtns = nBtns ^ oBtns & nBtns ' look for 0 -> 1 changes oBtns = nBtns ' save this scan RETURN