When you exclusive-or "new" and "old" a resulting 1 bit indicates a change of state (either a new hi or new press, or a new lo or new release). You need to filter these "change" bits. If you AND the "change" bit with the "old" bit you filter out the "new press" bits and you're left with "new release" bits. If you AND the "change" bit with the "new" bit you filter out the "new release" bits and you're left with the "new press" bits.
Hope this helps.
Cheerful regards, Mike
Code:
'
' swnew ____-----_____-----_____ new switch sample (positive logic)
' swold _____-----_____-----____ switch state latch
' delta ____-____-____-____-____ changes, press or release
' newhi ____-_________-_________ new press bits
' newlo _________-_________-____ new release bits
'
swnew = PortB ' sample active hi switches
delta = swnew ^ swold ' changes, press or release
newhi = delta & swnew ' get "new press" bits
newlo = delta & swold ' get "new release" bits
swold = swnew ' update switch state latch
IF newhi.4 = 1 THEN '
BTN_PLUS = 1 '
ENDIF
Bookmarks