cancel
Showing results for 
Search instead for 
Did you mean: 

How to add an integer to RTC_TimeTypeDef structure

xumiao_1
Associate
Posted on January 20, 2013 at 19:48

Dear all, is there anyone knows how to add an integer to RTC_TimeTypeDef structure?

For example, I have an RTC_TimeTypeDef variable 'timestamp'. After using the timestamp, I want to increase it by 45 seconds to get a new timestamp. Right now I am doing it with a self-defined function :


uint8_t time_add(RTC_TimeTypeDef *val, uint8_t delta) {

uint8_t carry = 0;


// Seconds

val->RTC_Seconds += delta;

if
(val->RTC_Seconds >= 60) { 
// seconds field overflow

val->RTC_Seconds -= 60;

carry = 1;

} 
else
{

carry = 0;

}


val->RTC_Minutes += carry;


return
carry;

} 

I am wondering is there any better way to add an integer (say 45 seconds) to the RTC_TimeTypeDef structure? PS: I am using STM32L151 and RTC_TimeTypeDef isdefined in stm32l1xx_rtc.h as follows:

/** 
* @brief RTC Time structure definition 
*/
typedef
struct
{
uint8_t RTC_Hours; 
/*!< Specifies the RTC Time Hour.
This parameter must be set to a value in the 0-12 range
if the RTC_HourFormat_12 is selected or 0-23 range if
the RTC_HourFormat_24 is selected. */
uint8_t RTC_Minutes; 
/*!< Specifies the RTC Time Minutes.
This parameter must be set to a value in the 0-59 range. */
uint8_t RTC_Seconds; 
/*!< Specifies the RTC Time Seconds.
This parameter must be set to a value in the 0-59 range. */
uint8_t RTC_H12; 
/*!< Specifies the RTC AM/PM Time.
This parameter can be a value of @ref RTC_AM_PM_Definitions */
}RTC_TimeTypeDef; 

3 REPLIES 3
Posted on January 21, 2013 at 18:06

There is no magic solution here. You have to check for the overflow of seconds, increment the minutes, and if that overflows the hours, days, etc.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..
nathanael
Associate
Posted on January 21, 2015 at 19:09

Does this mean that there is NO WAY to get a timestamp with usual libraries on STM32 ?

Do I have to implement the usual time_t time(time_t *) function by myself, based on HAL_RTC_GetTime() and HAL_RTC_GetDate() ?

Posted on January 21, 2015 at 20:04

Well if you were using an F1 part you'd have a 32-bit second count instead of a calendaring RTC. ST provides all the source code so it should be pretty transparent what is and what is not happening.

If you want to use time() and it's associates, then yes you'll have to provide the appropriate hosting, as you would with any embedded platform.

Tips, buy me a coffee, or three.. PayPal Venmo Up vote any posts that you find helpful, it shows what's working..