can I use USB ADC11/10 converter as timer for short delays?

Post your VB and VBA discussions here
Post Reply
inschris
Active User
Active User
Posts: 13
Joined: Sun Oct 16, 2005 2:28 pm
Location: Capetown

can I use USB ADC11/10 converter as timer for short delays?

Post by inschris »

Greetings Gurus! I envisage an application (VB6, or VBA macro in Excel; VBA in Excel 2000 also seems to be VB6) using one USBADC11/10 converter. Readings will be collected, but also control output implemented in the form of changes in settings (on or off) of the two Digital Outputs, DO1 and DO2. I want to effect on either DO a sequence from on to off then off to on, or vice versa, with a short interval of a few ms between. Alternatively, a sequence DO1 on or off, then DO2 on or off, with similar interval between. I have not succeeded in finding a timing mechanism for such short interval in the VBA.

But the USBADC11 convertor itself requires time to operate. To utilise this, I tried following code:

handle = UsbAdc11OpenUnit()
opened = handle <> 0
If opened Then ' Prepare for Run: take readings from channel 1 to 11 incl
...
sampleInterval = UsbAdc11SetInterval(handle, 500, 1, channels(0), 11)
' gives sampleInterval of about 550us. Hence:
Debug.Print Timer ' and for delay:
For j = 0 To 999
ok = UsbAdc11Run(handle, 1, 0)
Do While Ready = 0
Ready = UsbAdc11Ready(handle)
Loop
Next j
newTime = Timer
Debug.Print newTime; j
Call UsbAdc11CloseUnit(handle) ' Close the unit when finished to drop the driver
End If

Output in the immediate window was:
39002.48
39006.54 1000
ie 4.06s with 1000 loops.

And with j=99, output was:
39629.51
39629.95 100
ie 0.44s with 100 loops.

So it seems that a single sequence:
ok = UsbAdc11Run(handle, 1, 0)
....
Loop
is taking around 4ms, even tho sampleInterval is only 550us. Am I right in thinking that the apparent increase as j (the number of loops) diminishes has to do with the mediocre resolution of the VBA function Timer? Which is about 55ms, I understand, therefore I haven't tried directly to measure the duration of a single loop.

Results were similar when the number of samples was 3 instead of 1; that didn't seem significantly to increase the time with j=999, or 99.

So, can I run that sequence just once for a serviceable means of effecting a delay of about 4ms?

Is there any risk in this of damaging the USBADC11 converter, notably if done 10 or 15 times in quick succession?

Thanks in anticipation for valued responses.

AndyB
User
User
Posts: 9
Joined: Fri Sep 08, 2006 8:32 pm

Post by AndyB »

If you want to get decent timings try the API call

Declare Function GetTickCount lib "kernel32" () as long

Dim CurrentT as long
CurrentT=GetTickCount

This will return a value in milliseconds(elapsed since windows was started) each time it's called, so read it before and after and compute the result

inschris
Active User
Active User
Posts: 13
Joined: Sun Oct 16, 2005 2:28 pm
Location: Capetown

Post by inschris »

Thanks, AndyB, for the response 17/9/06. I will experiment with the suggested API call.

But I'll still be grateful if someone can help re the qu's in the original post:
- can I run that sequence
ok = UsbAdc11Run(handle, 1, 0)
Do While Ready = 0
Ready = UsbAdc11Ready(handle)
Loop
just once for a serviceable means of effecting a delay of about 4ms?

- Is there any risk in this of damaging the USBADC11 converter, notably if done 10 or 15 times in quick succession?

AndyB
User
User
Posts: 9
Joined: Fri Sep 08, 2006 8:32 pm

Post by AndyB »

The code that you have listed is the same as that used in the pico demos, so it should be OK. i.e Start the requested AtoD loop and then poll for Ready (capture complete). As for calling it in quick succession, that is also quoted in the demo as a away of collecting a sequence of 'almost' joined blocks i.e. after defining the trigger conditions and the collection parameters just keep calling start and retrieving the data. Thought, the trigger condition can be set to run from a timed or delayed trigger, might be an easier way to engineer your solution. My own experience is that you will have to empty the buffer with the one of the getdata commands. I have not had the unit long and experiment might give a work around for this if it hampers your timing.

AndyB
User
User
Posts: 9
Joined: Fri Sep 08, 2006 8:32 pm

Post by AndyB »

Sorry for the double post, but it got me wondering just how short a delay is possible with VB6 code. I tried the following;

'Create module - modDelay - and paste the following code

Declare Function GetTickCount Lib "kernel32" () As Long

Public Function WaitOnDelay(DelayWaitms As Long) As Long
Dim StartTime As Long
Dim DeltaTime As Long
'initialise the timer
StartTime = GetTickCount
While (GetTickCount - StartTime) < DelayWaitms
'compute elapsed time
'rem (') the following line out for max performance on small timings
'function will then always return 0, you will have to assume accuracy

DeltaTime = GetTickCount - StartTime
Wend

'Returns the actual delay
'returning of 0 indicates that the dealy was incurred before the delay loop cycled once
WaitOnDelay = DeltaTime
End Function
----------------------------------------------------------
create a form - frmDelayTest - with the following controls;
'textbox> txtDelay - enter the taget number of milliseconds for a delay
'command button> btnStart to start the process
'label> lblResult - to display the actaul delay

'and paste the following code

Private Sub btnStart_Click()
lblResult.Caption = Str(WaitOnDelay(Val(txtDelay.Text)))
End Sub

Testing in the VB6 design environment (compile before run) indicates that 4ms delay might be pushing it a bit (running on 3GHz Athlon64). I tried a do loop until originall, but this meant at least one run through the comparison, a 4ms delay appears to be generated just by the comiler running through the (very few) lines of code. 10 ms and above seems to be reasonable. Windows system overhead tasks also seem to give a little variation on the timings. Delays of 100 ms and greater seem to give very accurate results. Ithink this method will give much more accurate results than trying to use calls the pico dll's as delays. Have fun.........

Post Reply