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

📄 stm8s_tim1.c

📁 STM8全部资料
💻 C
📖 第 1 页 / 共 5 页
字号:
  * @endcode
  */
void TIM1_ITConfig(TIM1_IT_TypeDef  TIM1_IT, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_TIM1_IT_OK(TIM1_IT));
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));

  if (NewState != DISABLE)
  {
    /* Enable the Interrupt sources */
    TIM1->IER |= (u8)TIM1_IT;
  }
  else
  {
    /* Disable the Interrupt sources */
    TIM1->IER &= (u8)(~(u8)TIM1_IT);
  }
}


/**
  * @brief Configures the TIM1 internal Clock.
  * @param[in] :
  * None
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Disable slave mode to clock the prescaler directly with the internal clock.
  * @code
  * TIM1_InternalClockConfig();
  * @endcode
  */
void TIM1_InternalClockConfig(void)
{
  /* Disable slave mode to clock the prescaler directly with the internal clock */
  TIM1->SMCR &= (u8)(~TIM1_SMCR_SMS);
}


/**
  * @brief Configures the TIM1 External clock Mode1.
  * @param[in] TIM1_ExtTRGPrescaler specifies the external Trigger Prescaler.
  * This parameter can be one of the following values:
  *                       - TIM1_EXTTRGPSC_OFF
  *                       - TIM1_EXTTRGPSC_DIV2
  *                       - TIM1_EXTTRGPSC_DIV4
  *                       - TIM1_EXTTRGPSC_DIV8.
  * @param[in] TIM1_ExtTRGPolarity specifies the external Trigger Polarity.
  * This parameter can be one of the following values:
  *                       - TIM1_EXTTRGPOLARITY_INVERTED
  *                       - TIM1_EXTTRGPOLARITY_NONINVERTED
  * @param[in] ExtTRGFilter specifies the External Trigger Filter.
  * This parameter must be a value between 0x00 and 0x0F
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * TIM1_ETRConfig
  * @par Example:
  * Configure the TIM1 External clock Mode1
  * @code
  * TIM1_ETRClockMode1Config(TIM1_EXTTRGPSC_DIV2, TIM1_EXTTRGPOLARITY_INVERTED,0x0F);
  * @endcode
  */
void TIM1_ETRClockMode1Config(TIM1_ExtTRGPSC_TypeDef TIM1_ExtTRGPrescaler,
                              TIM1_ExtTRGPolarity_TypeDef TIM1_ExtTRGPolarity,
                              u8 ExtTRGFilter)
{
  /* Check the parameters */
  assert_param(IS_TIM1_EXT_PRESCALER_OK(TIM1_ExtTRGPrescaler));
  assert_param(IS_TIM1_EXT_POLARITY_OK(TIM1_ExtTRGPolarity));

  /* Configure the ETR Clock source */
  TIM1_ETRConfig(TIM1_ExtTRGPrescaler, TIM1_ExtTRGPolarity, ExtTRGFilter);

  /* Select the External clock mode1 & Select the Trigger selection : ETRF */
  TIM1->SMCR = (u8)((TIM1->SMCR & (u8)(~(TIM1_SMCR_SMS | TIM1_SMCR_TS ))) | (u8)(  TIM1_SLAVEMODE_EXTERNAL1 | TIM1_TS_ETRF ));
}


/**
  * @brief Configures the TIM1 External clock Mode2.
  * @param[in] TIM1_ExtTRGPrescaler specifies the external Trigger Prescaler.
  * This parameter can be one of the following values:
  *                       - TIM1_EXTTRGPSC_OFF
  *                       - TIM1_EXTTRGPSC_DIV2
  *                       - TIM1_EXTTRGPSC_DIV4
  *                       - TIM1_EXTTRGPSC_DIV8.
  * @param[in] TIM1_ExtTRGPolarity specifies the external Trigger Polarity.
  * This parameter can be one of the following values:
  *                       - TIM1_EXTTRGPOLARITY_INVERTED
  *                       - TIM1_EXTTRGPOLARITY_NONINVERTED
  * @param[in] ExtTRGFilter specifies the External Trigger Filter.
  * This parameter must be a value between 0x00 and 0x0F
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * TIM1_ETRConfig
  * @par Example:
  * Configure the TIM1 External clock Mode2
  * @code
  * TIM1_ETRClockMode2Config(TIM1_EXTTRGPSC_DIV2, TIM1_EXTTRGPOLARITY_INVERTED,0x0F);
  * @endcode
  */
