473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

C or C++ in measuring microseconds

Hi guys,

I have tried a lot of code and read forums discussing about C or C++ functions that measures time in microsecond, but there still seems to be confusions about that. Can anyone tell me again how do I get the time up to 1 microsecond in resolution? It doesn't need to be very precise in measuring the time, but at least need to be able to measure 1 microsecond the minimum. I saw in some forum that says usleep() gives microsecond delay, but when I check the source code and try it out myself, it seems like it can only give up until 1 millisecond in resolution (please advise me if I am wrong).

There is also a function call select() that can give up until microsecond, but I could not compile it. The following four lines of codes are from an example, but it doesn't work for me.

[PHP]struct tv time;
time.tv_sec = 10;//num_seconds_to_sleep;
time.tv_usec = 10;//num_microseconds_to_sleep;

select(NULL,NULL,NULL,& time);[/PHP]

(line 1) error C2079: 'time' uses undefined struct 'wmain::tv'
(line 2) error C2228: left of '.tv_sec' must have class/struct/union
(line 3) error C2228: left of '.tv_usec' must have class/struct/union
(line 5) error C2660: 'select' : function does not take 4 arguments

These errors might be because I did not include a proper .h file. But when I followed the example code and include <unistd.h> and <sys/time.h>, another error which says "fatal error C1083: Cannot open include file: 'unistd.h': No such file or directory" came out. Is that because those are for linux? I am using Windows XP by the way.

Thanks a lot.
Sep 11 '08 #1
7 18271
Laharl
849 Expert 512MB
Yep, those files are for POSIX systems, aka Linux/Unix (unistd actually stands for unix standard). The select function does exist in Windows, but it works differently. Don't ask me how, but I know it does.

There's a function in the standard header <time.h> or <ctime> (for C/C++, respectively) that can measure time in clock cycles and defines a macro CLOCKS_PER_SEC, giving you seconds. From there, you can convert to milliseconds. Unfortunately, timing code is a real pain to do accurately, since the OS is constantly a) switching processes in and out and b) constantly interrupting the current process. As a general rule, the following will work:

Expand|Select|Wrap|Line Numbers
  1. #include <ctime>
  2.  
  3. int main(){
  4.      clock_t start = clock(); //clock_t is probably an unsigned int...but it's platform specific
  5.      //code to time goes here
  6.      clock_t end = clock();
  7. }
Sep 11 '08 #2
I have tried that method, but it only able to measure until 1 millisecond, but I need to measure 1 or 2 microsecond at least, thats why the code doesn't really work fine for me.

Or I would say like that, I would like to generate an output like a clock signal, with a period of about 3 or 4 microseconds, thats why I need to toggle the output signal every 1 or 2 microsecond. One thing to note is that, of course is good to have the signal period accurately, but it is not a must in my case.

Any better way to do that?

Thanks in advance.
Sep 11 '08 #3
weaknessforcats
9,208 Expert Mod 8TB
You run the code to be timed say 10,000,000 times. Then subtract the start time from the end time and divide by 10,000,000 to get your answer.
Sep 11 '08 #4
donbock
2,426 Expert 2GB
Or I would say like that, I would like to generate an output like a clock signal, with a period of about 3 or 4 microseconds, thats why I need to toggle the output signal every 1 or 2 microsecond.
Toggling an output every microsecond or two solely via software makes it sound like you're working with an embedded system. Is that true?

What is your target system?
> What processor?
> What operating system?
> What compiler are you using?
> Is it a cross-compiler?

Are you sure you have to accomplish this feat of precise timing solely through software? That is, does your target system have a timer output that you can program to produce a nice stable square wave at the desired frequency.

Cheers,
donbock
Sep 11 '08 #5
Basically, I am trying to establish communication between PC's parallel port and a microconverter which has a 8051 microcontroller on it. I am using the SPI functionality on the microconverter to communicate with the PC. In this case, as the PC would be the master device, it has to generate a clock signal and transmit to the microconverter, which is the slave device, through the SCLOCK pin.

From what I know is, the period of the clock signal need not be that accurate, as the master and slave devices both receive and transmit the data only at negative and positive edge, so it seems doesn't really matter whether the clock signal will stay at high and low level for the exact same time.

I am not sure what is the operating system for the microconverter, but it is a 8051 microcontroller, and I am using Keil C compiler to compile the machine code, which I believe is a cross-compiler.

Hope that clears up the confusion.
Sep 12 '08 #6
newb16
687 512MB
[quote=i12know]

, so it seems doesn't really matter whether the clock signal will stay at high and low level for the exact same time.
It doesn not matter either - look at datasheet of microcontroller - any duration larger than minimal is ok.

I am not sure what is the operating system for the microconverter, but it is a 8051 microcontroller, and I am using Keil C compiler to compile the machine code, which I believe is a cross-compiler.
Operating system for PC.
Sep 12 '08 #7
Banfa
9,065 Expert Mod 8TB
Have you considered something like a USB - SPI adaptor?

In my experience PCs are not terribly reliable at anything below a few milliseconds because normally they are running so many processes that they don't service the timer often enough.

Do you have to talk to this micro-controller using SPI? is there no RS232 interface available?

Alternatively if the USB adaptor is too expensive, or you require many of them (in a large scale commercial project say) it may be cheaper and easier to get another cheap micro-processor and build an RS232 to SPI converter allowing you to use the serial port of the PC.


So of these suggestions by be infeasible or on investigation silly but my point is there are many more ways out of the problem of getting a PCto talk on an SPI bus that programming the parallel port to do something it isn't really designed for.

The point is that in your environment, which is actually solving a systems problem you should not necessarily try to program your way out of every problem. There is no point spending £1000 writing software to solve a problem if you can solve the same problem by buying a £100 USB dongle. However I do see people do this very thing on a monthly basis.
Sep 12 '08 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Mudge | last post by:
I'm writing PHP Web sites that are database driven with mysql. I'm looking for ways to measure the performance (speed) of my php code, so that I can find ways to write the fastest php code. Any...
3
by: ´_¬¡¤F | last post by:
I can include time.h and use time() to get UTC seconds,but how can I get microseconds ?? My OS is windows. -- >> ¥xÆW¬ì¤j¸ê¤u¨t ²M¬y¯¸ #...
2
by: Michael Evans | last post by:
First, we rely on a stable update rate so that our physics and dynamics calculations and integrations are based on a known interval and therefore are true-to-life. Second, the graphics positions...
2
by: In. Martin Prá¹ek | last post by:
I I read the documentation well (PG 7.3.3 ) , i see that there is not a native support for data type UNIX TIMESTAMP and "unix timestamp expressed as microseconds" ie there it is impossible...
0
by: NTPT | last post by:
I read the documentation well (PG 7.3.3 ) , it seems that there is not a native support for data type UNIX TIMESTAMP and "unix timestamp expressedas microseconds" ie there it is impossible...
74
by: Dominik Wallner | last post by:
Hi! I'm currently implementing a program which measures voltages through an external USB-AD-converter. It should output those values as time/voltage pairs. My problem is to measure the time...
5
by: mroeloffs | last post by:
Hi I have a time in microseconds, for example 0x8C905CBA7F84AF4. I want this to a normal view in hh:mm:ss DD:MM:YYYY. I tried with datetime, but it only takes a max of 1000000 microseconds is there...
3
by: Francesco | last post by:
I search a function to calculate microseconds in a time istant
9
by: Ross | last post by:
I'm a newbie at this, and have searched a lot but can't find something that seems appropriate for measuring a recurring elapsed time. Creating an object with: var mydate = new Date(); seems...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.