How C's time() is implemented in embedded systems?

Discussion in 'Embedded Systems and Microcontrollers' started by dor, Jan 22, 2010.

  1. dor

    Thread Starter Active Member

    Feb 20, 2009
    62
    0
    How C's time() is implemented in embedded systems, with a built-in uC RTT register, external RTC component, or... ?
    Same question for other time.h library functions.
    Thank you.
     
  2. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    You will find that most versions of C for embedded systems do not implement the standard C libraries. Instead each vendor of C for embedded systems supplies their own set of libraries tailored to functions useful for programming the microcontrollers. Unfortunately there is no standard for embedded system libraries and every vendor's version is different make porting between compilers a real pain.
     
  3. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    In a microcontroller system this must be implemented as an external part. Not as a core CPU function. So the code for this part must be rewritten for every system implementing it. Hence it It can not be part of a standard library
     
  4. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    That's just wrong. There are popular real time clock peripherals, but in the vast majority of cases microcontrollers can generate their own real time clock functions using one of their internal timers and a TINY bit of their overall processing time.

    dor- Have a look at this web page it shows some very simple systems for generating a 1-second event in C using a microcontroller;
    http://www.romanblack.com/one_sec.htm

    Specifically this example;
    Code ( (Unknown Language)):
    1.  
    2.     // C code for a 1 second period with a 1MHz timer (4MHz xtal);
    3.     // uses 1 variable; unsigned long bres
    4.     // gets here every TMR0 int (every 256 ticks)
    5.  
    6.     bres += 256;    // add 256 ticks to bresenham total
    7.  
    8.     if(bres >= 1000000) // if reached 1 second!
    9.     {
    10.         bres -= 1000000;    // subtract 1 second, retain error
    11.         do_1sec_event();    // update clock, etc
    12.     }
    13.  

    Once your microcontroller has a reliable one second clock it is fairly trivial to count 60 seconds and inc a minute etc, that only takes a afew lines of code;
    Code ( (Unknown Language)):
    1.  
    2.       secs++;
    3.       if(secs >= 60)
    4.       {
    5.         secs = 0;
    6.         mins++;
    7.       }
    8.       if(mins >= 60)
    9.       {
    10.         mins = 0;
    11.         hours++;
    12.       }
    13.       if(hours > 12) hours = 1;
    14.  
     
    Last edited: Jan 22, 2010
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    In the example of yours, you have implemented a clock function, using internal timer functions. So I would say it is still implemented. But then power goes off you will loose track of time. So it will not bee real time. A real time clock is a clock that keeps track of the time even when the system is turned off.
    The time() function returns the number of seconds that have elapsed since January 1, 1970, as an integer. In the HI-TECH C compiler, they have included much of the C time formatting functions, if not all. But not the time(). It is up to the user to implement this function. Either by allocating internal timer(s) for this purpose, and create a pseudo variant, or use an external circuit. ​
     
  6. frankv

    New Member

    Jan 23, 2010
    5
    0
    The question did relate directly to the time() function in C, so the timed interrupt doesn't even need to count 60 seconds = 1 minute, etc. It just needs to increment a 32-bit integer.

    The time() function then
    * temporarily disables the interrupt that updates the integer (if the micro doesn't support atomic 32-bit operations)
    * retrieves the value to CPU registers
    * re-enables the interrupt
    * returns the value

    You would also need to implement a function to set the 32-bit integer to the correct value. This would need to be called at startup.
     
  7. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Sorry to06afre, I didn't meant to sound picky at you.

    The OP originally asked about time() in regards to whether the micro had a built in time register and/or external real time clock. That obviously includes the option of having time() implemented totally within the microcontroller.

    I made an assumption (and probably a fair assumption) that he wants clock features and provided a simple proven solution.

    Now if the OP ever comes back with more info ;) we can probably give him better help.
     
  8. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    No offence taken :).
     
  9. dor

    Thread Starter Active Member

    Feb 20, 2009
    62
    0
    My uC has an RTT built-in which increments it's 32bit register each second.
    But I wanted to see what's the most common approach of applying a time() feature.

    Thank you guys :)
     
  10. delmasli

    New Member

    Sep 17, 2009
    17
    0
    If you are looking for exact time you need to install external device such as DS1307 RTC. If the connection of RTC is correct, when your MCU's power (even though all system power) is switched off, RTC remains counting. You can get correct data whenever you want. But as it is explained above, you need to write your own code to drive RTC.
     
  11. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    Most of the newer microcontrollers have a lot of power saving features, generally you don't turn them off but instead just turn off some features as needed. If they don't have a dedicated low power osc for clock use (most do) then the main oscillators use much less power than they did on micros 5 or 10 years ago so you just run the main osc and turn off power hungry peripherals.

    The whole idea of a micro plus a dedicated external clock IC with it's own battery is getting a little dated, like the idea of a hard on/off power switch.

    You definitely don't *need* an external RTC to have clock capability on a micro.
     
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Yes that is correct. But it still has to be implemented. And people who are making the C compiler do not know which method the designer select so they can not build this functionality into the compiler.
    I will also say it must be up to the designer to implement a realtime clock outside the micro if needed for some reason. If the micro loose power it will loose information stored(not in EEPROM). But then again it is nice to have and need to have. It is cool to have alarm clock that keep track of time even if it loos mains power for a year, but do we really need it. I would say no
     
Loading...