×
单片机 > 单片机程序设计 > 详情

stm32 的RTC 时钟程序

发布时间:2020-05-18 发布时间:
|
前些日子做了stm32 RTC时钟的程序,现在把它记录下来。

首先配置RTC,,使用外部时钟32.768KHz。其中配置了秒中断。

RTCFirstConfigure()程序是第一次配置RTC,如果配置后以后上电不需要重新配置,如果RTC时钟快了,可内部校准。

void RTCFirstConfigure()   //first ini
   {
    RCC_BackupResetCmd(ENABLE);
    RCC_BackupResetCmd(DISABLE);
     
     RCC_LSEConfig(RCC_LSE_ON);  //enable LSE clock 32.768K
     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();
     // Wait until last write operation on RTC registers has finished /
     RTC_WaitForLastTask();
     // Enable the RTC Second Interrupt/
     RTC_ITConfig(RTC_IT_SEC, ENABLE);
     RTC_WaitForLastTask();
     RTC_SetPrescaler(32767); // RTC period = RTCCLK/RTC_PR = (32.768KHz)/(32767+1)/
     RTC_WaitForLastTask();
     
   //  BKP_SetRTCCalibrationValue(120); //RTC Calibration
       
     RCC_ClearFlag();
   }

 

 RTCNorConfigure()程序配置完后每次上电都运行的程序

  void RTCNorConfigure()    //normal ini
  {
    RTC_WaitForSynchro();
    RTC_WaitForLastTask();
    RTC_SetPrescaler(32767);
    BKP_SetRTCCalibrationValue(0); //RTC Calibration
    RTC_ITConfig(RTC_IT_SEC, ENABLE);
    RTC_WaitForLastTask();
    RCC_ClearFlag();  
  }

int main(void)
{
  /* System Clocks Configuration */
  RCC_Configuration();       
  /* NVIC configuration */
  NVIC_Configuration();

  /* Configure the GPIO ports */
  GPIO_Configuration();

  UART_Configuration();
 if (BKP_ReadBackupRegister(BKP_DR1) != 0xA4A4)   //
  //if(GPIO_ReadInputBit(GPIOA,GPIO_Pin_5)==0x01)
  {
    RTCFirstConfigure();
    Time_Adjust(2011,5,20,18,12,0);
    BKP_WriteBackupRegister(BKP_DR1, 0xA4A4);
  }
 else 
  {
    RTCNorConfigure() ;
  }
  
   // BKP_WriteBackupRegister(BKP_DR2, 0x1234);

  while (1)
  {
  }
}



『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
STM32中断向量表的位置.重定向