help - timer in C?

This is a discussion on help - timer in C? within the C Programming forums, part of the General Programming Boards category; Hi guys. Sorry if this is a regularly posted question, but here goes: We're currently working on a project for ...

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    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!
    Last edited by psu_ece; 05-15-2010 at 12:58 PM.

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    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;
    }
    Last edited by rodrigorules; 05-15-2010 at 12:32 PM.

  3. #3
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    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.

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    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.
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by jeffcobb View Post
    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."
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    2

    Smile

    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.

  7. #7
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    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
    Last edited by rodrigorules; 05-15-2010 at 01:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. hi-res vs. normal timer
    By MK27 in forum C Programming
    Replies: 0
    Last Post: 12-02-2009, 07:53 PM
  2. SIGALRM and timer
    By nkhambal in forum C Programming
    Replies: 1
    Last Post: 06-30-2008, 12:23 AM
  3. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  4. Need help with a count down timer
    By GUIPenguin in forum C# Programming
    Replies: 0
    Last Post: 07-07-2006, 04:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21