# help - timer in C?

• 05-15-2010
psu_ece
help - timer in C?
Hi guys. Sorry if this is a regularly posted question, but here goes:

We're currently working on a project for a class based on C programming. Last term, we did the same class, but with MatLab. (I'm not sure if you guys are familiar with the software)

The project that we're assigned this term is to power two 7-segment LED's to reflect elapsed time in seconds. We've searched around, and found "time.h" and the "clock_t" function, but it seems that it only keeps track of time in full seconds? Also, it doesn't seem to have the functionality that we are looking for - we can calculate the difference in time via "difftime(t2-t1)", but in order for this to work effectively, we must calculate the difference constantly.

Right now, we've got a good idea of what the code should look like, but as you can see, we keep running into a wall in regards to measuring time.

The reason I brought up MatLab earlier in the post is because there's a function called tic toc. "tic" starts a timer in MatLab, while "toc" returns a time value since the intial "tic". Is there something in C that is similar to this?

for example, for our last term's project, we did the following:
Code:

```<headers, blah blah blah blah etc> tic while (toc < [set time values]) { do stuff } etc```
Thanks for the help!
• 05-15-2010
rodrigorules
edit: I am reading time.h now and there is a method called clock() .. it returns clock_ticks since launch of program , there is also a variable called CLOCKS_PER_SEC .. dividing your ticks by this variable gives you seconds.. (using a double will allow for larger precision than seconds)
do this

Code:

```#include <time.h> main() { clock_t launch = clock(); //do work clock_t done = clock(); double diff = (done - launch) / CLOCKS_PER_SEC; }```
• 05-15-2010
Brafil
Implement them yourself. Simply store the time elapsed before tic() and then use this information to get the time since tic() when you call toc(). BTW, clock_t is an integral type, and clock a function.

EDIT: Shouldn't it be "(toc < [set time values])"? That seems more useful for me, unless you want the loop terminate if it iterates too fast.
• 05-15-2010
jeffcobb
If you are working on Linux there are kernel timers that will give you better granularity in that but unless the kernel is compiled in RTOS mode, any resolution < 20ms (if I recall correctly) will be iffy.
• 05-15-2010
MK27
Quote:

Originally Posted by jeffcobb
If you are working on Linux there are kernel timers that will give you better granularity in that but unless the kernel is compiled in RTOS mode, any resolution < 20ms (if I recall correctly) will be iffy.

It's 10ms/multiples of 10 (I've tested this*). That means the userspace function "nanosleep" should not be called with a granularity finer than 100000ns (1/100th sec).

If you are using *nix I wrote some examples of how to use the userspace timers here:

SourceForge.net: POSIX timers - cpwiki

I would presume that all OS's have some equivalent to nanosleep and/or ntp_gettime.

* "You can test this yourself by calling nanosleep with a 10000 nanosecond (1 millisecond) delay 10000 times -- it will work out to much more than 10 seconds. However, if you ask for 100000 nsecs (1/100th second) 1000 times, you will get exactly 10 seconds."
• 05-15-2010
psu_ece
Thanks in advance for the replies, guys.

rodrigorules: the problem with that is we must be checking for time constantly. Our circuit is powered by a LabJack U3, which recieves commands through our C source code. The problem with clock_t, it seems, is that you must start/stop the time and then calculate the difference.

If I start "clock()" outside the while loop, and then stop "clock()" inside, does "clock()" cease immediately? i.e. if I stop "clock()" a SECOND time, nothing happens/ an error occurs? The way our code is working to control the two segments is this: There are a total of nine digital outputs: one to power on/off each LED assembly, and then seven for each segment of the LED assembly. (They're joined together, so if I turn on both assemblies + light up a "7" via source code, they will BOTH show "7").

In order to have the code work the way we want, we must use some crude multiplexing: turn the ten's place light on, disable the one's place, display the number desired in the tens place, disable the ten's place light, enable the one's place, and display the number desired in ones place. This must loop CONTINUOUSLY until the next 'second' is reached, and then the display will change accordingly. The reason the tic toc function in MatLab is so useful is because toc returns a time value without any calculation.

Brafil: my mistake. I'll edit that in a second. Could you elaborate on that more? Do note that "tic toc" is exclusively a MatLab function, and I'm looking for something as close to it as possible, for the sake of functionality.

Thanks again for the help, guys.. Any other opinions?

edit: for what it's worth, we're using Microsoft Visual Studio.
• 05-15-2010
rodrigorules
actually no, clock() does not start nor stop a clock .. it only tells you the time since you started your program

heres a program that prints hello every 1 second ( i think, ive actually never had much use for time.h , i might be wrong :) )

Code:

```#include <time.h> #include <stdio.h> main() {   clock_t last_check = clock() + CLOCKS_PER_SEC;   printf("%ld\n",CLOCKS_PER_SEC);   while( 1)   {       if( clock() < last_check  )           continue;       printf("HELLO!!"); //change display here       last_check = clock() + CLOCKS_PER_SEC;   } }```
edited code, still doesnt work lol