The RTC of F4. The RTC of the F1 series is a simple counter. To obtain the time, you have to calculate it by yourself. F4.
[Main features]
The RTC of ipvf4 is an independent BCD/clock Counter. RTC provides a time calendar and two-way alarm clock interrupt, a periodic programmable ring sign with Interrupt Capability. He is also a wake-up unit that manages low-power consumption mode in RTC.
The seconds, minutes, hours, dates, months, and years of RTC are stored in a 32-bit register in BCD format.
The number of days of each month is automatically processed, and the daytime power saving function is also available. Programmable alarm information is stored in another register.
Digital calibration function, which can be used to handle the deviation of Crystal Oscillator
During power-on, the RTC register is protected to prevent accidental Data Writing.
Whether the MCU is in that mode, RTC will not stop as long as the power supply voltage is within the operational range.
RTC clock]
The RTC clock can use LSE, LSI, and HSE of no greater than 1 m as the clock. The 1Hz clock can be obtained by dividing the clock into 1Hz for RTC.
The discovery board in this experiment does not have LSE, and the external crystal oscillator is 8 m, and cannot be used. Only the LSI clock can be used for RTC experiments.
This experiment should first enable the LSI clock. Set the LSI clock to the RTC clock. The operation is set through RCC. This register is not configured in the RTC part:
Rcc_lsicmd (enable );
While (rcc_getflagstatus (rcc_flag_lsirdy) = reset );
Rcc_rtcclkconfig (rcc_rtcclksource_lsi );
Rcc_rtcclkcmd (enable );
Rtc_waitforsynchro ();
[RTC initialization]
RTC register write protection during power-on. To enable the write operation, you need to write a sequence to the rtc_wpr register. Write 0xca first and then 0x53 to disable write protection. Writing other data will disable write protection, such as writing 0x00 data.
Calendar initialization and Configuration
Follow these steps to set the time and date of the RTC and the pre-division of the clock input:
1. Set init in the initialization and Status Register rtc_isr to 1 to enter the initialization mode. In this mode, the RTC time register count is stopped and can be updated.
2. Determine the initf bit in rtc_isr and whether the RTC has been initialized. When the read data is 1, you can enter the initialization mode. This process will last for about two RTC clock cycles
3. To get a 1Hz counter clock, first set the synchronization frequency value prediv_s [] in the rtc_prer register, and then set a part of the frequency value prediv_a [] in the register. Even if only one division value needs to be written, the other Division value needs to be written again.
4. Write the time and date to the Shadow registers of rtc_cr and rtc_dr, and set the time format in rtc_cr.
5. Write 0 to the Inti bit and exit the initialization mode. This means that rtc_dr and rtc_cr will load the date information from the shadow register to the register and run automatically after four RTC clock cycles.
ImplementationCodeAs follows:
Void rtc_config (void)
{
Rcc_apb1periphclockcmd (rcc_apb1periph_pwr, enable );
Pwr_backupaccesscmd (enable); // enables register backup operations
Rcc_lsicmd (enable );
While (rcc_getflagstatus (rcc_flag_lsirdy) = reset );
Rcc_rtcclkconfig (rcc_rtcclksource_lsi );
Rcc_rtcclkcmd (enable );
Rtc_waitforsynchro ();
If (rtc_readbackupregister (rtc_bkp_dr0 )! = 0x9527) // a variable. Check whether RTC Initialization is successful.
{
Rtc_writeprotectioncmd (disable );
Rtc_enterinitmode ();
Rtc_initstructure.rtc_hourformat = rtc_hourformat_24;
Rtc_initstructure.rtc_asynchprediv = 0x7d-1;
Rtc_initstructure.rtc_synchprediv = 0xff-1;
Rtc_init (& rtc_initstructure );
Rtc_timestructure.rtc_seconds = 0x00;
Rtc_timestructure.rtc_minutes = 0x01;
Rtc_timestructure.rtc_hours = 0x01;
Rtc_timestructure.rtc_h12 = rtc_h12_am;
Rtc_settime (rtc_format_bcd, & rtc_timestructure );
Rtc_datestructure.rtc_date = 30;
Rtc_datestructure.rtc_month = 5;
Rtc_datestructure.rtc_weekday = rtc_weekday_thursday;
Rtc_datestructure.rtc_year = 12;
Rtc_setdate (rtc_format_bcd, & rtc_datestructure );
Rtc_exitinitmode ();
Rtc_writebackupregister (rtc_bkp_dr0, 0x9527 );
Rtc_writeprotectioncmd (enable );
Rtc_writebackupregister (rtc_bkp_dr0, 0x9527); // The Initialization is complete and the flag is set.
}
Pwr_backupaccesscmd (disable );
}
Compile and run. Use rtc_gettime to read the time in the main function. We can see that the time is changing.
In the future, you only need to use rtc_gettime to read the time.
To set the time, you only need to use rtc_settime. The rtc_gettime function will enable write protection.