How to make the C time library use a physical Real Time Clock?

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

How to make the C time library use a physical Real Time Clock?

Jump to solution
3,920 Views
AndersJ
Contributor IV

Assuming i have a RTC physically connected the cpu,

how do I interface the RTC software wise,

to the C time functions, in time.h.

 

Thanks,

Anders J

Labels (1)
Tags (1)
0 Kudos
Reply
1 Solution
2,359 Views
bigmac
Specialist III

Hello Anders,

 

The time() function is required to return a 32-bit value corresponding to the elapsed seconds since 00:00:00 GMT on 01 January, 1970.  Some RTC devices are suitable to directly provide the time in this format.  Two such device are the DS2404 and the DS2417.  There are possibly other similar devices.

 

To use these devices, you would need to implement either a 1-wire serial protocol, or a 3-wire alternative method (presumably SPI compatible) for the DS2404.  The second device is 1-wire only.

 

Operationally, you might read the RTC register during initialization, and then use a 1 second hardware interrupt from the RTC device to periodically increment the count value.  The time() function can then simply return the current count value.

 

Setting the RTC to a specific date and time will involve more complex calculations.

 

Regards,

Mac

View solution in original post

0 Kudos
Reply
3 Replies
2,360 Views
bigmac
Specialist III

Hello Anders,

 

The time() function is required to return a 32-bit value corresponding to the elapsed seconds since 00:00:00 GMT on 01 January, 1970.  Some RTC devices are suitable to directly provide the time in this format.  Two such device are the DS2404 and the DS2417.  There are possibly other similar devices.

 

To use these devices, you would need to implement either a 1-wire serial protocol, or a 3-wire alternative method (presumably SPI compatible) for the DS2404.  The second device is 1-wire only.

 

Operationally, you might read the RTC register during initialization, and then use a 1 second hardware interrupt from the RTC device to periodically increment the count value.  The time() function can then simply return the current count value.

 

Setting the RTC to a specific date and time will involve more complex calculations.

 

Regards,

Mac

0 Kudos
Reply
2,359 Views
AndersJ
Contributor IV

Hi Mac,

 

Thanks for your reply.

 

From what you say, I understand that I need to write my own Time() to read the RTC.

But, how do I make sure my own Time() is used by the other

timekeeping functions built into C, such as clock, mktime, strftime etc?

 

I would prefer to write my RTC interface at the lowest possible level,

and then have all other built in time functions use it.

 

Br,

Anders J

 

 

 

 

 

0 Kudos
Reply
2,359 Views
bigmac
Specialist III

Hello Anders,

 

Within the time.h header file, it would appear that the function time() is the only one that is hardware dependent.

 

The functions ctime(), gmtime(), localtime() and strftime() are intended to use the time_t value returned by time(), as a parameter.  The function asctime()  would  use as a parameter, the  tm  structure returned by gmtime() or localtime().

 

The function mktime() returns a time_t value that is derived from a tm structure.  However, within the header file, there is no function prototype to set the hardware with the time_t value - a missing hardware dependent function. Usually there should be the function stime() to set the date and time.

 

Finally, the clock() function is not associated with the RTC device.

 

However, it would appear that this is "academic", since none of these functions seem to have been actually implemented within CW (at least for the HCS08 - I didn't check the HCS12). I might have reasonably expected all functions to be available, with the exception of time() and stime().

 

If none of the standard functions have been implemented, you will probably need to "roll your own".  It may be somewhat simpler to match the new functions more closely to the RTC device you are using.

 

Regards,

Mac

 

0 Kudos
Reply