原创 STM32 RTC初始化

2011-1-3 16:25 3441 4 5 分类: MCU/ 嵌入式
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.
PARTNER CONTENT

文章评论1条评论)

登录后参与讨论

用户377235 2013-6-25 21:34

感谢。试了很多 只有你这个可以,,哈哈哈
相关推荐阅读
用户329963 2011-10-16 18:09
JLH1969
JLH1969学习 1.       静态工作的设置 通过设置Tr4的基极电压来设置VE到1/2Vcc,通过电阻R1和R2来设置Tr1、Tr2的静态工作电流。   2.       电压增益 J...
用户329963 2011-07-01 10:05
AP发推导
AP法推导 AP法的推导,参考了PI的应用手册和K.B.的《开关电源手册》。 ...
用户329963 2011-06-20 22:54
PADS 2007初学
1. PADS2007的过孔VIAPADS要添加过孔必须先选择要添加过孔的网络,然后点击鼠标右键选择Add Via。可以在过孔Properties中更改成自己需要的过孔类型。由Setup > P...
用户329963 2011-05-31 23:29
uCOS-II在芯唐Cortex-M0上移植
uCOS-II在芯唐Cortex-M0(NuMicro M051)上移植(by homer) 1.新建includes.h  因为uCOS-II的源程序都包含了includes.h,所以先新建inc...
用户329963 2011-05-20 17:34
电容和电感
电容:电容两端的电压不能突变;电感:流过电感两端的电流不能突变。电容:Q=CV      dQ/dt = I = C * (dv/dt)电感:φ=LI    dφ/dt = V = L * (dI/d...
用户329963 2011-04-15 15:18
JHL1969功放学习
JHL1969学习1.      静态工作点的设置输出电容左端的直流电压等于PNP三极管基极电压加上0.7V再加上1.4*2.7/8.2静态电流可以通过调整R7的大小来调整。  2.      增益R...
我要评论
1
4
关闭 站长推荐上一条 /3 下一条