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

📄 stm8s_tim2.c

📁 STM8全部资料
💻 C
📖 第 1 页 / 共 4 页
字号:
  }

  /* Select the Opposite Input */
  if (TIM2_ICSelection == TIM2_ICSELECTION_DIRECTTI)
  {
    icselection = (u8)TIM2_ICSELECTION_INDIRECTTI;
  }
  else
  {
    icselection = (u8)TIM2_ICSELECTION_DIRECTTI;
  }

  if (TIM2_Channel == TIM2_CHANNEL_1)
  {
    /* TI1 Configuration */
    TI1_Config(TIM2_ICPolarity, TIM2_ICSelection,
               TIM2_ICFilter);

    /* Set the Input Capture Prescaler value */
    TIM2_SetIC1Prescaler(TIM2_ICPrescaler);

    /* TI2 Configuration */
    TI2_Config(icpolarity, icselection, TIM2_ICFilter);

    /* Set the Input Capture Prescaler value */
    TIM2_SetIC2Prescaler(TIM2_ICPrescaler);
  }
  else
  {
    /* TI2 Configuration */
    TI2_Config(TIM2_ICPolarity, TIM2_ICSelection,
               TIM2_ICFilter);

    /* Set the Input Capture Prescaler value */
    TIM2_SetIC2Prescaler(TIM2_ICPrescaler);

    /* TI1 Configuration */
    TI1_Config(icpolarity, icselection, TIM2_ICFilter);

    /* Set the Input Capture Prescaler value */
    TIM2_SetIC1Prescaler(TIM2_ICPrescaler);
  }
}


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

  /* set or Reset the CEN Bit */
  if (NewState != DISABLE)
  {
    TIM2->CR1 |= TIM2_CR1_CEN;
  }
  else
  {
    TIM2->CR1 &= (u8)(~TIM2_CR1_CEN);
  }
}


/**
  * @brief Enables or disables the specified TIM2 interrupts.
  * @param[in] NewState new state of the TIM2 peripheral.
  * This parameter can be: ENABLE or DISABLE.
  * @param[in] TIM2_IT specifies the TIM2 interrupts sources to be enabled or disabled.
  * This parameter can be any combination of the following values:
  *                       - TIM2_IT_UPDATE: TIM2 update Interrupt source
  *                       - TIM2_IT_CC1: TIM2 Capture Compare 1 Interrupt source
  *                       - TIM2_IT_CC2: TIM2 Capture Compare 2 Interrupt source
  *                       - TIM2_IT_CC3: TIM2 Capture Compare 3 Interrupt source
  * @param[in] NewState new state of the TIM2 peripheral.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Enable the the TIM2_IT_UPDATE interrupt.
  * @code
  * TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);
  * @endcode
  */
void TIM2_ITConfig(TIM2_IT_TypeDef TIM2_IT, FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_TIM2_IT_OK(TIM2_IT));
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));

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


/**
  * @brief Enables or Disables the TIM2 Update event.
  * @param[in] NewState new state of the TIM2 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 TIM2 Update event.
  * @code
  * TIM2_UpdateDisableConfig(ENABLE);
  * @endcode
  */
void TIM2_UpdateDisableConfig(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));

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

/**
  * @brief Selects the TIM2 Update Request Interrupt source.
  * @param[in] TIM2_UpdateSource specifies the Update source.
  * This parameter can be one of the following values
  *                       - TIM2_UPDATESOURCE_REGULAR
  *                       - TIM2_UPDATESOURCE_GLOBAL
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Select the TIM2 Update Request Interrupt source
  * @code
  * TIM2_UpdateRequestConfig(TIM2_UPDATESOURCE_GLOBAL);
  * @endcode
  */
void TIM2_UpdateRequestConfig(TIM2_UpdateSource_TypeDef TIM2_UpdateSource)
{
  /* Check the parameters */
  assert_param(IS_TIM2_UPDATE_SOURCE_OK(TIM2_UpdateSource));

  /* Set or Reset the URS Bit */
  if (TIM2_UpdateSource != TIM2_UPDATESOURCE_GLOBAL)
  {
    TIM2->CR1 |= TIM2_CR1_URS;
  }
  else
  {
    TIM2->CR1 &= (u8)(~TIM2_CR1_URS);
  }
}


/**
  * @brief Selects the TIM2抯 One Pulse Mode.
  * @param[in] TIM2_OPMode specifies the OPM Mode to be used.
  * This parameter can be one of the following values
  *                    - TIM2_OPMODE_SINGLE
  *                    - TIM2_OPMODE_REPETITIVE
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Select the TIM2 single One Pulse Mode
  * @code
  * TIM2_SelectOnePulseMode(TIM2_OPMODE_SINGLE);
  * @endcode
  */
void TIM2_SelectOnePulseMode(TIM2_OPMode_TypeDef TIM2_OPMode)
{
  /* Check the parameters */
  assert_param(IS_TIM2_OPM_MODE_OK(TIM2_OPMode));

  /* Set or Reset the OPM Bit */
  if (TIM2_OPMode != TIM2_OPMODE_REPETITIVE)
  {
    TIM2->CR1 |= TIM2_CR1_OPM;
  }
  else
  {
    TIM2->CR1 &= (u8)(~TIM2_CR1_OPM);
  }

}


