微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > stm32 利用备份寄存器保存实时时钟数据

stm32 利用备份寄存器保存实时时钟数据

时间:11-09 来源:互联网 点击:
在实际应用中,会出现许多复位或者掉电的情况,下面提供了一种方法使即使是在掉电和复位事件发生时,仍旧可以利用低功耗模式继续对于实时时钟进行供电,保证时钟的正常运行!


//bsp_rtc.h#ifndef _BSP_RTC_H#define _BSP_RTC_H#include "misc.h"/*全局变量*/	   uint8_t RTCInterruptFlag=0;	 //RTC 中断标志uint32_t RTC_TimeNum=0;			  // 设置时间变量uint16_t Year;uint8_t Month;uint8_t Day;/* RTC hardware init*/void RTC_NVIconfigration(void);void RTC_configration(void);void RTC_Init(void);/*日历及时间输入*/uint8_t RTC_InputTime(uint32_t border);uint32_t RTC_TimeCollate(void);void RTC_RxIntHandler(void); /*获得年月日*/uint16_t GetYear();uint8_t GetMonth();uint8_t GetDay();void CalenderSet(void);void  CalenderCount(void);static uint8_t Choice_MonthDay(uint16_t temp_year,uint8_t temp_month);/*显示*/void RTC_TimeDisplay(uint32_t TimeVar);/*测试*/void Text_RTC(void); #endif/*_BSP_RTC_H*/
bsp_rtc.c
#include "bsp_rtc.h"#define RTCClockSource_LSEuint32_t TimeDisplay=0;	// 用于显示测试 每次进入中断改变  /**\Function      RTC_NVIconfigration()*\Description   设置RTC中断优先级*\Parameter     void*\Return        void*\Note          *\Log          	2014年7月24日*/void RTC_NVIconfigration(void){NVIC_InitTypeDef NVIC_InitStructure;NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);	  //已经在bsp_usart.c进行了设置/* Enable the RTC Interrupt */NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn;					//配置外部中断源(秒中断)NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure);}/**\Function      RTC_configration(void)*\Description   RTC配置函数*\Parameter     void*\Return        void*\Note          *\Log          	2014年7月24日*/void RTC_configration(void){/* 使能 PWR 和 BKP 的时钟 */RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);PWR_DeInit();/* 允许访问BKP区域 */PWR_BackupAccessCmd(ENABLE);/* 复位BKP */BKP_DeInit();#ifdef RTCClockSource_LSI/* 使能内部RTC时钟 */RCC_LSICmd(ENABLE);/* 等待RTC内部时钟就绪 */while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET){}/* 选择RTC内部时钟为RTC时钟 */RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);#elif defined	RTCClockSource_LSE/* 使能RTC外部时钟 */RCC_LSEConfig(RCC_LSE_ON);/* 等待RTC外部时钟就绪 */while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET){}/* 选择RTC外部时钟为RTC时钟 */RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);#endif/* 使能RTC时钟 */RCC_RTCCLKCmd(ENABLE);#ifdef RTCClockOutput_Enable/* Disable the Tamper Pin */BKP_TamperPinCmd(DISABLE); /* To output RTCCLK/64 on Tamper pin, the tamperfunctionality must be disabled *//* 使能在TAMPER脚输出RTC时钟 */BKP_RTCCalibrationClockOutputCmd(ENABLE);#endif/* 等待RTC寄存器同步 */RTC_WaitForSynchro();/* 等待写RTC寄存器完成 */RTC_WaitForLastTask();/* 使能RTC秒中断 */RTC_ITConfig(RTC_IT_SEC, ENABLE);/* 等待写RTC寄存器完成 */RTC_WaitForLastTask();/* 设置RTC预分频 */#ifdef RTCClockSource_LSIRTC_SetPrescaler(31999);            /* RTC period = RTCCLK/RTC_PR = (32.000 KHz)/(31999+1) */#elif defined	RTCClockSource_LSERTC_SetPrescaler(32767);            /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */#endif/* 等待写RTC寄存器完成 */RTC_WaitForLastTask();}/**\Function      RTC_Init(void)*\Description   RTC初始化*\Parameter     void*\Return        void*\Note          *\Log          	2014年7月24日*/void RTC_Init(void){if (BKP_ReadBackupRegister(BKP_DR1)!=0x0229){//printf("\n RTC not yet configured....");RTC_configration();printf("\n RTC实时时钟设置...");            

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top