首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

STM32 RTC初始化

STM32 RTC初始化

RTC初始化过程
void RTC_Configuration(void)
{
  /* Enable PWR and BKP clocks */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
  /* Allow access to BKP Domain */
  PWR_BackupAccessCmd(ENABLE);  //说明1

  /* Reset Backup Domain */
  BKP_DeInit();

  /* Enable LSE */
  RCC_LSEConfig(RCC_LSE_ON);
  /* Wait till LSE is ready */

  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();       //说明2

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();       //说明3

  /* Enable the RTC Second */
  RTC_ITConfig(RTC_IT_SEC, ENABLE);

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();

  /* Set RTC prescaler: set RTC period to 1sec */
  RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (32.768 KHz)/(32767+1) */

  /* Wait until last write operation on RTC registers has finished */
  RTC_WaitForLastTask();
}
说明1:
使能对RTC寄存器的访问:
To enable access to the Backup reisters and the RTC,Proceed as follows:
> enable the power and backup interface clocks by setting the PWREN and BKPEN bits
   in the RCC_APB1ENR register
> set the DBP bit the Power Control Register(PWR_CR)to enable access to the Backup
   registers and RTC.

说明2:
Reading the RTC registers,after having disabled the RTC APB1 interface, the software must first wait for the RSF bit(Register Synchronized Flag)in the RTC_CRL register to be set by software

说明3:
A new value can be written to the RTC registers only when the RTOFF status bit value is '1'.

RTC注意事项:
RTC有两部分:RTC Core和APB1 Interface。控制寄存器RTC_CR属于APB1 Interface,RTC_PRL,RTC_DIV,RTC_CNT,RTC_ALR属于RTC Core。只有在RTOFF为1,才能写
RTC寄存器(包括RTC_CR)。RTC Core的4个寄存器中RTC_DIV是只读寄存器,其它三个RTC_PRL, RTC_CNT,RTC_ALR可读可写,用于配置RTC。但这三个寄存器的写操作(也称配置RTC寄存器),要先置位CNF,写完后要将其清零,过程如下:
Configuration procedure:
1.Poll RTOFF, wait until its value goes to '1';
2.Set the CNF bit to enter configuration mode
3.Write to one or more RTC registers
4.Clear the Cnf bit to exit configuration mode
5.Poll RTOFF,wait until its value goes to '1'to check the end of the write operation.
继承事业,薪火相传
返回列表