⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stm8l15x_rtc.c

📁 STM8L的tim4定时器使用
💻 C
📖 第 1 页 / 共 5 页
字号:
*         will be initialized.
* @retval None
*/
void RTC_AlarmStructInit(RTC_AlarmTypeDef* RTC_AlarmStruct)
{
  /* Alarm Time Settings : Time = 00h:00mn:00sec */
  RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = RTC_H12_AM;
  RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = 0;
  RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = 0;
  RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = 0;

  /* Alarm Date Settings : Date = 1st day of the month*/
  RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
  RTC_AlarmStruct->RTC_AlarmDateWeekDay = 1;

  /* Alarm Masks Settings : Mask =  all fields are masked*/
  RTC_AlarmStruct->RTC_AlarmMask = RTC_AlarmMask_All;
}
/**
  * @brief  Gets the RTC Alarm configuration.
  * @param  RTC_Format: specifies the format of the entered parameters.
  *         This parameter can be one of the @ref RTC_Format_TypeDef enumeration.
  * @param  RTC_AlarmStruct:  pointer to a  @ref RTC_AlarmTypeDef structure that
  *         will contain the Alarm configuration information of  the RTC.
  * @retval None
  */
void RTC_GetAlarm(RTC_Format_TypeDef RTC_Format,
                  RTC_AlarmTypeDef* RTC_AlarmStruct)
{
  uint8_t tmpreg1 = 0;
  uint8_t tmpreg2 = 0;
  uint8_t tmpreg3 = 0;
  uint8_t tmpreg4 = 0;
  uint8_t alarmmask = 0;

  /* Check the parameters */
  assert_param(IS_RTC_FORMAT(RTC_Format));

  /* Get Alarm registers data */
  tmpreg1 = (uint8_t)RTC->ALRMAR1;
  tmpreg2 = (uint8_t)RTC->ALRMAR2;
  tmpreg3 = (uint8_t)RTC->ALRMAR3;
  tmpreg4 = (uint8_t)RTC->ALRMAR4;

  /* Fill the structure with the read parameters */
  RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = (uint8_t)((uint8_t)tmpreg1 & (uint8_t)((uint8_t)RTC_ALRMAR1_ST | (uint8_t)RTC_ALRMAR1_SU));
  alarmmask = (uint8_t)(tmpreg1 & RTC_ALRMAR1_MSK1);

  /* Fill the structure with the read parameters */
  RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = (uint8_t)((uint8_t)tmpreg2 & (uint8_t)((uint8_t)RTC_ALRMAR2_MNT | (uint8_t)RTC_ALRMAR2_MNU));
  alarmmask = (uint8_t)((alarmmask) | (uint8_t)((uint8_t)(tmpreg2 & RTC_ALRMAR2_MSK2) >> 1));

  /* Fill the structure with the read parameters */
  RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = (uint8_t)((uint8_t)tmpreg3 & (uint8_t)((uint8_t)RTC_ALRMAR3_HT | (uint8_t)RTC_ALRMAR3_HU));
  RTC_AlarmStruct->RTC_AlarmTime.RTC_H12 = (RTC_H12_TypeDef)((uint8_t)tmpreg3 & (uint8_t)RTC_ALRMAR3_PM);
  alarmmask = (uint8_t)((alarmmask) | (uint8_t)((uint8_t)((uint8_t)tmpreg3 & (uint8_t)RTC_ALRMAR3_MSK3) >> 2));

  /* Fill the structure with the read parameters */
  RTC_AlarmStruct->RTC_AlarmDateWeekDay = (uint8_t)((uint8_t)tmpreg4 & (uint8_t)((uint8_t)RTC_ALRMAR4_DT | (uint8_t)RTC_ALRMAR4_DU));
  RTC_AlarmStruct->RTC_AlarmDateWeekDaySel = (RTC_AlarmDateWeekDaySel_TypeDef)((uint8_t)tmpreg4 & (uint8_t)RTC_ALRMAR4_WDSEL);
  alarmmask = (uint8_t)((alarmmask) | (uint8_t)((uint8_t)((uint8_t)tmpreg4 & RTC_ALRMAR4_MSK4) >> 3));

  RTC_AlarmStruct->RTC_AlarmMask = alarmmask;

  if (RTC_Format == RTC_Format_BIN)
  {
    RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours = Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Hours);
    RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes = Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Minutes);
    RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds = Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmTime.RTC_Seconds);
    RTC_AlarmStruct->RTC_AlarmDateWeekDay = Bcd2ToByte(RTC_AlarmStruct->RTC_AlarmDateWeekDay);
  }
}

/**
  * @brief  Enables or disables the RTC Alarm.
  * @param  NewState: new state of the alarm. This parameter can be: ENABLE or DISABLE.
  * @retval An ErrorStatus enumeration value:
  *          - SUCCESS: RTC Alarm is enabled/disabled
  *          - ERROR: RTC Alarm is not enabled/disabled
  */
