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

📄 stm8l15x_rtc.c

📁 STM8L的tim4定时器使用
💻 C
📖 第 1 页 / 共 5 页
字号:
  *         the @ref RTC_StoreOperation_TypeDef enumeration.
  */
RTC_StoreOperation_TypeDef  RTC_GetStoreOperation(void)
{
  /* Return the stored operation*/
  return (RTC_StoreOperation_TypeDef)(RTC->CR3 & RTC_CR3_BCK);
}






/**
  * @}
  */

/** @defgroup RTC_Group6 Output pin Configuration function
 *  @brief   Output pin Configuration function 
 *
@verbatim   
 ===============================================================================
                         Output pin Configuration function
 ===============================================================================  

  This section provide functions allowing to configure the RTC Output source.

@endverbatim
  * @{
  */
/**
  * @brief  Configures the RTC output for the output pin (RTC_ALARM output).
  * @param  RTC_OutputSel: Specifies which signal will be mapped to the output.
  *         This parameter can be one parameter from the 
  *         @ref RTC_OutputSel_TypeDef enumeration.
  * @param  RTC_OutputPolarity: Specifies the polarity of the output signal.
  *         This parameter can be one parameter from the 
  *         @ref RTC_OutputPolarity_TypeDef enumeration.
  * @retval None
  */
