cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F042 RTC ACCURACY

engenharia2
Associate II
Posted on January 28, 2016 at 12:42

to be migrated, sourceId: 62564:697285D7-A9CA-445D-B16C-F23BF0E3B1A3

#lsi-rc #stm32f0 #rtc #stm32f042
7 REPLIES 7
Posted on January 28, 2016 at 13:20

> I need to use the RTC in STM32F042 mcu and I'm using the internal oscillator LSI RC. I did a test and found that the RTC is not running with the precise clock. In 30 minutes there was an error of about 5 minutes. This is the most suitable osclilador for RTC?

No. RC oscillators are inherently imprecise. There is a reason people use crystals in oscillators. Read the LSI section of datasheet.

> Is there a way to calibrate it?

Sort of, but if you need to ask, you probably don't want to do it.

> I should use the HSE_RTC clock for more accuracy?

Yes. Or use a package where the OSC32 pins are present, and use LSE with appropriate 32.768kHz crystal.

JW

Posted on January 28, 2016 at 14:16

If the clock is NOT actually 40 KHz then setting the Prediv to be 40000 is going to result in obvious walk-off. Step one would be using divisors that match the source.

The LSI has a pretty wide range, and is not inherently very stable.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
engenharia2
Associate II
Posted on February 01, 2016 at 20:39

Okay clive1, I use HSE_RTC clock and accuracy improved significantly. But I'll have to use the LSI because I found that the HSE is turned off in STOP_MODE and unfortunately I can not use another package with OSC32 pins. I appreciate if you can give me any tips on how to calibrate/improve LSI clock. Something like check the real clock and reconfigure the pre divider would be valid? Thank you!

Posted on February 01, 2016 at 21:01

You'd need to bench LSI against a crystal source clocking TIM14. I'm looking at RM0360, not sure that's exactly the one for your part, but it's not a part I'm using.

You should be able to measure 1,2,4 or 8 periods of LSI against a lot faster clock, while remaining within 16-bits. This would give you a baseline for your board/voltage, and apt to be a lot closer to reality than using ''40000''. You'd take your measured frequency, and then factor that into the prediv settings.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
engenharia2
Associate II
Posted on March 29, 2016 at 22:40

Hi, I'm here again! Now I put an external 16MHz oscillator. I measured the frequency of the crystal with the oscilloscope and seems to be all right. But now I have an error of 12 minutes to every 24 hours. My application is totally dependent on the correct RTC and should work for 45 uninterrupted days.So I wonder if this error is normal for an external crystal or there may be something wrong with my settings.

/* RTC init function */
void MX_RTC_Init(void)
{
RTC_TimeTypeDef sTime;
RTC_DateTypeDef sDate;
RTC_AlarmTypeDef sAlarm;
/**Initialize RTC and set the Time and Date 
*/
hrtc.Instance = RTC;
hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
hrtc.Init.AsynchPrediv = 125;
hrtc.Init.SynchPrediv = 4000;
hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
HAL_RTC_Init(&hrtc);
/*
sTime.Hours = 0;
sTime.Minutes = 0;
sTime.Seconds = 0;
sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sTime.StoreOperation = RTC_STOREOPERATION_RESET;
HAL_RTC_SetTime(&hrtc, &sTime, FORMAT_BIN);
sDate.WeekDay = RTC_WEEKDAY_MONDAY;
sDate.Month = RTC_MONTH_JANUARY;
sDate.Date = 1;
sDate.Year = 0;
HAL_RTC_SetDate(&hrtc, &sDate, FORMAT_BIN);
//Enable the Alarm A 
sAlarm.AlarmTime.Hours = 0;
sAlarm.AlarmTime.Minutes = 0;
sAlarm.AlarmTime.Seconds = 0;
sAlarm.AlarmTime.SubSeconds = 0;
sAlarm.AlarmTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
sAlarm.AlarmTime.StoreOperation = RTC_STOREOPERATION_RESET;
sAlarm.AlarmMask = RTC_ALARMMASK_NONE;
sAlarm.AlarmSubSecondMask = RTC_ALARMSUBSECONDMASK_ALL;
sAlarm.AlarmDateWeekDaySel = RTC_ALARMDATEWEEKDAYSEL_DATE;
sAlarm.AlarmDateWeekDay = 1;
sAlarm.Alarm = RTC_ALARM_A;
HAL_RTC_SetAlarm(&hrtc, &sAlarm, FORMAT_BIN);
*/
}

0690X00000604XjQAI.png
Posted on March 30, 2016 at 11:43

hrtc.Init.AsynchPrediv = 125;
hrtc.Init.SynchPrediv = 4000;

 Are you sure?
 
 Consult the RTC prescaler register chapter in RM, and Cube's manual's RTC section.
 
 JW
 
 

engenharia2
Associate II
Posted on March 30, 2016 at 15:08

 Thank you JW, it's my fault. The dividers starts from 0. Probably this is the problem isn't?Now I will test with this setting:   hrtc.Init.AsynchPrediv = 124;

hrtc.Init.SynchPrediv = 3999;  Unfortunately I cannot configure the asynchronous prescaler to a high value because it will generate a small error.  For reference below the RM0091p573/1008 note:  0690X00000605KHQAY.png