' ========================================================================= ' File....... SW21-EX14-Debounce.SXB ' Purpose.... Simple digital clock with software multiplexing ' Author..... (C) 2000 - 2005, Parallax, Inc. ' E-mail..... support@parallax.com ' Started.... 01 SEP 2005 ' Updated.... 23 MAR 2007 by TSaavik for SX28 ' ========================================================================= ' -----[ 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. '------{ Device Settings ]------------------------------------------------- DEVICE SX28,TURBO,BANKS8,OSCHS3,SYNC,OPTIONX FREQ 50_000_000 ID "EX14-ADV" IRC_CAL IRC_SLOW ' -----[ I/O Definitions ]------------------------------------------------- BtnBus VAR RB ' four inputs, pins 0 - 3 ' -----[ Constants ]------------------------------------------------------- ' -----[ Variables ]------------------------------------------------------- nBtns VAR Byte ' new buttons oBtns VAR Byte ' old buttons xBtns VAR Byte ' changed, 0 -> 1 idx VAR Byte ' loop counter lowbit VAR Byte ' -----[ Initialization ]------------------------------------------------- PROGRAM Start watch nbtns, 4, UBIN watch obtns, 4, UBIN watch xbtns, 4, UBIN ' -----[Subs ]------------------------------------------------------------- ' -----[ Program Code ]---------------------------------------------------- Start: Tris_B = %00001111 ' make 0-3 inputs Main: DO GOSUB Get_Buttons ' get debounced inputs FOR idx = 0 TO 3 ' loop through inputs 'DEBUG CRSRXY, 0, idx lowbit = idx and $0f IF xBtns.lowbit = 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