Microsoft KB Archive/20572

= Timing Accurate to 1/60th Second Using PEEK or ToolBox Call =

Article ID: 20572

Article Last Modified on 10/20/2003

-

APPLIES TO


 * Microsoft QuickBasic Compiler for Macintosh 1.0
 * Microsoft QuickBasic Compiler for Macintosh 1.0
 * Microsoft QuickBasic Compiler for Macintosh 1.0

-



This article was previously published under Q20572



SUMMARY
BASIC provides a TIMER statement that is accurate to within one second.

In Macintosh System Version 4.2 and earlier, you can PEEK a Macintosh low memory address to obtain ticks accurate to 1/60th of a second, as shown below.

However, because this memory location may change in the future, you should instead call the system function TickCount using the Microsoft QuickBASIC ToolBox statement. The ToolBox statement is only provided in QuickBASIC for Macintosh, and is not available in earlier versions of the BASIC Interpreter or Compiler for Macintosh.



MORE INFORMATION
With Macintosh QuickBasic Versions 1.00, 1.00a, 1.00b, time intervals may be determined by using PEEKL(&H16A). &H16A is the Macintosh Global Ticks, the number of system "ticks" (1/60 second) since startup. Future versions of the Macintosh System may not support this as a global variable.

For future compatibility, a QuickBASIC program should use a call to the system function TickCount. The following QuickBASIC program shows how to call TickCount: ' This program requires QuickBASIC. ToolBox "i" ticks& = 0 ToolBox "LQ", &H975, ticks& PRINT "About"; ticks&\60 ;"seconds have elapsed since startup." Note that the value of TICKS is not guaranteed to be exact.

Versions of BASIC earlier than QuickBASIC do not support the ToolBox statement or PEEKL, but do support PEEK. PEEK can be used in combination with ON TIMER(1) GOSUB to provide timing accurate to sixtieths of a second as shown in the TICKS program below.

The following program returns the amount of time between two key presses, accurate to 1/60 of a second. The heart of the program is the function FNtic, which counts 0 to 255 by sixtieths of a second by making use of a PEEK into Macintosh low memory: ' TICKS ' This program is designed for Microsoft BASIC Interpreter 3.00 and ' earlier, and for the BASIC Compiler 1.00. DEF FNtic=PEEK(365)   ' FNtic counts 0 to 255 by 60ths of a second. loop: seconds=0             ' Seconds start out as 0. PRINT "Timing is done between your next two keystrokes:" nokey: a$=INKEY$ IF a$="" GOTO nokey   ' Start timing at key press: ON TIMER (1) GOSUB handler   ' Every second, increment seconds. TIMER ON             ' Turns on timer event trapping. startic=FNtic        ' Initial 60ths of a second. nopress: a$=INKEY$ IF a$="" GOTO nopress  ' Wait for 2nd press. endtic=FNtic   ' Final 60ths of a second. ticdif=ABS(endtic-startic) ' Adjust for rollover (255 to 0) of tic count: IF endtic=>startic THEN ticks=ticdif ELSE ticks=256-ticdif tenths=(ticks MOD 60)/60   ' Use MOD function, convert 60ths>10ths. total=seconds+tenths PRINT "elapsed time="total TIMER OFF             ' Turn off second counting until a key is pressed. GOTO loop handler:  seconds=seconds+1   :   RETURN

Additional query words: BasicCom MQuickB

Keywords: KB20572

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.