- bits 0-61 store the initial value - bits 63&62 store the format of the counter: 0=disabled 1=(actually 0x4000000000000000) indicates a one shot "AFTER x" type counter 2=(actually 0x8000000000000000) indicates a repetitive "EVERY x" type counter
Preamble: Option Base 0 Dim Integer TMct(9),TMin(9),Flag SETTICK 1000,CoreTMR,4' service timers every second Main: ' example usage for the main loop ' gives the user 10 seconds to hit a key (using timer 1) Print "Quick Press a key!" After 10,1 ' 10 seconds using timer 1 Every 1 ,2 ' count 1 second intervals using timer 2 Every 2 ,3 ' count 2 second intervals using timer 3 Do a$=Inkey$ If FlagTest(2) Then FlagRes 2:Print "x"' output an x while we wait If FlagTest(3) Then FlagRes 3:Print "y"' output a y while we wait Loop Until FlagTest(1) or a$<>"" x=Remain(1)' stop the timer and get the remaining time If FlagTest(1) Then ' the timer expired? Print "Too late!" Else ' otherwise someone pressed a key Print "that was close. There was only ";x;" seconds left!" EndIf DI For n=1 To 3: x=Remain(n): Next EI End ' The subs Sub CoreTMR ' needs to run on a ticker at whatever interval is required - this is also the multiple of timer counts If FlagTest(10) Then Exit Sub Local Integer n For n=0 To 9 Select Case TMin(n)>>62 ' extract the top 2 bits Case 0,3 'disabled or invalid FlagRes(n) Case 1' AFTER If TMct(n) then TMct(n)=TMct(n)-1 If TMct(n)=0 Then FlagSet(n) ' indicate the timer has expired EndIf Case 2' EVERY If TMct(n) then TMct(n)=TMct(n)-1 If TMct(n)=0 Then FlagSet(n) ' indicate the timer has expired TMct(n)=TMin(n) And &h3FFFFFFFFFFFFFFF ' reset the timer EndIf EndIf End Select Next End Sub Sub After(Interval As Integer,Tmr As Integer)' starts a one-shot timer TMin(Tmr)=Interval OR &h4000000000000000:TMct(Tmr)=Interval And &h3FFFFFFFFFFFFFFF:FlagRes(Tmr) End Sub Sub Every(Interval As Integer,Tmr As Integer)' starts a repetitive timer TMin(Tmr)=Interval OR &h8000000000000000:TMct(Tmr)=Interval And &h3FFFFFFFFFFFFFFF:FlagRes(Tmr) End Sub Function Remain(Tmr As Integer, opt As Integer) As Integer' returns the remaining time in the counter. 'if opt=0 (or omitted) timer is stopped 'if opt<>1 timer remains running Remain=TMct(Tmr) If opt=0 Then TMin(Tmr)=TMin(Tmr) And &h3FFFFFFFFFFFFFFF End Function Sub DI FlagSet 10 End Sub Sub EI FlagRes 10 End Sub