AnsweredAssumed Answered

How to add an integer to RTC_TimeTypeDef structure

Question asked by xu.miao on Jan 20, 2013
Latest reply on Jan 21, 2015 by Clive One
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 :

01.uint8_t time_add(RTC_TimeTypeDef *val, uint8_t delta) {
02.    uint8_t carry = 0;
03. 
04.    // Seconds
05.    val->RTC_Seconds += delta;
06.    if (val->RTC_Seconds >= 60) { // seconds field overflow
07.        val->RTC_Seconds -= 60;
08.        carry = 1;
09.    } else {
10.        carry = 0;
11.    }
12. 
13.    val->RTC_Minutes += carry;
14. 
15.    return carry;
16.}

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 is defined 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;

Outcomes