我爱编程

运动控制器39:STM32的备份寄存器和实时时钟RTC

2018-02-05  本文已影响0人  吴松乾

备份寄存器简介

备份寄存器特性

功能描述

侵入检测

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核心相关的四个寄存器CNT,ALR,DIV,PRL只能通过备份域复位信号才能复位,跟CR相关的,则同步系统复位。

读RTC

读RTC之前,一定要等待CRL寄存器中的RSF被置1,否则数据有可能被破坏。

配置过程

功能时序:

RTC1.png

从上图可以看出各个时序的关系。

库函数

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();
   }

上一篇 下一篇

猜你喜欢

热点阅读