Results 1 to 18 of 18

Thread: nanosecond delay

  1. #1

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    8

    nanosecond delay

    hello all,

    i want to stop the execution of my .net application for about 20nanoseconds
    so i am trying to measure time needed for specific samples of code, and i want a code that takes only 20nanoseconds, can any one help please????


  2. #2
    Addicted Member vb_ftw's Avatar
    Join Date
    Dec 2010
    Posts
    139

    Re: nanosecond delay

    u can use system threading sleep()
    in the bracket find out 1 ns to ms and use it

  3. #3

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    8

    Re: nanosecond delay

    sleep can only accept integer values??

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: nanosecond delay

    There is no way to time events with such precision. Why would you need to wait such an insignificant amount of time anyway?
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    8

    Re: nanosecond delay

    hi,

    because i want to interface 3 wire spi analog switches with parallel port
    and this is the time to wait between high and low of the clk signal that has a maximum frequency of 30MHz.

    thanks

  6. #6
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: nanosecond delay

    Hi,

    I think the Timer used by VS has an accuracy of 366 nanoseconds.
    So, You can't do less of that.
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  7. #7
    Addicted Member
    Join Date
    Jun 2009
    Posts
    245

    Re: nanosecond delay

    The closest you can come, is by using a Stopwatch. But again, it "only" support ticks, and one tick is 100 nanoseconds.

  8. #8

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    8

    Re: nanosecond delay

    thanks to all,

    so do you think i can use less clock frequency so as to have greater time period the switches i have has a maximum frequency of 30MHz, do you think i can use less than that??

  9. #9
    PowerPoster boops boops's Avatar
    Join Date
    Nov 2008
    Location
    Holland/France
    Posts
    3,201

    Re: nanosecond delay

    There's a lot of confusion around here about Timers and Ticks. For a start, there are two kinds of Tick in dotNet: DateTime ticks which are 100 nanoseconds, and Stopwatch ticks which depend on the hardware but are typically 280 nanoseconds or so (on >="Vista ready" PCs, which have high-res timers).

    Both of these are the smallest units for representing time, but they do not have any bearing on the accuracy of measuring time. Even a stopwatch, supposedly the most accurate measurement, has an overhead of about 8 ticks just to read it, as you can easily tell by running the following code:
    vb.net Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim sw As Stopwatch = Stopwatch.StartNew
    3.         Console.WriteLine("Stopwatch overhead = " & sw.ElapsedTicks.ToString & " ticks")
    4.         Console.WriteLine(" = " & (sw.ElapsedTicks * (10 ^ 9) / Stopwatch.Frequency).ToString & " nanoseconds")
    5.         Console.WriteLine("1 tick = " & (1 * (10 ^ 9) / Stopwatch.Frequency).ToString & " nanoseconds")
    6.     End Sub
    On my PC it gives this console output (after the first run, which takes much longer):
    Code:
    Stopwatch overhead = 8 ticks
     = 18158,7324646009 nanoseconds
    1 tick = 279,365114840015 nanoseconds
    That means you can't even measure time anything more accurately than about 8 ticks or 2 microseconds.

    When it comes to doing anything with those measured timings it's a different ballpark. Managed languages like VB.Net and Csharp are event based, and events can only be fired with an accuracy of about 55 milliseconds. The Forms.Timer, which fires on the GUI, is prone to interruption by other threads, so it may be even less accurate. The Timers.Timer is more accurate because it fires on its own thread, but you can't use it to Refresh (only Invalidate) the GUI. Timer intervals as low as about 10 milliseconds can still be useful, but they cannot be relied on to be regular.

    Windows also has a timer called the Multimedia Timer, which is used for things like high-speed game loops, video and audio. According to msdn it provides the most accurate timing possible with the given hardware, and suggests intervals like 1 millisecond are achievable. You can only use it directly from unmanaged code (C++ or the like) or indirectly though DirectX, WPF, XNA etc. There are several articles on the CodeProject by people who claim to take advantage of the MM timer in Csharp, but I wonder whether they are really useful.

    To sum up, there is no way of triggering anything like a nanosecond delay in VB.Net. Of course, if you have special hardware with a 50MHz clock, you might be able to start and stop it with VB.Net.

    BB

  10. #10

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    8

    Re: nanosecond delay

    hello,

    is it possible to use this formula to have 17nS:

    Thread.Sleep(17 * 1000 / (1000 * 1000 * 1000))

  11. #11
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: nanosecond delay

    No, you will never get such resolution without any real time extensions or a hardware solution.

    Regardless, what you are trying to achieve with a parallel port is impossible: you will need additional hardware.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  12. #12

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    8

    Re: nanosecond delay

    hello,

    so i heard that am inneed to use an external clock of 50MHz or so, or additional hardware??
    what kind of hardware i need to use??

    please help

  13. #13
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: nanosecond delay

    Why do you think you need an 'external clock' of 50mhz? What is it you are trying to do?

    That kind of frequency is in the high speed DAQ range, where hardware costs range from a few hundred to $10k or more.

    The hardware required is dependent on what you want to achieve.
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  14. #14

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    8

    Re: nanosecond delay

    hello,

    thanks

    what am trying to do is to use my parallel port to interface 3 wire spi analog switches, because i can't afford the cost of an spi interface,
    so i thought of programming it by myself. the clock for these switches has a maximum frequency of 30MHz, now am thinking of using less frequency and i don't know if it will affect the performance of these switches????


    i have been working in this project for 5 months and i have only one month left and am stuck !!!!!

  15. #15
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,754

    Re: nanosecond delay

    An analog switch needing nanosecond accuracy? Is this an off the shelf product? If it is post a link to it.
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  16. #16

    Thread Starter
    New Member
    Join Date
    Dec 2010
    Posts
    8

    Re: nanosecond delay

    hello,

    here is the link for the switch and the attached file gives the datasheet too.

    http://www.analog.com/en/switchesmul...s/product.html

    i appreciate your help
    thanks
    Attached Images Attached Images

  17. #17
    Fanatic Member
    Join Date
    Sep 2009
    Location
    Lakewood, Colorado
    Posts
    621

    Re: nanosecond delay

    You cannot come close to that resolution. AND there is no reason to try.

    1. The latency/timing uncertainty in dlls used to access the parallel port far exceed 20 nS. A single statement of even the simplest form (probably) exceeds 20 nS

    2. Parallel port hardware is not capable of the 30 MHz speed that you want - not even close (1 MHz, perhaps).

    Dick
    Richard Grier, Consultant, Hard & Software
    Microsoft MVP (Visual Basic)

  18. #18
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: nanosecond delay

    Okay, You meant SPI not spi (I thought spi was a spelling mistake): as in Serial Peripheral Interface?

    Why not simply work up from 1 second clock cycle and see how far you get before your bus becomes unstable through the parallel port?

    Because of the highly synchronous nature, you aren't going to achieve anything remotely close to the Khz range. Unless there's a lower limit on clock frequency, you can at least get your protocol established and tested.

    The frequency specced is an UPPER frequency limit at which performance will degrade. I would suspect the lower frequency limit is effectively DC; you can test that with physical switches (this is all assuming you know what you are doing, electrically).
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width