STM32F4 ST官方评估板笔记(10) - RTC实时时钟

RTC实时时钟

①NVIC_InitTypeDef NVIC_InitStructure;                        //结构体定义

  EXTI_InitTypeDef EXTI_InitStructure;

 

②/* Enable the PWR clock */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); //开PWR时钟

 

  /* Allow access to RTC */

  PWR_BackupAccessCmd(ENABLE); //允许访问RTC

 

NVIC_Init(&NVIC_InitStructure);

频道:RTC_WKUP_IRQn;

 

  EXTI_Init(&EXTI_InitStructure);

频道:EXTI_Line22;

模式:EXTI_Mode_Interrupt;

触发:EXTI_Trigger_Rising;

 

第一次进行RTC配置,完成后可省略本函数。

 

uint32_t RTC_ReadBackupRegister(uint32_t RTC_BKP_DR);

void RTC_WriteBackupRegister(uint32_t RTC_BKP_DR, uint32_t Data)    

//后备电源寄存器(可放数据,作初始化判断,16位数据宽度)

 

RTC_BKP_DR:

#define RTC_BKP_DR0                       ((uint32_t)0x00000000)

#define RTC_BKP_DR1                       ((uint32_t)0x00000001)

#define RTC_BKP_DR2                       ((uint32_t)0x00000002)

#define RTC_BKP_DR3                       ((uint32_t)0x00000003)

#define RTC_BKP_DR4                       ((uint32_t)0x00000004)

#define RTC_BKP_DR5                       ((uint32_t)0x00000005)

#define RTC_BKP_DR6                       ((uint32_t)0x00000006)

#define RTC_BKP_DR7                       ((uint32_t)0x00000007)

#define RTC_BKP_DR8                       ((uint32_t)0x00000008)

#define RTC_BKP_DR9                       ((uint32_t)0x00000009)

#define RTC_BKP_DR10                      ((uint32_t)0x0000000A)

#define RTC_BKP_DR11                      ((uint32_t)0x0000000B)

#define RTC_BKP_DR12                      ((uint32_t)0x0000000C)

#define RTC_BKP_DR13                      ((uint32_t)0x0000000D)

#define RTC_BKP_DR14                      ((uint32_t)0x0000000E)

#define RTC_BKP_DR15                      ((uint32_t)0x0000000F)

#define RTC_BKP_DR16                      ((uint32_t)0x00000010)

#define RTC_BKP_DR17                      ((uint32_t)0x00000011)

#define RTC_BKP_DR18                      ((uint32_t)0x00000012)

#define RTC_BKP_DR19                      ((uint32_t)0x00000013)

 

RTC_InitTypeDef   RTC_InitStructure;  //结构体定义

RTC_TimeTypeDef   RTC_TimeStructure;

RTC_DateTypeDef   RTC_DateStructure;

RTC_InitTypeDef   RTC_InitStructure;

/* Enable the PWR clock */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); //开PWR时钟

/* Allow access to RTC */

  PWR_BackupAccessCmd(ENABLE);                               //允许进入RTC设置

 

/* Enable the LSE OSC */

  RCC_LSEConfig(RCC_LSE_ON); //32.768kHz外部晶振作为晶振源

/* Wait till LSE is ready */  

  while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET){} //等待外部低速晶振就绪

 

/* Select the RTC Clock Source */

  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE); //选择晶振源

 

  /* Enable the RTC Clock */

  RCC_RTCCLKCmd(ENABLE); //开时钟

 

  /* Wait for RTC APB registers synchronisation */

  RTC_WaitForSynchro(); //等待同步

 

  /* Set the Time */

  RTC_TimeStructure

 

typedef struct

{

  uint8_t RTC_Hours;    /*!< Specifies the RTC Time Hour. //时

                        This parameter must be set to a value in the 0-12 range

                        if the RTC_HourFormat_12 is selected or 0-23 range if

                        the RTC_HourFormat_24 is selected. */

 

  uint8_t RTC_Minutes;  /*!< Specifies the RTC Time Minutes. //分

                        This parameter must be set to a value in the 0-59 range. */

  

  uint8_t RTC_Seconds;  /*!< Specifies the RTC Time Seconds. //秒

                        This parameter must be set to a value in the 0-59 range. */

 

  uint8_t RTC_H12;      /*!< Specifies the RTC AM/PM Time. //上午还是下午(12小时制用)

                        This parameter can be a value of @ref RTC_AM_PM_Definitions */

#define RTC_H12_AM                     ((uint8_t)0x00)

#define RTC_H12_PM                     ((uint8_t)0x40)

 

}RTC_TimeTypeDef;

 

  /* Set the Date */

  RTC_DateStructure

 

typedef struct

