The code below was written to use a momentary pushbutton switch as an ON/OFF control.
It is written to be used in a Timer interrupt routine.
PowerOnDelayTimer is the number of interrupts that the button must be held before turning ON the unit.
PowerOFFDelayTimer is the number of interrupts that the button must be held before turning OFF the unit
SwitchUpDelayTimer is the number of interrupts that must expire before the button can be pushed again (useful when you don't want someone rapidly turning ON and OFF a piece of hardware).
Code:IF !EnableSw THEN ; Aliased pin, goes low when button pushed. IF SwitchFlag THEN ; Switch has been pushed, but prevously released. IF PowerSwitchCounter < 250 THEN PowerSwitchCounter = PowerSwitchCounter + 1 ; don't let it overflow ENDIF IF PowerONstate = 0 THEN ; Currently OFF If PowerSwitchCounter >= PowerOnDelayTimer then ; Turn on or off Gosub PowerOn SwitchFlag = 0 SwitchUpCounter = 0 ENDIF ELSE ; Currently OFF If PowerSwitchCounter >= PowerOffDelayTimer then GOSUB PowerOff SwitchFlag = 0 SwitchUpCounter = 0 ENDIF ENDIF ENDIF ELSE IF SwitchUpCounter < 250 THEN SwitchUpCounter = SwitchUpCounter + 1 ENDIF IF SwitchUpCounter > SwitchUpDelayTimer THEN SwitchFlag = 1 ; Switch has been released PowerSwitchCounter = 0 ENDIF ENDIF




Bookmarks