Still Need Microsecons...

Hi fellows,
I tried at the advice of RichW the following to achieve a short delay of a few miliseconds, but unfortunately it kocks my computer during the time.
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Sleep 0.05

Any other ideas how to wait just about 50-60, say, at most 200 microseconds? I might need to make this value variable in the run-time too.
Thank you.
robertutAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
RichWConnect With a Mentor Commented:
Glad I could help with the Sleep API.
RichW
0
 
ventondCommented:
You can always set a timer control to go off after x amout of time.

Timer.interval = x
timer.enabled = true

0
 
Arthur_WoodCommented:
to ventond,

the finest time step for a timer is 1 millisecond = 1000 microseconds.  That WILL NOT meet the needs of this question.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
RichWCommented:
He originally said that he doesn't want to use a timer object.

Robertut, what do you mean it kocks your computer?

You declared the API in a .bas module and enter the Sleep 0.50 statement right before your function?  then what happens?

RichW
0
 
mcoopCommented:
Try searching under high resolution timer

You're right sleep() does stop your thread until the delay is over, and is not practical for anything much less than 50mS.

With a hi-res timer (there are a few around), you can get reliable timing well below 1mS (I can't remember the low limit).

Good luck
0
 
robertutAuthor Commented:
To RichW,
yes.
but, the performance monitor in the windows task manager jumps up to 100% during the whole routine.
It works, but the main problem is that i can hardly switch the routine off, my program responds very hard andeven stops responding. If i comment the sleep, it doesn't(but it doesn't read accurately)
I thougt about using soemthing like DoEvents(which does work well), but the duration of that cannot be predicted.
I am using the DLPortIO driver(www.sstnet.com) to access the LPT directly, this is a driver specially created for data aquisition, it is very fast.
Here's my code:

For i = 0 To 1000
DlPortWritePortUchar &H378, 0  'select hw channel 1
DlPortWritePortUchar &H37A, 0  'start hw follow
DlPortWritePortUchar &H37A, 1  'start hw memory
DlPortWritePortUchar &H37A, 3  'init hw A/D conversion
DlPortWritePortUchar &H37A, 1  'start hw A/D conversion
Sleep wait   'wait until conversion complete
DlPortWritePortUchar &H37A, 0  'select hw output mux1
puncteb1(i) = DlPortReadPortUchar(&H379)  'read value
DlPortWritePortUchar &H37A, 4  'select hw output mux2
puncteb2(i) = DlPortReadPortUchar(&H379)  'read value
Next i

where wait = 0.1 tipically, but must be set with a scrollbar.
Thx
0
 
robertutAuthor Commented:
To RichW,
yes.
but, the performance monitor in the windows task manager jumps up to 100% during the whole routine.
It works, but the main problem is that i can hardly switch the routine off, my program responds very hard andeven stops responding. If i comment the sleep, it doesn't(but it doesn't read accurately)
I thougt about using soemthing like DoEvents(which does work well), but the duration of that cannot be predicted.
I am using the DLPortIO driver(www.sstnet.com) to access the LPT directly, this is a driver specially created for data aquisition, it is very fast.
Here's my code:

For i = 0 To 1000
DlPortWritePortUchar &H378, 0  'select hw channel 1
DlPortWritePortUchar &H37A, 0  'start hw follow
DlPortWritePortUchar &H37A, 1  'start hw memory
DlPortWritePortUchar &H37A, 3  'init hw A/D conversion
DlPortWritePortUchar &H37A, 1  'start hw A/D conversion
Sleep wait   'wait until conversion complete
DlPortWritePortUchar &H37A, 0  'select hw output mux1
puncteb1(i) = DlPortReadPortUchar(&H379)  'read value
DlPortWritePortUchar &H37A, 4  'select hw output mux2
puncteb2(i) = DlPortReadPortUchar(&H379)  'read value
Next i

where wait = 0.1 tipically, but must be set with a scrollbar.
Thx
0
 
