备份寄存器简介
- 备份寄存器一共有42个16位的寄存器,可以存储84字节数据,由BAT电源供电。
- 复位以后,备份寄存器和RTC禁止访问,如果要访问,需要先打开电源和备份的时钟,以及电源控制使能两个功能的访问。
备份寄存器特性
- 84字节
- 管理侵入检测并具有中断功能
- 用来存储RTC校验值
- PC13输出RTC的校准时钟
功能描述
侵入检测
- 当TAMPER引脚上的信号从0变成1或者从1变成0(取决于备份控制寄存器BKP_CR的TPAL位),会产生一个侵入检测事件。这里注意,如果一开始引脚为高电平,我们设置侵入后初始为低电平,设置完成就会产生一个额外的侵入,这里要警惕。
- 如果设置了中断,侵入检测到以后,引脚被禁止,需要重新设置。
RTC校准
我们还可以将RTC时钟分频以后输出到侵入引脚,通过RTC校验寄存器使能该功能,如何进行RTC精度提高,有可以参考AN2604文档。
库函数
功能相当简单,列表如下:
void BKP_ClearFlag (void)
Clears Tamper Pin Event pending flag.
void BKP_ClearITPendingBit (void)
Clears Tamper Pin Interrupt pending bit.
void BKP_DeInit (void)
Deinitializes the BKP peripheral registers to their default reset values.
FlagStatus BKP_GetFlagStatus (void)
Checks whether the Tamper Pin Event flag is set or not.
ITStatus BKP_GetITStatus (void)
Checks whether the Tamper Pin Interrupt has occurred or not.
void BKP_ITConfig (FunctionalState NewState)
Enables or disables the Tamper Pin Interrupt.
uint16_t BKP_ReadBackupRegister (uint16_t BKP_DR)
Reads data from the specified Data Backup Register.
void BKP_RTCOutputConfig (uint16_t BKP_RTCOutputSource)
Select the RTC output source to output on the Tamper pin.
void BKP_SetRTCCalibrationValue (uint8_t CalibrationValue)
Sets RTC Clock Calibration value.
void BKP_TamperPinCmd (FunctionalState NewState)
Enables or disables the Tamper Pin activation.
void BKP_TamperPinLevelConfig (uint16_t BKP_TamperPinLevel)
Configures the Tamper Pin active level.
void BKP_WriteBackupRegister (uint16_t BKP_DR, uint16_t Data)
Writes user data to the specified Data Backup Register.
实时时钟RTC
概述
实时时钟由两部分组成:
- RTC_CR,和中断控制,有三个中断和标志:SECOND,OVERFLOW和ALARM
- RTC核心,有两个模块,一个分时钟分频,一个是32位计数器。如果CNT=ALR,则产生一个中断。
复位
跟RTC核心相关的四个寄存器CNT,ALR,DIV,PRL只能通过备份域复位信号才能复位,跟CR相关的,则同步系统复位。
读RTC
读RTC之前,一定要等待CRL寄存器中的RSF被置1,否则数据有可能被破坏。
配置过程
- 查询RTOFF位,等待变位1
- 设置CNF,进入设置模式
- 写RTC寄存器
- 清楚CNF,退出设置模式
- 继续查询RTOFF,等待完成。
功能时序:

从上图可以看出各个时序的关系。
库函数
void RTC_ClearFlag (uint16_t RTC_FLAG)
//状态一共有4个,溢出,闹钟,秒,寄存器同步
void RTC_ClearITPendingBit (uint16_t RTC_IT)
Clears the RTC's interrupt pending bits.
//中断源有3个,溢出,闹钟,秒
void RTC_EnterConfigMode (void)
//进入RTC配置模式,如下程序,可以设置闹钟
void RTC_ExitConfigMode (void)
//退出配置模式
uint32_t RTC_GetCounter (void)
//获取RTC_CNT值,参考下面的程序
uint32_t RTC_GetDivider (void)
//获取分频
FlagStatus RTC_GetFlagStatus (uint16_t RTC_FLAG)
//获取标志状态
ITStatus RTC_GetITStatus (uint16_t RTC_IT)
//中断标志
void RTC_ITConfig (uint16_t RTC_IT, FunctionalState NewState)
//配置中断
void RTC_SetAlarm (uint32_t AlarmValue)
//设置闹钟值ALR
void RTC_SetCounter (uint32_t CounterValue)
//设置CNT
void RTC_SetPrescaler (uint32_t PrescalerValue)
//设置分频
void RTC_WaitForLastTask (void)
//等待上一步工作完成
void RTC_WaitForSynchro (void)
//等待同步
例程如下:
void RTC_SetAlarm(uint32_t AlarmValue)
{
RTC_EnterConfigMode();
/* Set the ALARM MSB word */
RTC->ALRH = AlarmValue >> 16;
/* Set the ALARM LSB word */
RTC->ALRL = (AlarmValue & RTC_LSB_MASK);
RTC_ExitConfigMode();
}
当时间计时到23:59:59时候,进行清零。
if (RTC_GetCounter() == 0x0001517F)
{
RTC_SetCounter(0x0);
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
}
网友评论