03-11-2010 11:09 AM
Hi All,
I am wondering if there is a better way to generate a software clock for the USB-6501 in Measurement Studio for VS2008 in C Sharp?
I've developed a clock by using the C Sharp command "Thread.Sleep(msecPauseTime);" and statements to toggle the digital output high and low. There are a few things I've noticed by creating a software clock this way:
In my application, I am creating signals (a clock, a Latch Enable, and serial data) to control an RF step attenuator via the USB-6501 connected to a USB2.0 port on my computer. This particular RF step attenuator can accept clocks with frequencies up to 10 MHz, so I would like to generate a software clock (without having to connect an external clock to an input line on the USB-6501) that is closer to this maximum frequency, and I would think that the USB2.0 line could handle clock speeds much greater than 500 Hz. I'd also like to know why the delays I see on the scope are sometimes longer than the delays specified by the Thread.Sleep command. Is it caused by the processor pausing the execution of my program to do something else, as I suspect? Granted, this isn't a big deal, as it doesn't affect the timing that my serial data and LE bits change relative to my clock. However, I would like to know why it's doing this.
I appreciate your help.
Thank you,
Jonathan Becker
Ph.D. Research Engineer
Carnegie Mellon Silicon Valley
Solved! Go to Solution.
03-14-2010 11:19 PM
Hi Jonathan,
You are correct about the cause of the delays you are seeing. The operating system's job is to schedule processor time for each of the system's tasks and it is constantly switching which task is the active one. The OS will do the best it can to approximate a 1ms delay, but due to unpredictability regarding which process is active at any given time the interval will not always be exactly 1 ms.
Unfortunately, due to the limitations of running inside of an operating system the smallest sleep interval it will allow you to approximate is 1 ms. Since the USB-6501 DIO is software timed the highest frequency you will be able to produce is ~500 Hz using the method you described.
Regards,
03-15-2010 12:02 AM
Barron,
Thanks for you answer.
So, if I want a clock faster than ~500 Hz, would it work with the USB-6501 DIO if I were to bring an external clock on one of the DIO pins set as an input line, and then work with this external clock in software? Or am I stuck with a ~500 Hz software clock (as your message seems to imply) as long as I use the USB-6501 DIO?
If I am stuck with the ~500 Hz software clock while using the USB-6501 DIO, what (if any) C# commands would I use to make sure that my C# task that creates the software clock is always the top priority task while it runs, so I can get a more consistent 1 ms timing interval?
Thanks again for your help,
Jonathan
03-15-2010 11:41 AM
Jonathan,
Since the USB-6501's DIO is software timed you are at the mercy of the operating system's scheduling and will not be able to reliably work with an external clock in software.
You can try setting the priority of your "clock generation" thread to improve the performance, however, since Windows is not a deterministic operating system there are still no guarantees. Operating systems are not required to honor the priority of threads. You can find examples and information about setting thread priorities in C# here:
http://msdn.microsoft.com/en-us/library/system.threading.thread.priority.aspx
Regards,
03-18-2010 12:12 AM
I know how dodgy this sounds, but if time is critical for you then I can recommend polling the systems performance counter.
How To: Time Managed Code Using QueryPerformanceCounter
MSDN: QueryPerformanceCounter()
I have a timer that reads the performance counter value, works out what I want the value to be for my next event and poll the counter until the time occurs and then throw my event.
Before throwing your event you can calculate the next wake up counter value required so that the time your event takes to actually process doesnt affect your clock.
Or you can use C#'s event mechanisms which should spawn a new thread for your events to run on anyway.
I have tested this and get very good performance using a dual core machine. You are essentially wasting an entire core of your CPU to just give you accurate time, but it does work.
Of course, when you load up the system then you can never guarentee when you are going to get your thread's work done.
Dodgy, but effective software clock!
- Justin