Specifications

Event Multitasking - 12
MULTITASKING ON A CLOCK TICK
The three 200 Hz (100 Hz in 9 Mhz systems) tick timers are used for a number of multitasking functions in CAMBASIC.
In multitasking, it is used as a periodic interrupt. The ON TICK statement can call a subroutine as often as 200 (100 in 9
Mhz systems) times per second, or once every 327.67 seconds. The syntax is:
ON TICK number, time GOSUB line/label
On every multiple of the specified time, program execution branches to the subroutine at the line/label. Program execution
resumes when the RETURN statement is reached in the subroutine.
A simple example would be as follows:
10 ON TICK 0,1 GOSUB 50
20 PRINT "foreground"
30 FOR C=0 TO 1000:NEXT
40 GOTO 20
50 INC A%
60 PRINT A
70 RETURN
Line 10 tells CAMBASIC to interrupt every 1.00 seconds and branch to line 50.
Lines 20 through 40 represent a foreground program. It prints, does a short delay and prints again.
Line 50 increments a variable and represents the number of seconds.
In some applications multiple interrupts are required. This can be done with the TICK statement. The only limitation is that
the interrupt times must be multiples of each other. For example, 1.0 and 3.0 seconds would be acceptable, but 1.0 and 2.7
seconds would not.
Below is a program that demonstrates clock interrupts every 1 and 5 seconds.
10 ON TICK 0,1 GOSUB 50
20 PRINT "foreground"
30 DELAY .25
40 GOTO 20
50 PRINT TAB(20);"tick";
60 INC A%:IF A%=5 THEN A%=0:GOSUB 70 ELSE PRINT:RETURN
70 PRINT " tock"
80 RETURN
Line 10 sets up an interrupt every 1.00 seconds
Lines 20 through 40 simulate your foreground program, as in the first example.
Line 50 prints “tick” every second
Line 60 increments a second counter. When it reaches 5, the counter is reset and "tock" is printed at
line 70.