void TIM1_ETRClockMode2Config(TIM1_ExtTRGPSC_TypeDef TIM1_ExtTRGPrescaler,
                              TIM1_ExtTRGPolarity_TypeDef TIM1_ExtTRGPolarity,
                              u8 ExtTRGFilter)
{
  /* Check the parameters */
  assert_param(IS_TIM1_EXT_PRESCALER_OK(TIM1_ExtTRGPrescaler));
  assert_param(IS_TIM1_EXT_POLARITY_OK(TIM1_ExtTRGPolarity));

  /* Configure the ETR Clock source */
  TIM1_ETRConfig(TIM1_ExtTRGPrescaler, TIM1_ExtTRGPolarity, ExtTRGFilter);

  /* Enable the External clock mode2 */
  TIM1->ETR |= TIM1_ETR_ECE;
}


/**
  * @brief Configures the TIM1 External Trigger.
  * @param[in] TIM1_ExtTRGPrescaler specifies the external Trigger Prescaler.
  * This parameter can be one of the following values:
  *                       - TIM1_EXTTRGPSC_OFF
  *                       - TIM1_EXTTRGPSC_DIV2
  *                       - TIM1_EXTTRGPSC_DIV4
  *                       - TIM1_EXTTRGPSC_DIV8.
  * @param[in] TIM1_ExtTRGPolarity specifies the external Trigger Polarity.
  * This parameter can be one of the following values:
  *                       - TIM1_EXTTRGPOLARITY_INVERTED
  *                       - TIM1_EXTTRGPOLARITY_NONINVERTED
  * @param[in] ExtTRGFilter specifies the External Trigger Filter.
  * This parameter must be a value between 0x00 and 0x0F
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Configure the TIM1 External Trigger
  * @code
  * TIM1_ETRConfig(TIM1_EXTTRGPSC_DIV2, TIM1_EXTTRGPOLARITY_INVERTED,0x0F);
  * @endcode
  */
void TIM1_ETRConfig(TIM1_ExtTRGPSC_TypeDef TIM1_ExtTRGPrescaler,
                    TIM1_ExtTRGPolarity_TypeDef TIM1_ExtTRGPolarity,
                    u8 ExtTRGFilter)
{
  /* Set the Prescaler, the Filter value and the Polarity */
  TIM1->ETR |= (u8)((u8)TIM1_ExtTRGPrescaler |
                    (u8)TIM1_ExtTRGPolarity  |
                    (u8)ExtTRGFilter          );
}


/**
  * @brief Configures the TIM1 Trigger as External Clock.
  * @param[in] TIM1_TIxExternalCLKSource specifies Trigger source.
  * This parameter can be one of the following values:
  *                       - TIM1_TIXEXTERNALCLK1SOURCE_TI1: TI1 Edge Detector
  *                       - TIM1_TIXEXTERNALCLK1SOURCE_TI2: Filtered TIM1 Input 1
  *                       - TIM1_TIXEXTERNALCLK1SOURCE_TI1ED: Filtered TIM1 Input 2
  * @param[in] TIM1_ICPolarity specifies the TIx Polarity.
  * This parameter can be:
  *                       - TIM1_ICPOLARITY_RISING
  *                       - TIM1_ICPOLARITY_FALLING
  * @param[in] ICFilter specifies the filter value.
  * This parameter must be a value between 0x00 and 0x0F
  * @retval void None
  * @par Required preconditions:
  * TI1_Config
  * TI2_Config
  * TIM1_SelectInputTrigger
  * @par Called functions:
  * None
  * @par Example:
  * Configure the TIM1 Internal Trigger as External Clock
  * @code
  * TIM1_TIxExternalClockConfig(TIM1_TIXEXTERNALCLK1SOURCE_TI1ED, TIM1_ICPOLARITY_RISING, 0x0F);
  * @endcode
  */
void TIM1_TIxExternalClockConfig(TIM1_TIxExternalCLK1Source_TypeDef TIM1_TIxExternalCLKSource,
                                 TIM1_ICPolarity_TypeDef TIM1_ICPolarity,
                                 u8 ICFilter)
{
  /* Check the parameters */
  assert_param(IS_TIM1_TIXCLK_SOURCE_OK(TIM1_TIxExternalCLKSource));
  assert_param(IS_TIM1_IC_POLARITY_OK(TIM1_ICPolarity));
  assert_param(IS_TIM1_IC_FILTER_OK(ICFilter));

  /* Configure the TIM1 Input Clock Source */
  if (TIM1_TIxExternalCLKSource == TIM1_TIXEXTERNALCLK1SOURCE_TI2)
  {
    TI2_Config(TIM1_ICPolarity, TIM1_ICSELECTION_DIRECTTI, ICFilter);
  }
  else
  {
    TI1_Config(TIM1_ICPolarity, TIM1_ICSELECTION_DIRECTTI, ICFilter);
  }

  /* Select the Trigger source */
  TIM1_SelectInputTrigger(TIM1_TIxExternalCLKSource);

  /* Select the External clock mode1 */
  TIM1->SMCR |= (u8)(TIM1_SLAVEMODE_EXTERNAL1);
}

