cancel
Showing results for 
Search instead for 
Did you mean: 

Get more resolution from RTC clock

Posted on December 02, 2015 at 10:28

Hello there,

I was wondering either it is possible to count with microseconds resolution isntead of seconds resolution using RTC 32k clock? I am using HAL lib. In theRTC_TimeTypeDef time structure there are 2 members I dont quite understand:

/*!< Specifies the RTC_SSR RTC Sub Second register content.
This parameter corresponds to a time unit range between [0-1] Second
with [1 Sec / SecondFraction +1] granularity */
uint32_t SubSeconds; 
/*!< Specifies the range or granularity of Sub Second register content
corresponding to Synchronous pre-scaler factor value (PREDIV_S)
This parameter corresponds to a time unit range between [0-1] Second
with [1 Sec / SecondFraction +1] granularity.
This field will be used only by HAL_RTC_GetTime function */
uint32_t SecondFraction;

At the begining I thought I would be able to use them somehow to specify the time with more precision, but those values dont change. I would apreciate all help in this case.
14 REPLIES 14
Posted on December 03, 2015 at 08:17

But in this example HSI is used. When power is donw and only Vbat is left there is HSI as i recall. This wont work then. Is it possible to get more resolution from lsi 32k?

Posted on December 03, 2015 at 12:26

But in this example HSI is used...

Ok, HSE the STM32F4-DISCO doesn't have a LSE mounted, but it's illustrative, you're going to have to adapt things to your own needs.

The take away is that the SubSecond is readable, and demarks time over the synchronous prescaler range. The usual settings for the LSE would give you 1/256th of a second units (~4ms), although I suspect you could play with the prescalers to get 1/1024th.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on December 03, 2015 at 19:13

I see, ill try that out tommorow. Thank you.

Posted on December 04, 2015 at 11:32

I am sorry, but I cannot figure out how to get the mSec part from the post you have posted. Could you help out?

thomfischer
Senior
Posted on December 04, 2015 at 11:51

hrtc.Instance = RTC;

hrtc.Init.HourFormat = RTC_HOURFORMAT_24;

hrtc.Init.AsynchPrediv = 31; //127;

hrtc.Init.SynchPrediv = 1023; //255;

hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;

hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

HAL_RTC_Init(&hrtc);

HAL_RTC_GetTime(&hrtc0, &sTime, RTC_FORMAT_BIN);

HAL_RTC_GetDate(&hrtc0, &sDate, RTC_FORMAT_BIN);

msec = 999 - (sTime.SubSeconds * 999 / 1023);

Posted on December 04, 2015 at 13:24

msec = 999 - (sTime.SubSeconds * 999 / 1023);

Yeah, that doesn't look right, and wouldn't work on the SPL. SubSecond downcounts, in the range of the Synchronous Prescaler, ie 255..0 for a divide by 256 action

#define SYNCPREDIV 256

..

RTC_InitStructure.RTC_SynchPrediv = SYNCPREDIV - 1;

..

ms = (((SYNCPREDIV - 1) - SubSecond) * 1000) / SYNCPREDIV;

Where 32768 = 128 * 256 = (127 + 1) * (255 + 1)

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
thomfischer
Senior
Posted on December 04, 2015 at 20:00

Sorry,

I copied the the initialization values from the wrong file.

A #define for Constants is also  a better  style.

Both formulas should work correct , I had some problems with the original one, but don't know no more, maybe I made some silly mistake.

best regards

Posted on December 05, 2015 at 18:58

Thank you for answers, ill try this monday morning.