robertutAuthor Commented:
To RichW,
yes.
but, the performance monitor in the windows task manager jumps up to 100% during the whole routine.
It works, but the main problem is that i can hardly switch the routine off, my program responds very hard andeven stops responding. If i comment the sleep, it doesn't(but it doesn't read accurately)
I thougt about using soemthing like DoEvents(which does work well), but the duration of that cannot be predicted.
I am using the DLPortIO driver(www.sstnet.com) to access the LPT directly, this is a driver specially created for data aquisition, it is very fast.
Here's my code:

For i = 0 To 1000
DlPortWritePortUchar &H378, 0  'select hw channel 1
DlPortWritePortUchar &H37A, 0  'start hw follow
DlPortWritePortUchar &H37A, 1  'start hw memory
DlPortWritePortUchar &H37A, 3  'init hw A/D conversion
DlPortWritePortUchar &H37A, 1  'start hw A/D conversion
Sleep wait   'wait until conversion complete
DlPortWritePortUchar &H37A, 0  'select hw output mux1
puncteb1(i) = DlPortReadPortUchar(&H379)  'read value
DlPortWritePortUchar &H37A, 4  'select hw output mux2
puncteb2(i) = DlPortReadPortUchar(&H379)  'read value
Next i

where wait = 0.1 tipically, but must be set with a scrollbar.
Thx
0
 
robertutAuthor Commented:
sorry fellows, my internet connection really sucks... :(
0
 
RichWCommented:
0
 
amebaCommented:
>main problem is that i can hardly switch the routine off, my program responds very hard andeven stops responding

DoEvents is slow, it will allow execution of low priority tasks, like paint events.  But there are special functions for tight loops where execution speed is critical: GetInputState and GetQueueStatus

Here is the sample for loop, which can be interrupted by pressing "Stop" button using the keyboard.  To react to mouse events, change "QS_KEY" in "If GetQueueStatus(QS_KEY)" to some other value (e.g. QS_MOUSE)

' Form1, add button
Option Explicit
Private stopped As Boolean  ' flag
Private Declare Function GetInputState Lib "user32" () As Long
Private Declare Function GetQueueStatus Lib "user32" (ByVal fuFlags As Long) As Long
Private Const QS_HOTKEY = &H80
Private Const QS_KEY = &H1
Private Const QS_MOUSEBUTTON = &H4
Private Const QS_MOUSEMOVE = &H2
Private Const QS_PAINT = &H20
Private Const QS_TIMER = &H10
Private Const QS_MOUSE = (QS_MOUSEMOVE Or QS_MOUSEBUTTON)
Private Const QS_INPUT = (QS_MOUSE Or QS_KEY)
Private Const QS_POSTMESSAGE = &H8
Private Const QS_SENDMESSAGE = &H40
Private Const QS_ALLEVENTS = (QS_INPUT Or QS_POSTMESSAGE Or QS_TIMER _
    Or QS_PAINT Or QS_HOTKEY)
Private Const QS_ALLINPUT = (QS_SENDMESSAGE Or QS_PAINT Or QS_TIMER Or _
    QS_POSTMESSAGE Or QS_MOUSEBUTTON Or QS_MOUSEMOVE Or QS_HOTKEY Or QS_KEY)

Private Sub Command1_Click()
    Dim i As Long, x As Long
   
    If Command1.Caption = "Stop" Then
        stopped = True
    Else
        Command1.Caption = "Stop"
        stopped = False
       
        ' your loop
        For i = 1 To 20000000
            x = i + 1
            x = i - 1
            '...
           
            ' from time to time check if there is any input waiting
            If i Mod 100 = 0 Then
                If GetQueueStatus(QS_KEY) Then  ' key event is waiting?
                    DoEvents                    ' allow execution of event
                    If stopped Then Exit For
                End If
            End If
        Next
       
    End If
    Beep  ' done
    Command1.Caption = "Start task"
End Sub


If something is not clear, ask for clarification.
0
 
robertutAuthor Commented:
Hi!
I fixed the things this way:
instead of calling sleep(500) that locked my computer, i called sleep(10) 50 times in a for... next structure.
now it's ok, because I need a variable value of time.
Thx
Bye
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.