ErrorStatus RTC_AlarmCmd(FunctionalState NewState)
{
  __IO uint16_t alrawfcount = 0;
  ErrorStatus status = ERROR;
  uint8_t temp1 = 0;

  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NewState));

  /* Disable the write protection for RTC registers */
  RTC->WPR = 0xCA;
  RTC->WPR = 0x53;

  /* Configure the Alarm state */
  if (NewState != DISABLE)
  { /*Enable the Alarm*/
    RTC->CR2 |= (uint8_t)(RTC_CR2_ALRAE);
    status = SUCCESS;
  }
  else
  {  /* Disable the Alarm */
    RTC->CR2 &= (uint8_t)~(RTC_CR2_ALRAE) ;

    /* Wait until ALRxWF flag is set */
    temp1 = (uint8_t)(RTC->ISR1 & RTC_ISR1_ALRAWF);
    while ((alrawfcount != ALRAWF_TIMEOUT) && (temp1 == RESET))
    {
      alrawfcount++;
    }

    if ((RTC->ISR1 &  RTC_ISR1_ALRAWF) == RESET)
    {
      status = ERROR;
    }
    else
    {
      status = SUCCESS;
    }
  }

  /* Enable the write protection for RTC registers */
  RTC->WPR = 0xFF; 

  /* Return the status*/
  return (ErrorStatus)status;
}
/**
* @brief  Configure the RTC Alarm Subseconds value and mask.
* @param  RTC_AlarmSubSecondValue: specifies the Subseconds value.
*         This parameter can be a value from 0 to 0x7FFF.
* @param  RTC_AlarmSubSecondMask:  specifies the Subseconds Mask.
*         This parameter can be one of the @ref RTC_AlarmSubSecondMask_TypeDef enumeration.
* @retval An ErrorStatus enumeration value:
*          - SUCCESS: Alarm Subseconds value and mask are configured
*          - ERROR: Alarm Subseconds value and mask are  not configured
*/
ErrorStatus RTC_AlarmSubSecondConfig(uint16_t RTC_AlarmSubSecondValue,
                                     RTC_AlarmSubSecondMask_TypeDef RTC_AlarmSubSecondMask)
{
  uint8_t alarmstatus = 0;
  ErrorStatus status = ERROR;

  /* Check the parameters */
  assert_param(IS_RTC_ALARM_SS_VALUE(RTC_AlarmSubSecondValue));
  assert_param(IS_RTC_ALARM_SS_MASK(RTC_AlarmSubSecondMask));

  /* Disable the write protection for RTC registers */
  RTC->WPR = 0xCA;
  RTC->WPR = 0x53;

  /* Check if the initialization mode is not set */
  if ((RTC->ISR1 & RTC_ISR1_INITF) == RESET)
  {
    /* save  Alarm status */
    alarmstatus = (uint8_t)(RTC->CR2 | RTC_CR2_ALRAE);

    /* Disable the Alarm */
    RTC->CR2 &= (uint8_t)~(RTC_CR2_ALRAE);

    /* Configure the Alarm register */
    RTC->ALRMASSRH = (uint8_t)(RTC_AlarmSubSecondValue >> 8);
    RTC->ALRMASSRL = (uint8_t)(RTC_AlarmSubSecondValue);
    RTC->ALRMASSMSKR = (uint8_t)RTC_AlarmSubSecondMask;

    /* restore the saved  Alarm status */
    RTC->CR2 |= alarmstatus;

    status = SUCCESS;
  }
  else
  {
    status = ERROR;
  }

  /* Enable the write protection for RTC registers */
  RTC->WPR = 0xFF; 

  return (ErrorStatus)status;
}



/**
  * @}
  */

/** @defgroup RTC_Group4 WakeUp Timer configuration functions
 *  @brief   WakeUp Timer configuration functions 
 *
@verbatim   
 ===============================================================================
                     WakeUp Timer configuration functions
 ===============================================================================  

  This section provide functions allowing to program and read the RTC WakeUp.

@endverbatim
  * @{
  */
/**
  * @brief  Configures the RTC Wakeup clock source.
  * @pre    Before configuring the wakeup unit Clock source, the wake up Unit must
  *         be disabled (if enabled) using RTC_WakeUpCmd(Disable) .
  * @param  RTC_WakeupClockSrc: specifies the Wakeup clock source,
  *         this parameter  can be one of the @ref RTC_WakeupClockSrc_TypeDef enumeration.
  * @retval None
  */
void RTC_WakeUpClockConfig(RTC_WakeUpClock_TypeDef RTC_WakeUpClock)
{

  /* Check the parameters */
  assert_param(IS_RTC_WAKEUP_CLOCK(RTC_WakeUpClock));

  /* Disable the write protection for RTC registers */
  RTC->WPR = 0xCA;
  RTC->WPR = 0x53;

  /* Disable the Wakeup timer in RTC_CR2 register */
  RTC->CR2 &= (uint8_t)~RTC_CR2_WUTE;

  /* Clear the Wakeup Timer clock source bits in CR1 register */
  RTC->CR1 &= (uint8_t)~RTC_CR1_WUCKSEL;

  /* Configure the clock source */
  RTC->CR1 |= (uint8_t)RTC_WakeUpClock;

  /* Enable the write protection for RTC registers */
  RTC->WPR = 0xFF; 
}

