nano second counting

Discussion in 'Programmer's Corner' started by sytem_recon, Jun 14, 2010.

  1. sytem_recon

    Thread Starter Active Member

    Apr 21, 2009
    52
    0
    How we can make a counter with accurate nanosecond and picosecond steps measurement
     
  2. eng1ne

    Member

    Dec 4, 2009
    97
    3
    Give a little more background information; what is the application? What resources do you have?
     
  3. t_n_k

    AAC Fanatic!

    Mar 6, 2009
    5,448
    782
    Pretty small time frame. To put it in some sort of context, in one picosecond light travels about 300 μm.

    That's getting to be quite sophisticated technology. There are oscilloscopes commercially available with timebase control down to that order of magnitude. Interval counters with similar specs are also available.

    The attached link is to a paper which discloses a PC based system with 200ps resolution.

    http://www.dtic.mil/cgi-bin/GetTRDoc?AD=ADA427911&Location=U2&doc=GetTRDoc.pdf
     
  4. sytem_recon

    Thread Starter Active Member

    Apr 21, 2009
    52
    0
    I want a counter of this resolution through computer processor or programming.
     
  5. beenthere

    Retired Moderator

    Apr 20, 2004
    15,815
    282
    Good luck. Apparently Intel has pushed a processor to just over 5 GHz using LN2 cooling. That is a clock pulse width of around 100 picosceonds (50% duty cycle). To do counting, though, some internal register must be able to increment at the rate you wish. Programming is not a really significant factor.

    What is your interest?
     
  6. sytem_recon

    Thread Starter Active Member

    Apr 21, 2009
    52
    0
    I tried to count in C++ and assembly but the problem is that in actual the processor ticks with clock of Mhz and Ghz is only processor cycles so to count in ps or ns i have some problem. I have an application that has totla time to run is from 2ns to 12ns and i want measure that with computer so how i can do that.
     
  7. eng1ne

    Member

    Dec 4, 2009
    97
    3
    I am certainly no expert in computer programming or computer engineering but 2 ns is 500 Mhz.

    What processor does your computer have?

    As I understand it, the actual clock on even the more recent Intel Core i7 is only 133 Mhz.

    The Ghz speeds for processors are quoted based upon an internal multiplier. If the internal multiplier is 24 on a 100 Mhz clock, the processor is said to run at 2.4 Ghz.

    Presumably, if you can gain acess the internal clock speed, i.e. 2.4 Ghz or whatever your processor runs at then you would have a pretty good resolution.

    How to access it... (if it's at all possible) is not something I can help you with!

    You could however, find a 500 Mhz oscillator (example) and interface with that - but then your problem is finding some application quick enough to read it!
     
  8. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,789
    In order to count in those time frames you need to find devices that can be clocked at those rates. I don't know of any current logic family that has flip-flops that clock at over 1 GHz. In my recollection 500 MHz. is about the top for F-series or ECL logic.

    Next on my list of things to research would be a CMOS FPGA with a low core voltage and the capability of using a high speed clock. I hope your pockets are deep because I don't think this is going to come on the cheap.

    Other than that I believe you are SOL, unless you know something that all of us do not. In the words of Judy Tanuta: "It could happen!"
     
  9. sytem_recon

    Thread Starter Active Member

    Apr 21, 2009
    52
    0
    FPG is not a cheap solution. I want to solve this with the computer processor. Is it possible or not.
     
  10. eng1ne

    Member

    Dec 4, 2009
    97
    3
    You are doing the project so you are likely to have investigated our solutions and your own.

    Yes it's possible - but with your resources, I don't know! Only you can comment.

    Naturally then, you should tell us if it's possible.
     
  11. Papabravo

    Expert

    Feb 24, 2006
    10,140
    1,789
    Probably not possible given those constraints.
     
  12. CVMichael

    Senior Member

    Aug 3, 2007
    416
    17
    I don't understand why does it have to be a processor that does the counting ?

    All you need is some flip flops that are fast enough. And if not, then find some transistors that are that fast, and make your own flip flops.

    Since for every flip flop the frequency gets cut in half, then only the first few have to be fast.

    Reading the bits on the flip flops will be tricky, you have to "freeze" (or just stop counting) while you read the bits using a slower processor.

    I think it all depends what you need this for.
     
  13. davebee

    Well-Known Member

    Oct 22, 2008
    539
    46
    Is this on a Pentium or a related CPU? If so, then have you tried accessing the RDTSC register before and after the code runs to get the count of CPU clocks that the code took?

    I've used that instruction in the past on older pentiums. But even on the older CPUs, it was sometimes hard to get the correct permissions to execute the instruction, and I remember warnings about pipeline flushing that I didn't completely understand. I don't even know if the newest CPUs even still support that instruction, or allow users access to it.

    But if you can access it, then it will tell you how many CPU clock ticks have elapsed between any two reads of the register, which sounds like what you need. That will be sub-nanosecond resolution if you're running a machine with a clock over a gigahertz.

    I think it does funny stuff like flushing the CPU pipeline when it is called, but for your purposes that may not matter.
     
  14. davebee

    Well-Known Member

    Oct 22, 2008
    539
    46
  15. AsmCoder8088

    New Member

    Apr 17, 2010
    15
    1
    If you wish to use a computer for high frequency counting, then I recommend using the API suitable for your operating system.

    If you are using Windows, almost everyone uses the QueryPerformanceCounter() and QueryPerformanceFrequency() functions. As mentioned here, it has a frequency of 838 ns at its minimum resolution. It may in fact be better.

    Here is some code that uses QueryPerformanceCounter() to get you started:

    Code ( (Unknown Language)):
    1.  
    2. __int64 freq, start, end, diff;
    3.  
    4. // start
    5. QueryPerformanceFrequency((LARGE_INTEGER*)&freq);
    6. QueryPerformanceCounter((LARGE_INTEGER*)&start);
    7.  
    8. // code to measure
    9. Sleep(1234);
    10.  
    11. // end
    12. QueryPerformanceCounter((LARGE_INTEGER*)&end);
    13. diff = ((end - start) * 1000) / freq;
    14.  
    15. unsigned int milliseconds = (unsigned int)(diff & 0xffffffff);
    16. printf("It took %u ms\n", milliseconds);
    17.  
    (taken from here)

    If programming on Linux, I have used clock_gettime() function and it works well. Here is some reference on that.

    Basically, I would avoid using any code that is dependent on the CPU clock frequency. What happens when you give your program to a friend whose CPU is slower/faster than yours? Then it won't work. Sure, you can try to determine the CPU frequency before running the dependent code, and then scaling the count as appropriate... But really that would only work if the CPU is faster. If it is much slower, then it may not provide resolution required.

    It is almost always preferable to use the timing APIs provided by the host operating system than to roll your own, since in a multi-tasking environment your application does not necessarily get real-time updates and also, the OS writers likely know more about interacting with what's under the hood than you do. However, if you do have a RTOS, or raw timing hardware, then you've taken out a lot of the variables (like the effects of other programs running in the background) and it is easier to work with.

    But as you mentioned in an earlier post, you seem to want this high resolution clock accessible on a computer platform. In which case, you are advised to use the API functions I listed above (e.g., QueryPerformanceCounter, if using Windows, and clock_gettime if using Linux).

    Here are some additional links:

    http://www.songho.ca/misc/timer/timer.html

    http://www.devx.com/cplus/Article/35375/1954
     
    retched likes this.
  16. AsmCoder8088

    New Member

    Apr 17, 2010
    15
    1
    Hmm, yesterday I wrote a well-written response to the gentleman's question and it seems to be stuck in the moderator queue... ? Any ideas here?
     
  17. sceadwian

    New Member

    Jun 1, 2009
    499
    37
    Perhaps trying to contact a moderator might be in order. A post in the forum is probably not a good idea, they don't moderate every single last post that comes through so they might not see this one. Use the boards contact section to leave a message to a moderator.
     
  18. AsmCoder8088

    New Member

    Apr 17, 2010
    15
    1
    Ah, it seems the message has been posted now. It just took longer than expected (~23 hours).
     
  19. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    Very, VERY interesting.

    I had no clue about those functions.

    hmm. Good to know.
     
  20. sceadwian

    New Member

    Jun 1, 2009
    499
    37
    I don't know how this forum operates, but it would be common practice on a generally open forum to have posts containing certain keywords or combination of them to go to a moderator que. There may have been nothing wrong with your post but it went to the que because of whatever words triggered it, probably at the bottom, and after 23 hours it expired and went to the forum.
     
Loading...