int e l 4.20 DESCRIPTION OF STATEMENTS STATEMENT: ONTIME [expr], [ln num] MODE: RUN TYPE: CONTROL Since MCS BASIC-52 processes a line in the millisecond time frame and the timer/counters on the 8052AH operate in the micro-second time frame, there is an inherent incompatibility between the timer/counters on the 8052AH and MCS BASIC-52. To help solve this situation the ONTIME [expr], [ln num] statement was devised. What ONTIME does is generate an interrupt every time the SPECIAL FUNCTION OPERATOR, TIME, is equal to or greater than the expression following the ONTIME statement. Actually, only the integer portion of TIME is compared to the integer portion of the expression.
The interrupt forces a GOSUB to the line number ([ln num]) following the expression ([expr]) in the ONTIME statement. Since the ONTIME statement uses the SPECIAL FUNCTION OPERATOR, TIME, the CLOCK1 statement must be executed in order for ONTIME to operate. If CLOCK1 is not executed the SPECIAL FUNCTION OPERATOR, TIME, will never increment and not much will happen. Since the ONTIME statement generates an interrupt when TIME is greater than or equal to the expression following the ONTIME statement, how can periodic interrupts be generated? That's easy, the ONTIME statement must be executed again in the interrupt routine: EXAMPLE: >10 TIME=0 : CLOCK1 : ONTIME 2,100 :
DO >20 WHILE TIME >100 PRINT "TIMER INTERRUPT AT -",TIME,"SECONDS" >110 ONTIME TIME+2,100 : RETI >RUN TIMER INTERRUPT AT - 2.045 SECONDS TIMER INTERRUPT AT - 4.045 SECONDS TIMER INTERRUPT AT - 6.045 SECONDS TIMER INTERRUPT AT - 8.045 SECONDS TIMER INTERRUPT AT - 10.045 SECONDS READY You may wonder why the TIME that was printed out was 45 milliseconds greater than the time that the interrupt was supposed to be generated. That's because the terminal used in this example was running at 4800 BAUD and it takes about 45 milliseconds to print the message TIMER INTERRUPT AT -" ". - 52 -