QB / QB64 Discussion Forum      Other Subforums, Links and Downloads
 

 Return to Index  

You can try a ticks per second change to TIMER

January 11 2008 at 1:06 PM
  (Login burger2227)
R


Response to Thanks, Pete... but the routine has holes...

 
The following code demonstrates the most accurate timing possible in QBASIC. It changes the speed of the system timer and records whenever its value changes. Try changing the part where it draws a dot in the main loop to making it out to the ports you wanted. Further optimization is possible by PEEKing the byte of memory the TIMER's value is stored instead of using QBASIC. Also, QBASIC does lots of things between each command you give it so some variation is inevitable, especially under a multitasking environment???

Galleon

'----------------CODE-----------------------------------------
DECLARE SUB settickspersecond (speed#)
DEFINT A-Z

'value passed to settickspersecond must be above 18.2!
settickspersecond 20 'or above

DO 'main loop

PRINT ".";

'very accurate delay!
DO
newtime! = TIMER
LOOP WHILE newtime! = lasttime!
lasttime! = newtime!

LOOP

DEFDBL A-Z
SUB settickspersecond (speed#)
OUT &H43, &H36
OUT &H40, 255
OUT &H40, 255
scanfreq = speed
'Initial Calculations
TickFreq = 18.20678518# 'Number of Ticks per second
TimerResolution = 65536 * TickFreq 'Used to change computer's tick freq.
Delay = INT(TimerResolution / scanfreq) 'Calculates the delay between
'clock ticks (Quantized)
DelayL = Delay MOD 256 'Delay - Low Byte
DelayH = INT(Delay / 256) 'Delay - High byte
actualfreq = TimerResolution / Delay 'Actual frequency the computer is
'operating at
resfrac = TickFreq / actualfreq 'A resulting fraction used to easily
'calculate the elapsed time
'Set timer register to new timing rate (Timer now speeds up)
'Elapsed time 1s will now show up using "TIMER" as > 1s
OUT &H43, &H36
OUT &H40, DelayL
OUT &H40, DelayH
END SUB

The only problem may be that ANY QB routine used will slow down the procedure. Notice how just PRINT makes the accuracy off in Galleon's example. Try printing less often and it should improve. The fastest loop possible is a counting loop with no other calculations.

Ted


 
 Respond to this message   
Responses

 Copyright © 1999-2014 Network54. All rights reserved.   Terms of Use   Privacy Statement  

Newbies usually go to www.qbasic.com and click on The QBasic Forum
Forum regulars have their own ways, which include The QBasic Community Forums