Timer accuracy 
Author Message
 Timer accuracy

In my application I need a timer-interval of 100 ms. I found it is not
very accurate. Trying to 'tune' the timer to aquire a acceptable
inaccuracy caused the accuracy to change in steps; not gradually!
 I think the problem is that Vb uses the system timer (runs at 18.2
Hz) to control it's timer.

Is there a way to increase timer accuracy.

I do use VB3.0 and W3.11

Greetings, H. Luijmes



Tue, 15 Aug 2000 03:00:00 GMT  
 Timer accuracy

I had to overcome this as well.  I wrote a routine that gives quite good
sub millisecond accuracy.  It was a while ago but it went something like
this:
Get the time from the timer
Call a function that  for next loops  for around 5 seconds (no DoEvents in
the function), passing it a parameter telling it how many times to for/next
loop.
When the function returns get the timer time again (~5 seconds).  You can
then with a few lines of code, have a routine that delays Nseconds per
for/next loop.  This routine should be run once when the program starts to
obtain the constant, or just save the constant in a file and have the
program read it each time it begins running.  Note I use a general delay
routine which follows this procedure for delays less than one second, and
just counts timer ticks for times > 1 second.  Also, this constant is very
much machine dependent! And if there is a lot of network/interrupt stuff
happening while the loop constant is being derived, there will be
inaccuracy in the constant, so keep network/interrupt stuff to a minimum
while doing the initial loop.

Hope this helps
Joe



Quote:
> In my application I need a timer-interval of 100 ms. I found it is not
> very accurate. Trying to 'tune' the timer to aquire a acceptable
> inaccuracy caused the accuracy to change in steps; not gradually!
>  I think the problem is that Vb uses the system timer (runs at 18.2
> Hz) to control it's timer.

> Is there a way to increase timer accuracy.

> I do use VB3.0 and W3.11

> Greetings, H. Luijmes



Tue, 15 Aug 2000 03:00:00 GMT  
 Timer accuracy

VB 5, Using the Multimedia timer of Windows from winmm.dll, you may have a
resolution of 1 millisecond and this timer is not stopped when a form is
moved or a modal dialog box is open. I noticed only a small jitter of +-
1ms, when I programmed an interval of 250 ms, I got sometimes 249 or 251
ms, but most intervals were 250 ms long.

Here is a short example, but write each declare on a single line. The
Callback function possible with VB5 is used here.

' declarations in a code module

Public Const TIME_ONESHOT = 0
Public Const TIME_PERIODIC = 1

Declare Function timeKillEvent Lib "winmm.dll" (ByVal uID As Long) As Long
Declare Function timeSetEvent Lib "winmm.dll" (ByVal uDelay As Long,
ByVal uResolution As Long, ByVal lpFunction As Long, ByVal dwUser As
Long, ByVal uFlags As Long) As Long

Public FID&

' the Callback function within the code module
Sub TimeCallback(TimerID%, Msg%, dwUser&, dw1&, dw2&)
Static i&
i = i + 1
End Sub

' and now the code for the form load and unload events

Private Sub Form_Load()
Show
FID = timeSetEvent(250, 0, AddressOf TimeCallback, 0, TIME_PERIODIC)
End Sub

Private Sub Form_Unload(Cancel As Integer)

If FID <> 0 Then timeKillEvent FID

End Sub

I tested it, it works great!

Submitted by:
Uwe Hercksen

For EMail response, please replace nospam with zew
'--------------------------------------------------'
HTH
--
Henri
remove "MyName"



: In my application I need a timer-interval of 100 ms. I found it is not
: very accurate. Trying to 'tune' the timer to aquire a acceptable
: inaccuracy caused the accuracy to change in steps; not gradually!
:  I think the problem is that Vb uses the system timer (runs at 18.2
: Hz) to control it's timer.
:
: Is there a way to increase timer accuracy.
:
: I do use VB3.0 and W3.11
:
: Greetings, H. Luijmes
:



Wed, 16 Aug 2000 03:00:00 GMT  
 Timer accuracy

Quote:



> > In my application I need a timer-interval of 100 ms. I found it is not
> > very accurate. Trying to 'tune' the timer to aquire a acceptable
> > inaccuracy caused the accuracy to change in steps; not gradually!
> >  I think the problem is that Vb uses the system timer (runs at 18.2
> > Hz) to control it's timer.

> > Is there a way to increase timer accuracy.

There is a multimedia timer with 1 ms resolution, and IIRC it's
available in Win3 too. Look into the multimedia help files.
Here are two function calls to look for:

MMRESULT timeSetEvent(UINT uDelay, UINT uResolution,
    LPTIMECALLBACK lpTimeProc, DWORD dwUser, UINT fuEvent);

void CALLBACK TimeProc(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1,
    DWORD dw2);

This would need another language than VB3, though.

HTH,
Peter
--
Peter Lindgren                        Bachelor of Computer Engineering
Norrsken Konsult, M?lndal, Sweden                 +46 - (0)31 27 17 09



Wed, 16 Aug 2000 03:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Timer, accuracy in millisecs!

2. vb timer accuracy ???

3. Timer Accuracy - Help !

4. Timer Accuracy

5. Timer accuracy??? -RFH ( Request for Help)

6. timer accuracy of mmsystem.dll

7. Timer accuracy

8. Timer Function Accuracy

9. Accuracy of Timers in VB5

10. Timer with high accuracy

11. Wanted: VB3 Timer with millisecond accuracy.

12. Accuracy of timers in VB?

 

 
Powered by phpBB® Forum Software