Stm32 RTC Real-Time Clock

Source: Internet
Author: User
Tags min

STM32 's real-time clock is a standalone timer

Typically, a coin cell is added to the supply side of the backup area, and the RTC does not stop working when the mains power is not powered

RTC can trigger a second interrupt, overflow interrupt, and alarm interrupt if the VDD power supply is active

Backup Register BKP
The backup registers are 42 16-bit registers, they are in the backup domain, and when the VDD power is cut off, they are still powered by the Vbat. They will not be reset when the system is woken up in standby mode or when the system is reset or the power is reset

RTC Configuration Steps

1. Enable power supply clock and backup area clock
2. Remove write protection from the backing area, DBP
3. Reset the Backup area
4. External low-speed oscillator enable, LSE
5.RTC clock Source Selection, LSE
6.RTC Clock Enable

7. Enter configuration mode
8. Setting the RTC Prescaler load value
9. Setting the RTC counter value
10. Exit Configuration Mode

11. Initialize the Nvic peripherals and set the RTC interrupt priority
12. Write the RTC interrupt handler function

Example

typedef struct {U32 hour;
    U32 min;
U32 sec;
}time_t;

time_t G_time;

    void Rtc_get () {u32 counter; Counter = Rtc_getcounter ();
    Read counter value G_time.hour = counter/3600;
    G_time.min = (counter% 3600)/60;
G_time.sec = counter% 60;
        } void Rtc_irqhandler () {if (Rtc_getflagstatus (rtc_flag_sec) = = SET) {rtc_get ();
    printf ("Time%02d:%02d:%02d\n", G_time.hour, G_time.min, g_time.sec);
} rtc_clearflag (RTC_FLAG_SEC);
    } void Rtc_init () {u16 bkp_data = 0xa5a5;
    U16 Prescaler = 32768-1;

    Nvic_inittypedef NVIC = {rtc_irqn, 2, 2, ENABLE}; Rcc_apb1periphclockcmd (RCC_APB1PERIPH_PWR | RCC_APB1PERIPH_BKP, ENABLE); Enable power supply clock and backup area clock pwr_backupaccesscmd (enable); Cancel write protection for the backing area if (Bkp_readbackupregister (BKP_DR1)! = Bkp_data)//Read backup Data {bkp_deinit ();//reset Backup Area RC C_lseconfig (rcc_lse_on); External low-speed oscillator enable while (Rcc_getflagstatus (rcc_flag_lserdy)! = SET)
        {Delay_ms (10); } rcc_rtcclkconfig (Rcc_rtcclksource_lse); The LSE oscillator acts as the RTC Clock Rcc_rtcclkcmd (ENABLE); RTC Clock Open rtc_waitforlasttask (); RTC Operation closed (last write to RTC Register completed) Rtc_waitforsynchro (); Register Sync Flag (register already synchronized) Rtc_itconfig (rtc_it_sec, ENABLE);

        Mask Interrupt request (allow seconds interrupt) rtc_waitforlasttask (); Rtc_enterconfigmode (); Configuration flag (Enter configuration mode) Rtc_setprescaler (Prescaler);

        Prescaler load Value rtc_waitforlasttask (); Rtc_setcounter (0xf73f);

        Set counter Value (17:34:55) Rtc_waitforlasttask (); Rtc_exitconfigmode (); Exit configuration mode (start updating RTC Register) Bkp_writebackupregister (BKP_DR1, bkp_data);
        Write backup Data} else {Rtc_waitforsynchro ();
        Rtc_itconfig (Rtc_it_sec, ENABLE);
    Rtc_waitforlasttask ();   

    } nvic_init (&nvic);
Rtc_get (); }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.