{

  uint8_t RTC_WeekDay; /*!< Specifies the RTC Date WeekDay.

                        This parameter can be a value of @ref RTC_WeekDay_Definitions */

#define RTC_Weekday_Monday             ((uint8_t)0x01)

#define RTC_Weekday_Tuesday            ((uint8_t)0x02)

#define RTC_Weekday_Wednesday          ((uint8_t)0x03)

#define RTC_Weekday_Thursday           ((uint8_t)0x04)

#define RTC_Weekday_Friday             ((uint8_t)0x05)

#define RTC_Weekday_Saturday           ((uint8_t)0x06)

#define RTC_Weekday_Sunday             ((uint8_t)0x07)

  

  uint8_t RTC_Month;   /*!< Specifies the RTC Date Month (in BCD format).

                        This parameter can be a value of @ref RTC_Month_Date_Definitions */

#define RTC_Month_January              ((uint8_t)0x01)

#define RTC_Month_February             ((uint8_t)0x02)

#define RTC_Month_March                ((uint8_t)0x03)

#define RTC_Month_April                ((uint8_t)0x04)

#define RTC_Month_May                  ((uint8_t)0x05)

#define RTC_Month_June                 ((uint8_t)0x06)

#define RTC_Month_July                 ((uint8_t)0x07)

#define RTC_Month_August               ((uint8_t)0x08)

#define RTC_Month_September            ((uint8_t)0x09)

#define RTC_Month_October              ((uint8_t)0x10)

#define RTC_Month_November             ((uint8_t)0x11)

#define RTC_Month_December             ((uint8_t)0x12)

 

  uint8_t RTC_Date;     /*!< Specifies the RTC Date.

                        This parameter must be set to a value in the 1-31 range. */

  

  uint8_t RTC_Year;     /*!< Specifies the RTC Date Year.

                        This parameter must be set to a value in the 0-99 range. */

}RTC_DateTypeDef;

 

  /* Calendar Configuration */

  RTC_InitStructure

 

typedef struct

{

  uint32_t RTC_HourFormat;   /*!< Specifies the RTC Hour Format.

                             This parameter can be a value of @ref RTC_Hour_Formats */

#define RTC_HourFormat_24              ((uint32_t)0x00000000) √

#define RTC_HourFormat_12              ((uint32_t)0x00000040)

 

  uint32_t RTC_AsynchPrediv; /*!< Specifies the RTC Asynchronous Predivider value.

                             This parameter must be set to a value lower than 0x7F */ //7F

  

  uint32_t RTC_SynchPrediv;  /*!< Specifies the RTC Synchronous Predivider value.

                             This parameter must be set to a value lower than 0x7FFF */  //FF

}RTC_InitTypeDef;

 

ErrorStatus RTC_Init(RTC_InitTypeDef* RTC_InitStruct); //初始化时钟

ErrorStatus RTC_SetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct); 

ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct);

 

void RTC_WakeUpClockConfig(uint32_t RTC_WakeUpClock);    //RTC唤醒分频

 

  * @param  RTC_WakeUpClock: Wakeup Clock source.

  *          This parameter can be one of the following values:

  *            @arg RTC_WakeUpClock_RTCCLK_Div16: RTC Wakeup Counter Clock = RTCCLK/16

  *            @arg RTC_WakeUpClock_RTCCLK_Div8: RTC Wakeup Counter Clock = RTCCLK/8

  *            @arg RTC_WakeUpClock_RTCCLK_Div4: RTC Wakeup Counter Clock = RTCCLK/4

  *            @arg RTC_WakeUpClock_RTCCLK_Div2: RTC Wakeup Counter Clock = RTCCLK/2

  *            @arg RTC_WakeUpClock_CK_SPRE_16bits: RTC Wakeup Counter Clock = CK_SPRE

  *            @arg RTC_WakeUpClock_CK_SPRE_17bits: RTC Wakeup Counter Clock = CK_SPRE

 

void RTC_SetWakeUpCounter(uint32_t RTC_WakeUpCounter); //唤醒计数0x7FF

 

/* Enable the Wakeup Interrupt */

RTC_ITConfig(RTC_IT_WUT, ENABLE); //注册唤醒中断

 

  /* Enable Wakeup Counter */

RTC_WakeUpCmd(ENABLE); //使能唤醒

 

  /* Backup SRAM ***************************************************************/

  /* Enable BKPRAM Clock */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE); //使能备用存储器时钟

 

  /* Enable the Backup SRAM low power Regulator to retain it's content in VBAT mode */

  PWR_BackupRegulatorCmd(ENABLE); //允许备用电池hold住SRAM

 

  /* Wait until the Backup SRAM low power Regulator is ready */

  while(PWR_GetFlagStatus(PWR_FLAG_BRR) == RESET){} //等待备用存储器就绪

 

⑥/* Wait for RTC APB registers synchronisation */

  RTC_WaitForSynchro(); //等待同步

  RTC_ClearITPendingBit(RTC_IT_WUT); //清除唤醒中断

  EXTI_ClearITPendingBit(EXTI_Line22); //清除中断线

 

  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_BKPSRAM, ENABLE);      //开备用存储器时钟

 

void RTC_WKUP_IRQHandler(void) //RTC中断服务

{

  if(RTC_GetITStatus(RTC_IT_WUT) != RESET)

  {

    /* Toggle on LED1 */

    LED_Toggle(orange);

    RTC_ClearITPendingBit(RTC_IT_WUT); //清除唤醒中断

    EXTI_ClearITPendingBit(EXTI_Line22); //清除中断线

  }

}

 

void RTC_GetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct); //获得时间

  void RTC_GetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct); //获得日期

 

ErrorStatus RTC_SetDate(uint32_t RTC_Format, RTC_DateTypeDef* RTC_DateStruct); //设定日期

ErrorStatus RTC_SetTime(uint32_t RTC_Format, RTC_TimeTypeDef* RTC_TimeStruct); //设定时间

 

你可能感兴趣的:(Embedded,System,STM32F4官方例程)