zoukankan      html  css  js  c++  java
  • 关于STM32 RTC的使用

    直接上代码,很爽。

    1、RTC的首次初始化问题

    使能时钟:RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);

    void RTC_Configuration(void)
    {

      /* Allow access to BKP Domain */
      PWR_BackupAccessCmd(ENABLE);


      /* 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();


      /* Wait until last write operation on RTC registers has finished */
      //RTC_WaitForLastTask();
      /* 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();
    }

    2、设置时间

           PWR_BackupAccessCmd(ENABLE); //必须有该行,否则写不进去

    RTC_WaitForLastTask();
            RTC_SetCounter(1356998400);//2013年1月1号0时0分0秒  
            BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);//时间更新标志,下次开机读取该值,如果已经设置,则不需要设置时间

    3、第二次开机RTC操作

        if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5){
            /* Backup data register value is not correct or not yet programmed (when
               the first time the program is executed) */
            /* RTC Configuration */
            RTC_Configuration();

    PWR_BackupAccessCmd(ENABLE); 
            RTC_WaitForLastTask(); 

     RTC_SetCounter(1356998400);//2013年1月1号0时0分0秒1356969600UL  

            BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);//时间更新标志
        }

    4、读取时间

    UNIXTime=RTC_GetCounter();

  • 相关阅读:
    python的复制,深拷贝和浅拷贝的区别(转)
    linux下ffmpeg安装(转)
    Linux下的tar压缩解压缩命令详解(转)
    centos7安装python-pip(转)
    爬山算法和模拟退火算法简介
    协方差、协方差矩阵定义与计算
    七种常见阈值分割代码(Otsu、最大熵、迭代法、自适应阀值、手动、迭代法、基本全局阈值法)
    Canny边缘检测算法原理及其VC实现详解(二)
    Canny边缘检测算法原理及其VC实现详解(一)
    perforce变量配置与使用
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3228780.html
Copyright © 2011-2022 走看看