Skip to main content
Visitor II
January 26, 2024
Question

RTC Offset by milliseconds + and -

  • January 26, 2024
  • 2 replies
  • 1137 views

I am trying to offset the RTC by milliseconds. I have read through the documentation on this and this has lead me to RTC_SHIFTR. I have looked for examples how to set this up and what maths I need to use. However I am still unable to see the results I am expecting. Can someone please help me and tell me where I am going wrong.

 

void offsetMiliseconds(uint32_t miliseconds, bool add)
{
	 GetTime(&local_time);

	 sprintf(time,"Before: Time: %02d:%02d:%02d : %03d\r\n",local_time.tm_hour,local_time.tm_min,local_time.tm_sec, (((32766-sTime.SubSeconds)*1000)/32766));

	 WriteToSerialPort(time);

	 __HAL_RTC_WRITEPROTECTION_DISABLE(&hrtc);
	 
	 if(add)
		 hrtc.Instance->SHIFTR = 1 - miliseconds;
	 else
		 hrtc.Instance->SHIFTR = miliseconds;
	 
	 __HAL_RTC_WRITEPROTECTION_ENABLE(&hrtc);

	 GetTime(&local_time);

	 sprintf(time,"After : Time: %02d:%02d:%02d : %03d\r\n\r\n",local_time.tm_hour,local_time.tm_min,local_time.tm_sec, (((32766-sTime.SubSeconds)*1000)/32766));

	 WriteToSerialPort(time);
}

 

    This topic has been closed for replies.

    2 replies

    Super User
    January 26, 2024

    >tell me where I am going wrong

    The RTC has no millisecond register .

     

    AScha3_0-1706267992812.png

    Why you want some 1 ms offset on RTC ?

     

    If you want "tune" the rtc, use :

    AScha3_0-1706268420586.png

     

    Super User
    January 26, 2024

    > However I am still unable to see the results I am expecting.

    What results do you see?

    RTC does not work with "milliseconds", the "shift" feature works with the subseconds counter i.e. it "shifts" time by number of ticks (periods) of the asynchronous prescaler's output.

    After setting RTC_SHIFTR, you are supposed to check RTC_ISR.SHPF to find out, whether the "shift" has been accomplished. Also, you are supposed to check RTC_ISR.RSF before reading the RTC calendar registers.

    Note, that "adding" time involves setting RTC_SHIFTR.ADD1S bit, which advances time by  one second. If you want to advance by less than that, combine appropriately ADD1S and SUBFS fields.

    Also make sure, you have enabled backup domain access by setting PWR_CR1.DBP.

    Read RTC synchronization subchapter of RTC chapter in RM, and description of RTC_SHIFTR register at the end of the chapter.

    JW