/**
  * @brief  Sets the RTC Wakeup counter.
  * @note   Before configuring the wakeup unit counter, the wake up Unit must be
  *         disabled (if enabled) using RTC_WakeUpCmd(Disable).
  * @param  RTC_WakeupCounter: specifies the Wake up counter,
  *         This parameter can be a value from 0x0000 to 0xFFFF.
  * @retval None.
  */
void RTC_SetWakeUpCounter(uint16_t RTC_WakeupCounter)
{
  /* Disable the write protection for RTC registers */
  RTC->WPR = 0xCA;
  RTC->WPR = 0x53;


  /* Configure the Wakeup Timer counter */
  RTC->WUTRH = (uint8_t)(RTC_WakeupCounter >> 8);
  RTC->WUTRL = (uint8_t)(RTC_WakeupCounter);

  /* Enable the write protection for RTC registers */
  RTC->WPR = 0xFF; 
}

/**
  * @brief  Returns the RTC Wakeup timer counter value.
  * @param  None.
  * @retval RTC Wakeup Counter value.
  */
uint16_t RTC_GetWakeUpCounter(void)
{
  uint16_t tmpreg = 0;

  /* Get the counter value */
  tmpreg = ((uint16_t)RTC->WUTRH) << 8;
  tmpreg |= RTC->WUTRL;

  /* return RTC Wakeup Counter value*/
  return (uint16_t)tmpreg;
}

/**
  * @brief  Enables or Disables the RTC Wakeup Unit.
  * @param  NewState: new state of the Wakeup Unit. This parameter can 
  *                   be: ENABLE or DISABLE.
  * @retval An ErrorStatus enumeration value:
  *          - SUCCESS : RTC Wakeup Unit is enabled/disabled
  *          - ERROR    : RTC Wakeup Unit is not enabled/disabled
  */
ErrorStatus RTC_WakeUpCmd(FunctionalState NewState)
{
  ErrorStatus status = ERROR;
  uint16_t wutwfcount = 0;

  /* Check the parameters */
  assert_param(IS_FUNCTIONAL_STATE(NewState));

  /* Disable the write protection for RTC registers */
  RTC->WPR = 0xCA;
  RTC->WPR = 0x53;

  if (NewState != DISABLE)
  {
    /* Enable the Wakeup Timer */
    RTC->CR2 |= (uint8_t)RTC_CR2_WUTE;

    status = SUCCESS;
  }
  else
  {
    /* Disable the Wakeup Timer */
    RTC->CR2 &= (uint8_t)~RTC_CR2_WUTE;

    /* Wait until WUTWF flag is set */
    while (((RTC->ISR1 & RTC_ISR1_WUTWF) == RESET) && ( wutwfcount != WUTWF_TIMEOUT))
    {
      wutwfcount++;
    }

    /* Check WUTWF flag is set or not */
    if ((RTC->ISR1 & RTC_ISR1_WUTWF) == RESET)
    {
      status = ERROR;
    }
    else
    {
      status = SUCCESS;
    }
  }

  /* Enable the write protection for RTC registers */
  RTC->WPR = 0xFF; 

  /* Return the status*/
  return (ErrorStatus)status;
}






/**
  * @}
  */

/** @defgroup RTC_Group5 Daylight Saving configuration functions
 *  @brief   Daylight Saving configuration functions 
 *
@verbatim   
 ===============================================================================
                    Daylight Saving configuration functions
 ===============================================================================  

  This section provide functions allowing to configure the RTC DayLight Saving.

@endverbatim
  * @{
  */

/**
  * @brief  Adds or subtracts one hour from the current time depending on
  *         the daylight saving parameter.
  * @param  RTC_DayLightSaving: the day light saving Mode
  *         This parameter can be one of the @ref RTC_DayLightSaving_TypeDef 
  *         enumeration.
  * @param  RTC_StoreOperation: the day light saving store operation
  *         This parameter can be one of the @ref RTC_StoreOperation_TypeDef 
  *         enumeration.
  * @retval None
  */
void RTC_DayLightSavingConfig(RTC_DayLightSaving_TypeDef RTC_DayLightSaving,
                              RTC_StoreOperation_TypeDef RTC_StoreOperation)
{
  /* Check the parameters */
  assert_param(IS_RTC_DAYLIGHT_SAVING(RTC_DayLightSaving));
  assert_param(IS_RTC_STORE_OPERATION(RTC_StoreOperation));

  /* Disable the write protection for RTC registers */
  RTC->WPR = 0xCA;
  RTC->WPR = 0x53;

  /* Clear the bits to be configured */
  RTC->CR3 &= (uint8_t)~(RTC_CR3_BCK);

  /* Configure the RTC_CR3 register */
  RTC->CR3 |= (uint8_t)((uint8_t)RTC_DayLightSaving | (uint8_t)RTC_StoreOperation);

  /* Enable the write protection for RTC registers */
  RTC->WPR = 0xFF; 
}

/**
  * @brief  Returns the stored operation.
  * @param  None
  * @retval the store operation, this parameter can be one of

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -