cancel
Showing results for 
Search instead for 
Did you mean: 

RTC Time Functions

richardh
Associate II
Posted on January 17, 2012 at 17:27

Hi

I need to work out the difference in days between 2 dates, what is the simplest way to do this?  the only way I can think is work out if its a leap year or not, and put in the days per month etc. but this seems like re-inventing the wheel.

Having looked at how PC compilers do this, most functions I've seen work in the seconds since a specific date/time manner, so its easy to perform calculations like this, but the STM32F103 RTC seems to use BCD instead, so I can't think of a way round this apart from hard coding.

#rtcc-time-date
14 REPLIES 14
trevor23
Associate III
Posted on January 18, 2012 at 00:20

In fact the the STM32 RTC is much more suited to the seconds method. The BCD method might have been used in an example you have seen but that is software related and not STM32 hardware related. IMHO the BCD method is lame. Let the RTC count seconds from 1-Jan-1970 00:00:00 (epoch). Convert dates to seconds using standard epoch C functions (time.h) and then counting the days (or any other comparisons) between dates is just simple maths.

Posted on January 19, 2012 at 22:33

The RTC on the F103 is a 32-bit counter, typically counting off seconds. You can slew it faster or slower with the prescaler. At 32-bit the second rollover would occur in ~136 years. The biggest mistake people usually make is to try and make it rollover at 24 hour periods.

Pick an epoch date you are comfortable with, the UNIX one works, but a 2000, or 2010 start time would be equally valid.

The leap day computation is much more efficient if you limit yourself to 1901 to 2099. If you need to constantly convert seconds to D:M:Y H:M:S, then yes you'll need to count through the years, and then days, but you can cache the results, and step through multiple 4 year periods.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
richardh
Associate II
Posted on January 30, 2012 at 14:31

thanks, that all makes sense!

Any idea of a sensible time.h and associated files I can download? the ones I have found are low on documentation (and I have to hack it to feed in my own counter value I guess) which is OK if you are sure what you are doing, I'm not 🙂

trevor23
Associate III
Posted on January 30, 2012 at 15:44

What complier tools are you using. This sort of thing is usually already there in the tools.

richardh
Associate II
Posted on January 30, 2012 at 15:57

I'm using Keil, and I think I have found it.... thanks

Do I simply re-write the time function? and everything else works...

Where do I set the epoch?

Sorry if this is all very obvious...

trevor23
Associate III
Posted on January 30, 2012 at 16:11

It's only obvious when you've done it 🙂 Yes, use your own get time function or implement a new ''time'' function that simply gets the tick count from the STM32 RTC counter (add a mutex if you are using an RTOS). The epoch is 1/1/1970 00:00:00 and I'm not sure how'd you'd go about using something else without implementing your own time fuctions (I've never used anything else to date).

richardh
Associate II
Posted on January 30, 2012 at 16:27

thanks again 🙂  as you say, everything is obvious once you've done it, its just getting there in the first place!

I can honestly say the whole date thing is so confusing, far to many fudge factors for my liking, I hunted high and low for a simple demo, the Keil one is a classic, turns an LED on and off at 1Hz, does nothing else, like actually demo time/date routines.  More than that, I found nothing.  I will probably get a list of them posted on here now lol

ColdWeather
Senior
Posted on January 31, 2012 at 14:14

You can easily find the difference between two dates if you convert the given dates into the 32-bit values that represent seconds and compute then (Value2 - Value2)/(60*60*24).

I attached my hardware independent calender support using some KEIL RTL time definition/convertion functions. Post here, should you have any questions.

________________

Attachments :

HAL_Calender.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006I112&d=%2Fa%2F0X0000000bgz%2F7rZohE.St6bAict8etnVZEkF9.1hDXHYpqa4iSaqRYY&asPdf=false
richardh
Associate II
Posted on February 17, 2012 at 12:34

thanks, your calender support routines are pretty comprehensive!  

One thing that I am unsure of, surely when I set the time, I have to set the date at the same time?  so why do you have individual SetDate and SetTime routines? as these just generate a value to write to the hardware counter, and one will overwrite the other surely?