Embedded real time clock RTC .

Mondo Technology Updated on 2024-02-01

1. Getting to know RTC for the first time

1.Introduction

The real-time clock is oneStand-alone timer。The RTC module has a set of counters that count continuously, and with the corresponding software configuration, it is possibleProvides the function of a clock calendar, the counting frequency is usually seconds. Modify the value of the counter to reset the current time and date of the system.

2.Features:

1) 32-bit programmable counter for longer period of measurement.

2) It can run after the MCU is powered off.

3) Low power consumption.

3.Fallback register and RTC register characteristics

Partial register write protection: RTC PRL, RTC ALR, RTC CNT, and RTC div registers are not reset by the system.

Data storage capabilities: RTC and backing registersIt will not be reset by the system or power reset source;When waking up from standby mode, it is also not reset. Fallback registers can be used to save data in the event of a power failure

Work independently: RTC and backing registersPowered by a switchWhen VDD is active, the switch selects VDD power supply, otherwise byVBAT pin powered. It can continue to work while the VBAT is powered

2 independent resets: The APB1 interface is reset by the system; RTC cores can only be reset by the fallback domain;

2. RTC composition

1.Related registers

(1) Control register high (RTC CRH).

Bit 2 (owie): Allows overflow break bits.

Bit 1 (alrie): Allows the alarm to be interrupted.

Bit 0 (secie): Seconds interrupt is allowed.

Bit 5 (RTOFF): RTC operation is off. (Indicates the status of the last operation performed on its register.)That is, it indicates whether the operation is complete or not

Bit 4 (CNF): Configure the flag. (Only when this bit.)After being set to '1' and re-cleared by the software'0', thus allowing data to be written to the RTC CNT, RTC ALR, or RTC PRL registers).

(3) Pre-crossover loading register high (RTC PRLH).

Bit 28 (pwren): The power interface clock is enabled.

Bit 27 (BKPEN): The backup interface clock is enabled.

Bit 15 (RTCEN): RTC clock enabled.

Bit 9:8 (RTCSEL): RTC clock source selection.

Bit 0 (seon): External low-speed oscillator enabled.

2.RTC structure

RTC consists of two main components. Part 1 (APB1 interface).Used toIt is connected to the APB1 busThe other part (RTC cores).It consists of a set of programmable counters, divided into two main modules. The first module is:RTC's pre-crossover module(Programmically generates an RTC time reference TR CLK of up to 1 second). The second module is one32-bit programmable counter, can beInitialize to the current system time

Added: 1) The RTC can choose from three RTC clock sources:

2) The APB1 unit of the RTC also contains a set16-bit registers, it can be read and written through APB1 and the bus.

3) The APB1 interface is made up ofAPB1 bus clockDriver, which is used to interface with the APB1 bus.

4) RTCPre-crossover moduleWhat's included is one20-bit programmable crossover(RTC prescaler), the highest crossover factor is2 to the 20th power

5) RTC has 3 specializedInterrupts can be masked

6) If the corresponding allow bits are set in the RTC CR register, in eachtr clk cycleRTC produces an interrupt (seconds of interruption

7) System time presstr clk cycleAdding up and comparing the programmable time stored in the RTC ALR register, if the corresponding permissible bits are set in the RTC CR control register, an interrupt ( will be generated when comparing matches).The alarm is interrupted

1) Enable access to RTC: Enable PWR and BKP clocks (RCC APB1ENR) and enable access to fallback registers and RTC (PWR CR).

2) Set RTC Clock Source: Activate LSE and set the counting clock source of RTC to LSE. (rcc_bdcr)

3) Enter the configuration mode: wait for the rtoff bit to be 1, and set the cnf bit to 1. (rtc_crl)

4) Set RTC registers: set the frequency division value, counting value, etc. (RTC PRL), generally only set the frequency division value, and the CNT setting is independent.

5) Exit the configuration mode: Clear the CNF bit and wait for the RTOFF bit to be 1 (RTC CRL) to complete the configuration.

1) Query the rtoff bit until the value of rtoff changes to 1.

2) Set the CNF value to 1 to enter the configuration mode.

3) Write to one or more RTC registers.

4) Clear the CNF flag and exit the configuration mode.

5) Query rtoff until the rtoff bit changes to '1' to confirm that the write operation has been completed.

1.Enable the power clock and enable backup domain access: HAL RCC PWR CLK Enable, HAL RCC BKP Clk Enable, and HAL PWR EnableBkupAccess

2.Enable LSE Select RTC Clock Source Enable RTC clock: HAL RCC OSCconfig (LSE enabled), HAL RCCEX PERIPHCLKCONFIG (select RTC clock source), HAL RCC RTC Enable (RTC clock enabled).

3.Initialize the RTC, set the crossover value and working parameters: HAL RTC INIT (initialize RTC) and HAL RTC MSnSpinit (complete the RTC bottom initialization).

4.Set the date and time of the RTC.

5.Get the current date and time of the RTC.

That's all for this time

Related Pages