stm32 rtc how to set timer alarm not time and date alarm - Page 1
Forum section: electronics / general

author: tariq
author_rank: Contributor
timestamp: May 04, 2021, 07:02:37 pm
content: hiI'm using Stm32f101vbt6 i want to set rtc alarm every 25 second.because i want to wake up microcontroller from stop mode to reset the watchdog. in HAL libraries i can set rtc alarm by this command:HAL_RTC_SetAlarm_IT(&hrtc, &RTC_Alarm_Time, RTC_FORMAT_BIN);but for this command i have to get time from rtc which is hour min and sec and calculate 25 second after and then write the command above. what should i do if i don't want to calculate time each time and set rtc alarm just by adding numbers to RTC alarm register.Thank you.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: May 04, 2021, 07:28:55 pm
content: The RTC is for hour, minute, second stuff.If you just want to have regular intervals, then just use a generic timer an program it to generate an ISR every 25s.

author: ajb
author_rank: Super Contributor
timestamp: May 04, 2021, 07:37:14 pm
content: The operation of the RTC is described in the Reference Manual, section 18. It looks to be pretty simple, just a 32-bit counter with a prescaler. So you'd set the prescaler to give you whatever base frequency you want from whatever the clock source is, so if you want seconds from a 32kHz clock then you should set the prescaler to 32768. That will increment the RTC counter once per second, so to set an alarm for 25 seconds from now, simply set the alarm register to the current counter value plus 25. Or if you want slightly more consistent intervals you can simply add 25 to the alarm register every time the alarm interrupt is triggered. Be sure to note the configuration flag and register synchronization flags, it looks like you will need to deal with them during initialization/alarm setting.Link to reference manual:https://www.st.com/resource/en/reference_manual/cd00171190-stm32f101xx-stm32f102xx-stm32f103xx-stm32f105xx-and-stm32f107xx-advanced-armbased-32bit-mcus-stmicroelectronics.pdf

author: tariq
author_rank: Contributor
timestamp: May 04, 2021, 07:55:47 pm
content: my rtc clock comes from LSI RC which is 40khz and 40000 in hex is 9C40 so i need write this value to RTC prescaler load register?and i have to read data from RTC counter register low (RTC_CNTL) and add 25 to it and then write it to RTC alarm register low (RTC_ALRL)?

author: ajb
author_rank: Super Contributor
timestamp: May 04, 2021, 08:06:45 pm
content: yes. Plus enable the RTC, enable the alarm interrupt, etc

author: tariq
author_rank: Contributor
timestamp: May 04, 2021, 08:36:21 pm
content: what the difference between RTC counter register low and RTC counter register high?is it because when RTC counter register low overflows rest of the counter comes at RTC counter register high?so i have to check RTC counter register low not overflows?

author: ajb
author_rank: Super Contributor
timestamp: May 04, 2021, 10:27:47 pm
content: The RTC counter register is really one 32-bit value, but it's shown as two 16-bit registers. I'm not sure why, maybe the RTC is reused from an older 16-bit architecture. You can probably just use a 32-bit access to read the entire counter value at once. It's possible the header files for the device even have just a single RTC_CNT register defined for this purpose, you'd have to check.You don't have to check anything for overflows, the RTC just fires the alarm interrupt when the alarm register matches the count register. It will wrap around when it hits 0xFFFFFFFF, so when you add 25 to get the next alarm time, just use 32-bit unsigned integers for the math and you'll always get correct behavior even when the counter wraps around

author: Doctorandus_P
author_rank: Super Contributor
timestamp: May 04, 2021, 11:22:18 pm
content: I just had a peek at the datasheet of a STM32F411 I have lying around...And it can both be used as Date + HH:mm:ss and for a periodic interrupt adjustable between 120us and 36 hours.So forget the HAL read the datasheet for further assistance. and start programming some registers. (Maybe there is another HAL function that can do this too)Code:DocID026289 Rev 725/149STM32F411xC STM32F411xEFunctional overview3.17 Real-time clock (RTC) and backup registersThe backup domain includes:•The real-time clock (RTC)•20 backup registersThe real-time clock (RTC) is an independent BCD timer/counter.Dedicated registers contain the second, minute, hour (in 12/24 hour),week day, date, month, year, in BCD (binary-coded decimal) format.Correction for 28, 29 (leap year), 30, and 31 day of the month areperformed automatically.The RTC features a reference clock detection, a more precise secondsource clock (50 or 60 Hz) can be used to enhance the calendar precision.The RTC provides a programmable alarm and programmable periodicinterrupts with wakeup from Stop and Standby modes. The sub-secondsvalue is also available in binary format.It is clocked by a 32.768 kHzexternal crystal, resonator or oscillator, the internal low-power RCoscillator or the high-speed external clock divided by 128. The internallow-speed RC has a typical frequency of 32 kHz. The RTC can becalibrated using an external 512 Hz output to compensate for anynatural quartz deviation. Two alarm registers are used to generatean alarm at a specific time and calendar fields can be independentlymasked for alarm comparison. To generate a periodic interrupt, a16-bit programmable binary auto-reload downcounter withprogrammable resolution is available and allows automaticwakeup and periodic alarms from every 120 μs to every 36hours.