新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > stm32 的RTC 時鐘程序

        stm32 的RTC 時鐘程序

        作者: 時間:2016-11-11 來源:網絡 收藏
        前些日子做了stm32 RTC時鐘的程序,現在把它記錄下來。

        首先配置RTC,,使用外部時鐘32.768KHz。其中配置了秒中斷。

        本文引用地址:http://www.104case.com/article/201611/316828.htm

        RTCFirstConfigure()程序是第一次配置RTC,如果配置后以后上電不需要重新配置,如果RTC時鐘快了,可內部校準。

        void RTCFirstConfigure() //first ini
        {
        RCC_BackupResetCmd(ENABLE);
        RCC_BackupResetCmd(DISABLE);

        RCC_LSEConfig(RCC_LSE_ON); //enable LSE clock 32.768K
        while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
        {}
        // Select LSE as RTC Clock Source
        RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
        //Enable RTC Clock /
        RCC_RTCCLKCmd(ENABLE);
        // Wait for RTC registers synchronization /
        RTC_WaitForSynchro();
        // Wait until last write operation on RTC registers has finished /
        RTC_WaitForLastTask();
        // Enable the RTC Second Interrupt/
        RTC_ITConfig(RTC_IT_SEC, ENABLE);
        RTC_WaitForLastTask();
        RTC_SetPrescaler(32767); // RTC period = RTCCLK/RTC_PR = (32.768KHz)/(32767+1)/
        RTC_WaitForLastTask();

        // BKP_SetRTCCalibrationValue(120); //RTC Calibration

        RCC_ClearFlag();
        }

        RTCNorConfigure()程序配置完后每次上電都運行的程序

        void RTCNorConfigure() //normal ini
        {
        RTC_WaitForSynchro();
        RTC_WaitForLastTask();
        RTC_SetPrescaler(32767);
        BKP_SetRTCCalibrationValue(0); //RTC Calibration
        RTC_ITConfig(RTC_IT_SEC, ENABLE);
        RTC_WaitForLastTask();
        RCC_ClearFlag();
        }

        int main(void)
        {
        /* System Clocks Configuration */
        RCC_Configuration();
        /* NVIC configuration */
        NVIC_Configuration();

        /* Configure the GPIO ports */
        GPIO_Configuration();

        UART_Configuration();
        if (BKP_ReadBackupRegister(BKP_DR1) != 0xA4A4) //
        //if(GPIO_ReadInputBit(GPIOA,GPIO_Pin_5)==0x01)
        {
        RTCFirstConfigure();
        Time_Adjust(2011,5,20,18,12,0);
        BKP_WriteBackupRegister(BKP_DR1, 0xA4A4);
        }
        else
        {
        RTCNorConfigure();
        }

        // BKP_WriteBackupRegister(BKP_DR2, 0x1234);

        while (1)
        {
        }
        }



        關鍵詞: stm32RTC時鐘程

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 望都县| 崇仁县| 桓仁| 克山县| 连城县| 响水县| 日喀则市| 通州区| 依兰县| 渑池县| 庆城县| 静海县| 松江区| 泸州市| 石河子市| 瓦房店市| 樟树市| 句容市| 武义县| 桐柏县| 固始县| 平阴县| 万州区| 绥阳县| 辉县市| 突泉县| 米易县| 铜鼓县| 贺兰县| 隆回县| 乌鲁木齐市| 贡山| 横峰县| 合水县| 凤阳县| 大冶市| 云龙县| 鄢陵县| 白银市| 山东| 乌兰县|