For the MCU to arm of the students, the RTC may be less contact. Referring to the real-time clock, more often think of is DS1302. Of course, in the STM32, a CPU is enough, do not need to DS1302.
In fact, RTC is just a timer, all the information is lost after power-down, so we need to find a place to store this information, so we find the backup register. Because it can still be powered by a button battery after power-down, this data can be saved at all times. We will detail the RTC principles and routines in this tutorial to guide you into the world of RTC.
1.stm32 's RTC Module
The RTC module has a real-time clock function because it maintains a separate timer internally, which can be configured to interrupt exactly once per second. Here is a look at the composition of the following.
The composition of the 1.1RTC
The RTC consists of two parts: the APB1 interface and the RTC core (it feels like it's not said, because any module will have an interface part and its own core part.) Please note that this is what the authoritative STM32 series of manuals say?). I guess the reason may be that STM32 all peripheral default clock is invalid, use a peripheral, then turn on the clock, in this way to reduce power consumption. The RTC,APB1 interface here is driven by the APB1 bus clock. To highlight the clock, right? However, it is said that the APB1 interface section also includes a set of 16-bit registers.
The RTC core is divided into a prescaler module and a 32-bit programmable counter. The former causes the RTC to have a second interrupt per TR_CLK cycle, which can be initialized to the current system time. The system time is then accumulated according to the TR_CLK cycle, which realizes the clock function.
1.2 Operation of the RTC
Our access to RTC is done through the APB1 interface. Note that the first value of the RTC register read from APB1 is likely to be corrupted (usually read to 0) when the APB1 has just been turned on (for example, just after power-up, or just after a reset). This unfortunate, how is STM32 prevented? In the program, we will wait for the RSF bit in the RTC_CRL register (register synchronization flag) to be hardware 1, and then start the read operation, this time read out the value is OK.
Will there be a similar situation with the RTC Register's write operation? For write operations, we just need to be aware that each write operation must be done after the previous write operation. This "Make sure",
is to determine if the RTC register is in the update by querying the Rtoff status bit in the RTC_CR register. The RTC register can only be written if the Rtoff status bit is 1.
Programming of 2.RTC
The RTC routine, which mainly sets the RTC clock, makes it display the current clock on the HyperTerminal. The display of this clock is "keep going". And after power-down, the clock is still walking and the current time is still displayed. Of course, if you're interested, you can make it appear on the LCD-it's a veritable electronic clock.
When programming, pay attention to the backup register BKP_DR1, which does a key thing: to determine if the RTC has been set. Because RTC differs from other timers, it is powered separately by a coin cell battery, so it does not have to be reset every time the power-up or reset is done. To determine if the RTC has been set, you can decide whether you need to set up RTC at this moment. If you just installed the battery, the first power up, naturally need to go to set. Otherwise, we just have to let it show the current clock.
When using RTC for the first time, you can refer to the manual. Here is a summary of the work that needs to be done for the first configuration:
1. Turn on power management and backup register clock. Note that you must open the clock for the backup register. It is precisely by writing fixed data in the backup register to determine whether the chip is a practical RTC for the first time, thus prompting the configuration of the clock when the system is running RTC.
2. Enable RTC and Backup register access (reset is off by default to prevent possible accidental write operations.) )。
3, select the external low-speed crystal for the RTC clock, and enable the clock. When I first debugged the RTC, I made a low-level error: Because it is not defined as follows: #defineRTCClockSource_LSE
Cause the program to stay here:/*waittilllseisready*/
while (Rcc_getflagstatus (Rcc_flag_lserdy) ==reset)
{
}
I hope you can avoid this error after reading this tutorial.
4, enable the second interrupt, Cheng in the second interrupt in the set bit flag to inform the main program display time data, while the 32-bit counter to 23:59:59 when the clear 0;
5, set the RTC Prescaler value to generate 1 seconds signal Calculation Formula ftr_clk=frtcclk/(prl+1), we set the 32767来 to generate a second signal.
Again, we emphasize that all the read and write operations are done before the RTC register operation, that is, whether there are read and write operations inside.
Let's look at the code:/*systemclocksconfiguration*/rcc_configuration ();
/*nvicconfiguration*/
Nvic_configuration ();/*configurethegpios*/gpio_configuration ();
/*configuretheusart1*/
Usart_configuration ();
The above four function calls, although the most common, but still want to attract people's attention. In particular interrupt nvic_configuration (); and usart_configuration (); I hope you will look into the specific function implementation.
Related to this tutorial the first point is the clock, in order to avoid omission, I put its code first: Rcc_apb1periphclockcmd (rcc_apb1periph_pwr| rcc_apb1periph_pwr,enable);
We then read the value in the backup register BKP_DR1 to determine if it was the first power-up, and if not, to display the clock directly, otherwise make the time setting. When the value of BKP_DR1 is not 0xAAAA, the first power-up is required and the RTC needs to be initialized at this time. Note the initialization of the implementation function rtc_configuration (); Why write, please refer to the "First time using RTC Configuration Summary" Given earlier, then the clock setting. Note that because we need to write, the firmware Library manual calls Rtc_waitforlasttask () and waits for the flag bit rtoff to be set so that it can be done after the previous write operation. Call Rtc_setcounter (Time_regulate ()), and write the count value to the RTC counter.
Because the Bkp_writebackupregister () function is followed by the BKP_DR1 write operation, a rtc_waitforlasttask () is required before the time is set to complete. The rest of the code, relatively simple, mainly note the following:
Rtccount=rtc_getcounter ();//Gets the count value and calculates the current clock/*computehours*/
thh=rtccount/3600;/*computeminutes*/
Tmm= (rtccount%3600)/60;/*computeseconds*/
Tss= (rtccount%3600)%60;
This is done by Rtc_getcounter (), the function gets the count value, and then the count value is expressed in hours, minutes, seconds, respectively. Finally, you need to call the printf function to display it.
The above is the whole RTC process, look forward to everyone shooting bricks. If you need to make bricks, please go directly to the racket to promote the STM32 further improvement tutorial, thank you!
STM32 Learning Note 10 (Real-time clock RTC)