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.
7 18008
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: -
#include <ctime>
-
-
int main(){
-
clock_t start = clock(); //clock_t is probably an unsigned int...but it's platform specific
-
//code to time goes here
-
clock_t end = clock();
-
}
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.
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.
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
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.
[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.
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.
Post your reply Sign in to post your reply or Sign up for a free account.
Similar topics
6 posts
views
Thread by Mudge |
last post: by
|
3 posts
views
Thread by ´_¬¡¤F |
last post: by
|
2 posts
views
Thread by Michael Evans |
last post: by
|
2 posts
views
Thread by In. Martin Prá¹ek |
last post: by
|
reply
views
Thread by NTPT |
last post: by
|
74 posts
views
Thread by Dominik Wallner |
last post: by
|
5 posts
views
Thread by mroeloffs |
last post: by
|
3 posts
views
Thread by Francesco |
last post: by
|
9 posts
views
Thread by Ross |
last post: by
| | | | | | | | | | |