void RTC_OutputConfig(RTC_Output_TypeDef RTC_Output,
                      RTC_OutputPolarity_TypeDef RTC_OutputPolarity)
{
  /* Check the parameters */
  assert_param(IS_RTC_OUTPUT_SEL(RTC_Output));
  assert_param(IS_RTC_OUTPUT_POL(RTC_OutputPolarity));

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

  /* Configure the output selection and polarity */
  RTC->CR3 |= (uint8_t)((uint8_t)RTC_Output | (uint8_t)RTC_OutputPolarity);

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




/**
  * @}
  */

/** @defgroup RTC_Group7 Output pin Configuration function
 *  @brief   Output pin Configuration function 
 *
@verbatim   
 ===============================================================================
                         Output pin Configuration function
 ===============================================================================  

  This section provide functions allowing to configure the RTC Output source.

@endverbatim
  * @{
  */
/**
* @brief  Configures the Synchronization Shift Control Settings.
* @param  RTC_ShiftAdd1S : Select to add or not 1 second to the time Calendar.
*         This parameter can be one parameter from the @ref RTC_ShiftAdd1S_TypeDef
*         enumeration.
* @param  RTC_ShiftSubFS: Select the number of Second Fractions to Substitute.
*         This parameter can be one any value from 0 to 0x7FFF.
 * @retval An ErrorStatus enumeration value:
*          - SUCCESS: RTC Shift registers are configured
*          - ERROR: RTC Shift registers are not configured
*/
ErrorStatus RTC_SynchroShiftConfig(RTC_ShiftAdd1S_TypeDef RTC_ShiftAdd1S,
                                   uint16_t RTC_ShiftSubFS)
{
  uint8_t shiftrhreg = 0;
  ErrorStatus status = ERROR;
  uint16_t shpfcount = 0;

  /* Check the parameters */
  assert_param(IS_RTC_SHIFT_ADD1S(RTC_ShiftAdd1S));
  assert_param(IS_RTC_SHIFT_SUBFS(RTC_ShiftSubFS));

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

  /* Check if a Shift is pending*/
  if ((RTC->ISR1 & RTC_ISR1_SHPF) != RESET)
  {
    /* wait until the shift is completed*/
    while (((RTC->ISR1 & RTC_ISR1_SHPF) != RESET) && (shpfcount != SHPF_TIMEOUT))
    {
      shpfcount++;
    }
  }

  /* check if the Shift pending is completed or if there is no Shift operation at all*/
  if ((RTC->ISR1 & RTC_ISR1_SHPF) == RESET)
  {
    /* Configure the Shift settings */
    shiftrhreg = (uint8_t)((uint8_t)(RTC_ShiftSubFS >> 8) | (uint8_t)(RTC_ShiftAdd1S));
    RTC->SHIFTRH = (uint8_t)(shiftrhreg);
    RTC->SHIFTRL = (uint8_t)(RTC_ShiftSubFS);

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

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

  return (ErrorStatus)(status);
}



/**
  * @}
  */

/** @defgroup RTC_Group8 Output pin Configuration function
 *  @brief   Output pin Configuration function 
 *
@verbatim   
 ===============================================================================
                         Output pin Configuration function
 ===============================================================================  

  This section provide functions allowing to configure the RTC Output source.

@endverbatim
  * @{
  */
/**
* @brief  Configures the Smooth Calibration Settings.
* @param  RTC_SmoothCalibPeriod : Select the Smooth Calibration Period.
*         This parameter can be one parameter from
*         the @ref RTC_SmoothCalibPeriod_TypeDef enumeration.
* @param  RTC_SmoothCalibPlusPulses : Select to Set or reset the CALP bit.
*         This parameter can be one parameter from the
*         @ref RTC_SmoothCalibPlusPulses_TypeDef enumeration.
* @param  RTC_SmouthCalibMinusPulsesValue: Select the value of CALM[8:0] bits.
*         This parameter can be one any value from 0 to 0x01FF.
* @retval An ErrorStatus enumeration value:
*          - SUCCESS: RTC Calib registers are configured
*          - ERROR: RTC Calib registers are not configured
*/
ErrorStatus RTC_SmoothCalibConfig(RTC_SmoothCalibPeriod_TypeDef RTC_SmoothCalibPeriod,
                                  RTC_SmoothCalibPlusPulses_TypeDef RTC_SmoothCalibPlusPulses,
                                  uint16_t RTC_SmouthCalibMinusPulsesValue)
{
  ErrorStatus status = ERROR;
  uint16_t recalpfcount = 0;

  /* Check the parameters */
  assert_param(IS_RTC_SMOOTH_CALIB_PERIOD(RTC_SmoothCalibPeriod));
  assert_param(IS_RTC_SMOOTH_CALIB_PLUS(RTC_SmoothCalibPlusPulses));
  assert_param(IS_RTC_SMOOTH_CALIB_MINUS(RTC_SmouthCalibMinusPulsesValue));

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

  /* check if a calibration is pending*/
  if ((RTC->ISR1 & RTC_ISR1_RECALPF) != RESET)
  {
    /* wait until the Calibration is completed*/
    while (((RTC->ISR1 & RTC_ISR1_RECALPF) != RESET) && (recalpfcount != RECALPF_TIMEOUT))
    {
      recalpfcount++;
    }
  }

  /* check if the calibration pending is completed or if there is 
     no calibration operation at all*/
  if ((RTC->ISR1 & RTC_ISR1_RECALPF) == RESET)
  {
    /* Configure the Smooth calibration settings */
    RTC->CALRH = (uint8_t)((uint8_t)((uint8_t)RTC_SmoothCalibPeriod | \
                           (uint8_t)RTC_SmoothCalibPlusPulses) | \
                           (uint8_t)((uint16_t)RTC_SmouthCalibMinusPulsesValue >> 8));
    RTC->CALRL = (uint8_t)(RTC_SmouthCalibMinusPulsesValue);

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

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

  return (ErrorStatus)(status);
}


/**
  * @}
  */

/** @defgroup RTC_Group9 Digital Calibration configuration functions
 *  @brief   Digital Calibration configuration functions 
 *
@verbatim   
 ===============================================================================
                  Calibration configuration functions
 ===============================================================================  

@endverbatim
  * @{
  */
/**
* @brief  Configure the Calibration Pin-out (RTC_CALIB) Selection (1Hz or 512Hz).
* @param  RTC_CalibOutput : Select the Calibration output Selection .
*         This parameter can be one parameter from the
*         @ref RTC_CalibOutput_TypeDef enumeration.
* @retval None
*/
void RTC_CalibOutputConfig(RTC_CalibOutput_TypeDef RTC_CalibOutput)
{
  /* Check the parameters */
  assert_param(IS_RTC_CALOUTPUT_SELECT(RTC_CalibOutput));

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

  if (RTC_CalibOutput != RTC_CalibOutput_512Hz)
  {
    /* Enable the RTC clock output */
    RTC->CR3 |= (uint8_t)RTC_CR3_COSEL;
  }
  else
  {
    /* Disable the RTC clock output */
    RTC->CR3 &= (uint8_t)~RTC_CR3_COSEL;
  }

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

/**
* @brief  Enables or disables the RTC clock to be output through the relative pin.
* @param  NewState: new state of the RTC Calibration output
  *         This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void RTC_CalibOutputCmd(FunctionalState NewState)
{
  /* 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 RTC clock output */
    RTC->CR3 |= (uint8_t)RTC_CR3_COE;
  }
  else
  {
    /* Disable the RTC clock output */
    RTC->CR3 &= (uint8_t)~RTC_CR3_COE;
  }

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


/**
  * @}
  */

/** @defgroup RTC_Group10 Tampers configuration functions
 *  @brief   Tampers configuration functions 
 *
@verbatim   
 ===============================================================================
                       Tampers configuration functions
 ===============================================================================  

@endverbatim
  * @{
  */

/**
* @brief  Configures the Tamper Sensitive Level.
* @param  RTC_Tamper: Select the tamper to configure.
*         This parameter can be one parameter from the @ref RTC_Tamper_TypeDef
*         enumeration.
* @param  RTC_TamperLevel: Select the tamper Sensitive Level.
*         This parameter can be one parameter from the 
*         @ref RTC_TamperLevel_TypeDef enumeration.
* @retval None
*/
void RTC_TamperLevelConfig(RTC_Tamper_TypeDef RTC_Tamper,
                           RTC_TamperLevel_TypeDef RTC_TamperLevel)
{
  /* Check the parameters */
  assert_param(IS_RTC_TAMPER((uint8_t)RTC_Tamper));
  assert_param(IS_RTC_TAMPER_LEVEL(RTC_TamperLevel));

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

  if (RTC_TamperLevel != RTC_TamperLevel_Low)
  {
    /* Enable the selected Tampers */
    RTC->TCR1 |= (uint8_t)(RTC_Tamper << 1);
  }
  else
  {
    /* Disable the selected Tampers */
    RTC->TCR1 &= (uint8_t)~(uint8_t)(RTC_Tamper << 1);
  }

  /* Enable the write protection for RTC registers */
  RTC->WPR = 0xFF; 
}
/**
  * @brief  Configures the Tampers Filter.
  * @param  RTC_TamperFilter: Specifies the tampers filter.
  *         This parameter can be one parameter from the
  *         @ref RTC_TamperFilter_TypeDef enumeration.
  * @retval None
  */
void RTC_TamperFilterConfig(RTC_TamperFilter_TypeDef RTC_TamperFilter)
{

  /* Check the parameters */
  assert_param(IS_RTC_TAMPER_FILTER(RTC_TamperFilter));

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

  /*clear flags before configuration */
  RTC->TCR2 &= (uint8_t)~(RTC_TCR2_TAMPFLT);

  /* Configure the RTC_TCR register */
  RTC->TCR2 |= (uint8_t)RTC_TamperFilter;

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

}

/**
  * @brief  Configures the Tampers Sampling Frequency.
  * @param  RTC_TamperSamplingFreq: Specifies the tampers Sampling Frequency.
  *         This parameter can be one para

⌨️ 快捷键说明

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