TACHYON V4 IFDEF sw_1.fth @rest org FORGET sw_1.fth } pub sw_1.fth ." switch 2017/06/13 13:49:19 " ; @org W@ == @rest --- remember { 2017/06/12 11:40:54 3V3 | 10kohm | P0 -------| | sw | GND |\| P1 ----| | ----220ohm---GND |/| LED } \ =========================================================================== \ Constants \ =========================================================================== #P0 == swpin #P1 == ledpin #4000000 == 50msec swpin MASK == swMask ledpin MASK == ledMask \ =========================================================================== \ Variables \ =========================================================================== word swState word debounce word lastswState long lastDebounceTime word alternate \ =========================================================================== \ Main \ =========================================================================== \ read current sw-state \ ( -- n1 ) n1:1[sw:off] 0[sw:on] : rd_sw INA COG@ swMask AND ; : init \ Set initial values 1 swState W! 0 lastDebounceTime ! 1 lastswState W! 0 debounce W! ; \ 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 rd_sw DUP lastswState W@ <> IF \ If sw is under debouncong debounce W@ 0= IF CNT COG@ lastDebounceTime ! 1 debounce W! ENDIF ELSE 0 debounce W! ENDIF debounce W@ IF CNT COG@ lastDebounceTime @ - 50msec > IF \ Update current swState and lastswState DUP swState W! lastswState W! ELSE DROP ENDIF ELSE DROP ENDIF \ Activate LED when sw-on ledMask swState W@ IF OUTCLR ELSE OUTSET ENDIF 0 \ fkey? swap drop UNTIL ; END \ ?BACKUP