/**
  * @brief Configures the TIM2 Prescaler.
  * @param[in] Prescaler specifies the Prescaler Register value
  * This parameter can be one of the following values
  *                       -  TIM2_PRESCALER_1
  *                       -  TIM2_PRESCALER_2
  *                       -  TIM2_PRESCALER_4
  *                       -  TIM2_PRESCALER_8
  *                       -  TIM2_PRESCALER_16
  *                       -  TIM2_PRESCALER_32
  *                       -  TIM2_PRESCALER_64
  *                       -  TIM2_PRESCALER_128
  *                       -  TIM2_PRESCALER_256
  *                       -  TIM2_PRESCALER_512
  *                       -  TIM2_PRESCALER_1024
  *                       -  TIM2_PRESCALER_2048
  *                       -  TIM2_PRESCALER_4096
  *                       -  TIM2_PRESCALER_8192
  *                       -  TIM2_PRESCALER_16384
  *                       -  TIM2_PRESCALER_32768
  * @param[in] TIM2_PSCReloadMode specifies the TIM2 Prescaler Reload mode.
  * This parameter can be one of the following values
  *                       - TIM2_PSCRELOADMODE_IMMEDIATE: The Prescaler is loaded
  *                         immediately.
  *                       - TIM2_PSCRELOADMODE_UPDATE: The Prescaler is loaded at
  *                         the update event.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Configures the TIM2 Prescaler.
  * @code
  * TIM2_PrescalerConfig(TIM2_PRESCALER_1, TIM2_PSCRELOADMODE_UPDATE);
  * @endcode
  */
void TIM2_PrescalerConfig(TIM2_Prescaler_TypeDef Prescaler,
                          TIM2_PSCReloadMode_TypeDef TIM2_PSCReloadMode)
{
  /* Check the parameters */
  assert_param(IS_TIM2_PRESCALER_RELOAD_OK(TIM2_PSCReloadMode));
  assert_param(IS_TIM2_PRESCALER_OK(Prescaler));

  /* Set the Prescaler value */
  TIM2->PSCR = Prescaler;

  /* Set or reset the UG Bit */
  TIM2->EGR = TIM2_PSCReloadMode;
}

/**
  * @brief Forces the TIM2 Channel1 output waveform to active or inactive level.
  * @param[in] TIM2_ForcedAction specifies the forced Action to be set to the output waveform.
  * This parameter can be one of the following values:
  *                       - TIM2_FORCEDACTION_ACTIVE: Force active level on OC1REF
  *                       - TIM2_FORCEDACTION_INACTIVE: Force inactive level on
  *                         OC1REF.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Force the TIM2 Channel1 output waveform to active or inactive level.
  * @code
  * TIM2_ForcedOC1Config(TIM2_FORCEDACTION_ACTIVE);
  * @endcode
  */
void TIM2_ForcedOC1Config(TIM2_ForcedAction_TypeDef TIM2_ForcedAction)
{
  /* Check the parameters */
  assert_param(IS_TIM2_FORCED_ACTION_OK(TIM2_ForcedAction));

  /* Reset the OCM Bits & Configure the Forced output Mode */
  TIM2->CCMR1  =  (u8)((TIM2->CCMR1 & (u8)(~TIM2_CCMR_OCM))  | (u8)TIM2_ForcedAction);
}

/**
  * @brief Forces the TIM2 Channel2 output waveform to active or inactive level.
  * @param[in] TIM2_ForcedAction specifies the forced Action to be set to the output waveform.
  * This parameter can be one of the following values:
  *                       - TIM2_FORCEDACTION_ACTIVE: Force active level on OC2REF
  *                       - TIM2_FORCEDACTION_INACTIVE: Force inactive level on
  *                         OC2REF.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Force the TIM2 Channel2 output waveform to active or inactive level.
  * @code
  * TIM2_ForcedOC2Config(TIM2_FORCEDACTION_ACTIVE);
  * @endcode
  */
void TIM2_ForcedOC2Config(TIM2_ForcedAction_TypeDef TIM2_ForcedAction)
{
  /* Check the parameters */
  assert_param(IS_TIM2_FORCED_ACTION_OK(TIM2_ForcedAction));

  /* Reset the OCM Bits & Configure the Forced output Mode */
  TIM2->CCMR2 = (u8)((TIM2->CCMR2 & (u8)(~TIM2_CCMR_OCM))  | (u8)TIM2_ForcedAction);
}

/**
  * @brief Forces the TIM2 Channel3 output waveform to active or inactive level.
  * @param[in] TIM2_ForcedAction specifies the forced Action to be set to the output waveform.
  * This parameter can be one of the following values:
  *                       - TIM2_FORCEDACTION_ACTIVE: Force active level on OC3REF
  *                       - TIM2_FORCEDACTION_INACTIVE: Force inactive level on
  *                         OC3REF.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Force the TIM2 Channel3 output waveform to active or inactive level.
  * @code
  * TIM2_ForcedOC3Config(TIM2_FORCEDACTION_ACTIVE);
  * @endcode
  */
void TIM2_ForcedOC3Config(TIM2_ForcedAction_TypeDef TIM2_ForcedAction)
{
  /* Check the parameters */
  assert_param(IS_TIM2_FORCED_ACTION_OK(TIM2_ForcedAction));

  /* Reset the OCM Bits & Configure the Forced output Mode */
  TIM2->CCMR3  =  (u8)((TIM2->CCMR3 & (u8)(~TIM2_CCMR_OCM))  | (u8)TIM2_ForcedAction);
}


/**
  * @brief Enables or disables TIM2 peripheral Preload register on ARR.
  * @param[in] NewState new state of the TIM2 peripheral Preload register.
  * This parameter can be ENABLE or DISABLE.
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * None
  * @par Example:
  * Enable TIM2 peripheral Preload register on ARR.
  * @code
  * TIM2_ARRPreloadConfig(ENABLE);
  * @endcode
  */
void TIM2_ARRPreloadConfig(FunctionalState NewState)
{
  /* Check the parameters */
  assert_param(IS_FUNCTIONALSTATE_OK(NewState));

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


/**
  * @brief Enables or disables the TIM2 peripheral Preload Register on CCR1.

⌨️ 快捷键说明

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