/**
  * @brief Selects the TIM1 Input Trigger source.
  * @param[in] TIM1_InputTriggerSource specifies Input Trigger source.
  * This parameter can be one of the following values:
  *                       - TIM1_TS_TI1F_ED: TI1 Edge Detector
  *                       - TIM1_TS_TI1FP1: Filtered Timer Input 1
  *                       - TIM1_TS_TI2FP2: Filtered Timer Input 2
  *                       - TIM1_TS_ETRF: External Trigger input
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Select the TIM1 Input Trigger source
  * @code
  * TIM1_SelectInputTrigger(TIM1_TS_TI1FP1);
  * @endcode
  */
void TIM1_SelectInputTrigger(TIM1_TS_TypeDef TIM1_InputTriggerSource)
{
  /* Check the parameters */
  assert_param(IS_TIM1_TRIGGER_SELECTION_OK(TIM1_InputTriggerSource));

  /* Select the Tgigger Source */
  TIM1->SMCR = (u8)((TIM1->SMCR & (u8)(~TIM1_SMCR_TS)) | (u8)TIM1_InputTriggerSource);
}


/**
  * @brief Enables or Disables the TIM1 Update event.
  * @param[in] NewState new state of the TIM1 peripheral Preload register. This parameter can
  * be ENABLE or DISABLE.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Enable the TIM1 Update event.
  * @code
  * TIM1_UpdateDisableConfig(ENABLE);
  * @endcode
  */

void TIM1_UpdateDisableConfig(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));

  /* Set or Reset the UDIS Bit */
  if (NewState != DISABLE)
  {
    TIM1->CR1 |= TIM1_CR1_UDIS;
  }
  else
  {
    TIM1->CR1 &= (u8)(~TIM1_CR1_UDIS);
  }
}

/**
  * @brief Selects the TIM1 Update Request Interrupt source.
  * @param[in] TIM1_UpdateSource specifies the Update source.
  * This parameter can be one of the following values
  *                       - TIM1_UPDATESOURCE_REGULAR
  *                       - TIM1_UPDATESOURCE_GLOBAL
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Select the TIM1 Update Request Interrupt source
  * @code
  * TIM1_UpdateRequestConfig(TIM1_UPDATESOURCE_GLOBAL);
  * @endcode
  */
void TIM1_UpdateRequestConfig(TIM1_UpdateSource_TypeDef TIM1_UpdateSource)
{
  /* Check the parameters */
  assert_param(IS_TIM1_UPDATE_SOURCE_OK(TIM1_UpdateSource));

  /* Set or Reset the URS Bit */
  if (TIM1_UpdateSource != TIM1_UPDATESOURCE_GLOBAL)
  {
    TIM1->CR1 |= TIM1_CR1_URS;
  }
  else
  {
    TIM1->CR1 &= (u8)(~TIM1_CR1_URS);
  }
}


/**
  * @brief Enables or Disables the TIM1抯 Hall sensor interface.
  * @param[in] NewState new state of the TIM1 Hall sensor interface.This parameter can
  * be ENABLE or DISABLE.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Enable the TIM1抯 Hall sensor interface.
  * @code
  * TIM1_SelectHallSensor(ENABLE);
  * @endcode
  */
void TIM1_SelectHallSensor(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));

  /* Set or Reset the TI1S Bit */
  if (NewState != DISABLE)
  {
    TIM1->CR2 |= TIM1_CR2_TI1S;
  }
  else
  {
    TIM1->CR2 &= (u8)(~TIM1_CR2_TI1S);
  }
}


/**
  * @brief Selects the TIM1抯 One Pulse Mode.
  * @param[in] TIM1_OPMode specifies the OPM Mode to be used.
  * This parameter can be one of the following values
  *                    - TIM1_OPMODE_SINGLE
  *                    - TIM1_OPMODE_REPETITIVE
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Select the TIM1 single One Pulse Mode
  * @code
  * TIM1_SelectOnePulseMode(TIM1_OPMODE_SINGLE);
  * @endcode
  */
void TIM1_SelectOnePulseMode(TIM1_OPMode_TypeDef TIM1_OPMode)
{
  /* Check the parameters */
  assert_param(IS_TIM1_OPM_MODE_OK(TIM1_OPMode));

  /* Set or Reset the OPM Bit */
  if (TIM1_OPMode != TIM1_OPMODE_REPETITIVE)
  {
    TIM1->CR1 |= TIM1_CR1_OPM;
  }
  else
  {
    TIM1->CR1 &= (u8)(~TIM1_CR1_OPM);
  }

}

⌨️ 快捷键说明

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