TACHYON V4 IFDEF sw_1.fth @rest org FORGET sw_1.fth } pub sw_1.fth ." switch 2017/06/14 14:07:02 " ; @org W@ == @rest --- remember { 2017/06/14 14:05:26 Reduced code size by Peter's advice 2017/06/14 9:48:50 Translated sw to Tachyon 3V3 | 10kohm | P0 -------| | sw | GND |\| P1 ----| | ----220ohm---GND |/| LED } \ =========================================================================== \ Constants \ =========================================================================== #P0 == swpin #P1 == ledpin #800000 == 10msec swpin MASK == swMask \ =========================================================================== \ Variables \ =========================================================================== long lastDebounceTime long lastswState byte swState byte debounce byte alternate \ =========================================================================== \ Main \ =========================================================================== : init \ Set initial values 0 lastDebounceTime ! -1 lastswState ! 1 swState C! 0 debounce C! 1 alternate C! ; \ On/Off switch \ LED on when pushed sw, LED off when release sw \ ( -- ) : sw1 init BEGIN \ Read current sw and check if pushed or released swpin PIN@ DUP lastswState @ <> ( -1/0 1/0 ) IF \ If sw is under debouncong debounce C@ 0= IF CNT@ lastDebounceTime ! 1 debounce C! ENDIF ELSE 0 debounce C! ENDIF debounce C@ ( -1/0 1/0 ) IF CNT@ lastDebounceTime @ - 10msec > IF \ Update current swState and lastswState DUP swState C! lastswState ! ELSE DROP ENDIF ELSE DROP ENDIF ( -- ) \ Activate LED when sw-on ledpin swState C@ IF LOW ELSE HIGH ENDIF KEY \ Break loop if hitting any key UNTIL ; \ Alternative switch \ Switching LED on/off when pushed sw \ ( -- ) : sw2 init BEGIN \ Read current sw and check if pushed or released swpin PIN@ DUP lastswState @ <> ( -1/0 1/0 ) IF \ If sw is under debouncong debounce C@ 0= IF CNT@ lastDebounceTime ! 1 debounce C! ENDIF ELSE 0 debounce C! ENDIF debounce C@ ( -1/0 1/0 ) IF CNT@ lastDebounceTime @ - 10msec > IF \ Treat only when pushed switch lastswState @ IF alternate C@ 0= swState C! \ Set swState alternate C@ 0= alternate C! \ set alternate ENDIF lastswState ! \ Update current lastswState ELSE DROP ENDIF ELSE DROP ENDIF ( -- ) \ Activate LED when sw-on ledpin swState C@ IF LOW ELSE HIGH ENDIF KEY \ Break loop if hitting any key UNTIL ; END \ ?BACKUP