|
-
Jan 18th, 2016, 12:45 PM
#1
Thread Starter
Member
Timer Interval Is No Accurate! Why???
Hi Every One.
As I know 1 Sec. is 1000 milliseconds so in the code blow the final number in label must be 1000:
Code:
Dim a As Integer
Private Sub Form_Load()
a = 0
Timer1.Interval = 1
Timer2.Interval = 1000
End Sub
Private Sub Timer1_Timer()
a = a + 1
End Sub
Private Sub Timer2_Timer()
Timer1.Interval = 0
Label1.Caption = a
End Sub
But the label shows 129 !!!!!!
Why???
Can any one tell me?
-
Jan 18th, 2016, 01:04 PM
#2
Re: Timer Interval Is No Accurate! Why???
I don't think you understand what a Timer control is.
They don't have anything to do with real-time intervals. They are "wake me up no sooner than" intervals and these are also low-priority events that can be discarded if you code isn't sleeping on its messagewait when they arrive.
Values below 64ms actually trigger at varying intervals (by machine) from 16ms to 64 ms. Any value below 10ms is first taken as 10ms (USER_TIMER_MINIMUM).
SetTimer function
-
Jan 18th, 2016, 01:12 PM
#3
Fanatic Member
Re: Timer Interval Is No Accurate! Why???
 Originally Posted by qazwsx123
Hi Every One.
As I know 1 Sec. is 1000 milliseconds so in the code blow the final number in label must be 1000:
...
But the label shows 129 !!!!!!
Why???
Can any one tell me?
Timing with timer is not deterministic - so to measure events accurately, you must use other methods than timer.
https://msdn.microsoft.com/en-us/lib...or=-2147217396
Even if timer allows one to set 1 millisecond interval does not mean it can fire within millisecond intervals - timer can fire 1/18 Hz intervals - so the minimum interval is about 55-56 milliseconds and non deterministic. In another words, firing interval could be anything from 1 to 56 milliseconds.
Last edited by Tech99; Jan 18th, 2016 at 01:22 PM.
-
Jan 18th, 2016, 02:11 PM
#4
Thread Starter
Member
Re: Timer Interval Is No Accurate! Why???
Thanks a lot.
Now how can I use real-time intervals in my program?
-
Jan 18th, 2016, 02:40 PM
#5
Fanatic Member
Re: Timer Interval Is No Accurate! Why???
In terms of real-time intervals, do you mean hard deterministic real-time** or 'pseudo' real time? Former is possible only with added real-time kernel, latter is possible by using multimedia timer or using queryperformance counter.
** fex...
https://en.wikipedia.org/wiki/RTX_(operating_system)
https://msdn.microsoft.com/en-US/lib...bedded.5).aspx
https://en.wikipedia.org/wiki/INtime
Last edited by Tech99; Jan 18th, 2016 at 02:47 PM.
-
Jan 18th, 2016, 06:25 PM
#6
Frenzied Member
Re: Timer Interval Is No Accurate! Why???
what do you want the intervals for?
may seem a stupid question
here to help
-
Jan 18th, 2016, 07:45 PM
#7
Re: Timer Interval Is No Accurate! Why???
Real-time timing can be a chore even in C on an Arduino.
See the video Julian Investigates: How Slow is Arduino? which covers that topic a little.
I'm not sure whether he quite understands that a C statement is not a machine instruction, but then he is an electrician and no computer scientist.
Last edited by dilettante; Jan 18th, 2016 at 08:19 PM.
-
Jan 18th, 2016, 07:56 PM
#8
Fanatic Member
Re: Timer Interval Is No Accurate! Why???
Here is some measurement. Event timing - ordinary 'std' Win32 assembler code ie. without realtime kernel additions.
http://s241.photobucket.com/user/205...mance.jpg.html
http://s241.photobucket.com/user/205...ance2.jpg.html
-
Jan 19th, 2016, 06:18 AM
#9
Re: Timer Interval Is No Accurate! Why???
Code:
Option Explicit
Private Declare Function QueryPerformanceFrequencyAny _
Lib "kernel32" _
Alias "QueryPerformanceFrequency" ( _
ByRef lpFrequency As Any) As Long
Private Declare Function QueryPerformanceCounterAny _
Lib "kernel32" _
Alias "QueryPerformanceCounter" ( _
ByRef lpPerformanceCount As Any) As Long
Dim i As Long
Dim f As Boolean
Private Sub Form_Load()
Dim SampleRate As Currency
Dim Freq As Currency
Dim Ct1 As Currency, Ct2 As Currency
Dim Num As Currency, i As Long
Dim Refr As Long
Me.Show
Freq = 1000 ' Set frequency 1 kHz
QueryPerformanceFrequencyAny SampleRate
SampleRate = SampleRate * 10000 ' Get high-performance counter frequency
Num = SampleRate / Freq / 10000 ' Calculate number of tick per period
Refr = Freq / 5 ' Refresh frequency 5 Гц
QueryPerformanceCounterAny Ct1 ' Get current counter
Do
QueryPerformanceCounterAny Ct2 ' Get current counter
If Ct2 - Ct1 >= Num Then ' If number of tick greater
i = i + 1
' ---------------------------------------
' This code is performed with needed frequency
' ---------------------------------------
If i Mod Refr = 0 Then ' Refresh
Me.Caption = i
DoEvents
End If
Ct1 = Ct2 + ((Ct2 - Ct1) - Num)
End If
Loop Until f
End Sub
Private Sub Form_Unload(Cancel As Integer)
f = True
End Sub
Anyway it is "dirty impulses", because period isn't same and depends on loading the system. However overall this is stable impulses.
-
Jan 19th, 2016, 07:44 AM
#10
Re: Timer Interval Is No Accurate! Why???
Yes, there are other counters and timers such as the multimedia timers too. They all have different issues and limitations because they serve different purposes. Without knowing the actual goal specific recommendations are difficult to make.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|