QB / QB64 Discussion Forum      Other Subforums, Links and Downloads
Respond to this messageReturn to Index
Original Message
  • You can try a ticks per second change to TIMER
    • (Login burger2227)
      R
      Posted Jan 11, 2008 1:06 PM

      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

    Login Status
  • You are not logged in
    • Login
      Password
       

      Optional
      Provides additional benefits such as notifications, signatures, and user authentication.


      Create Account
    Your Name
    Your Email
    (Optional)
    Message Title
    Message Text
    Options Also send responses to my email address
          


    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