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

📄 stm8s_tim1.c

📁 STM8s
💻 C
📖 第 1 页 / 共 5 页
字号:
  * @param[in]  TIM1_Channel specifies the input capture channel from TIM1_Channel_TypeDef.
  * @param[in]  TIM1_ICPolarity specifies the Input capture polarity from  TIM1_ICPolarity_TypeDef .
  * @param[in] TIM1_ICSelection specifies the Input capture source selection  from  TIM1_ICSelection_TypeDef.
  * @param[in] TIM1_ICPrescaler specifies the Input capture Prescaler from  TIM1_ICPSC_TypeDef.
  * @param[in] TIM1_ICFilter specifies the Input capture filter value.
  * @retval None
  */

void TIM1_ICInit(TIM1_Channel_TypeDef TIM1_Channel,
                 TIM1_ICPolarity_TypeDef TIM1_ICPolarity,
                 TIM1_ICSelection_TypeDef TIM1_ICSelection,
                 TIM1_ICPSC_TypeDef TIM1_ICPrescaler,
                 u8 TIM1_ICFilter)
{

    /* Check the parameters */
    assert_param(IS_TIM1_CHANNEL_OK(TIM1_Channel));
    assert_param(IS_TIM1_IC_POLARITY_OK(TIM1_ICPolarity));
    assert_param(IS_TIM1_IC_SELECTION_OK(TIM1_ICSelection));
    assert_param(IS_TIM1_IC_PRESCALER_OK(TIM1_ICPrescaler));
    assert_param(IS_TIM1_IC_FILTER_OK(TIM1_ICFilter));

    if (TIM1_Channel == TIM1_CHANNEL_1)
    {
        /* TI1 Configuration */
        TI1_Config((u8)TIM1_ICPolarity,
                   (u8)TIM1_ICSelection,
                   (u8)TIM1_ICFilter);
        /* Set the Input Capture Prescaler value */
        TIM1_SetIC1Prescaler(TIM1_ICPrescaler);
    }
    else if (TIM1_Channel == TIM1_CHANNEL_2)
    {
        /* TI2 Configuration */
        TI2_Config((u8)TIM1_ICPolarity,
                   (u8)TIM1_ICSelection,
                   (u8)TIM1_ICFilter);
        /* Set the Input Capture Prescaler value */
        TIM1_SetIC2Prescaler(TIM1_ICPrescaler);
    }
    else if (TIM1_Channel == TIM1_CHANNEL_3)
    {
        /* TI3 Configuration */
        TI3_Config((u8)TIM1_ICPolarity,
                   (u8)TIM1_ICSelection,
                   (u8)TIM1_ICFilter);
        /* Set the Input Capture Prescaler value */
        TIM1_SetIC3Prescaler(TIM1_ICPrescaler);
    }
    else
    {
        /* TI4 Configuration */
        TI4_Config((u8)TIM1_ICPolarity,
                   (u8)TIM1_ICSelection,
                   (u8)TIM1_ICFilter);
        /* Set the Input Capture Prescaler value */
        TIM1_SetIC4Prescaler(TIM1_ICPrescaler);
    }

}

/**
  * @brief Configures the TIM1 peripheral in PWM Input Mode according to the specified parameters.
  * @param[in]  TIM1_Channel specifies the input capture channel from TIM1_Channel_TypeDef.
  * @param[in]  TIM1_ICPolarity specifies the Input capture polarity from  TIM1_ICPolarity_TypeDef .
  * @param[in] TIM1_ICSelection specifies the Input capture source selection  from  TIM1_ICSelection_TypeDef.
  * @param[in] TIM1_ICPrescaler specifies the Input capture Prescaler from  TIM1_ICPSC_TypeDef.
  * @param[in] TIM1_ICFilter specifies the Input capture filter value.
  * @retval None
  */
void TIM1_PWMIConfig(TIM1_Channel_TypeDef TIM1_Channel,
                     TIM1_ICPolarity_TypeDef TIM1_ICPolarity,
                     TIM1_ICSelection_TypeDef TIM1_ICSelection,
                     TIM1_ICPSC_TypeDef TIM1_ICPrescaler,
                     u8 TIM1_ICFilter)
{
    u8 icpolarity = TIM1_ICPOLARITY_RISING;
    u8 icselection = TIM1_ICSELECTION_DIRECTTI;

    /* Check the parameters */
    assert_param(IS_TIM1_PWMI_CHANNEL_OK(TIM1_Channel));
    assert_param(IS_TIM1_IC_POLARITY_OK(TIM1_ICPolarity));
    assert_param(IS_TIM1_IC_SELECTION_OK(TIM1_ICSelection));
    assert_param(IS_TIM1_IC_PRESCALER_OK(TIM1_ICPrescaler));

    /* Select the Opposite Input Polarity */
    if (TIM1_ICPolarity != TIM1_ICPOLARITY_FALLING)
    {
        icpolarity = TIM1_ICPOLARITY_FALLING;
    }
    else
    {
        icpolarity = TIM1_ICPOLARITY_RISING;
    }

    /* Select the Opposite Input */
    if (TIM1_ICSelection == TIM1_ICSELECTION_DIRECTTI)
    {
        icselection = TIM1_ICSELECTION_INDIRECTTI;
    }
    else
    {
        icselection = TIM1_ICSELECTION_DIRECTTI;
    }

    if (TIM1_Channel == TIM1_CHANNEL_1)
    {
        /* TI1 Configuration */
        TI1_Config((u8)TIM1_ICPolarity, (u8)TIM1_ICSelection,
                   (u8)TIM1_ICFilter);

        /* Set the Input Capture Prescaler value */
        TIM1_SetIC1Prescaler(TIM1_ICPrescaler);

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

        /* Set the Input Capture Prescaler value */
        TIM1_SetIC2Prescaler(TIM1_ICPrescaler);
    }
    else
    {
        /* TI2 Configuration */
        TI2_Config((u8)TIM1_ICPolarity, (u8)TIM1_ICSelection,
                   (u8)TIM1_ICFilter);

        /* Set the Input Capture Prescaler value */
        TIM1_SetIC2Prescaler(TIM1_ICPrescaler);

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

        /* Set the Input Capture Prescaler value */
        TIM1_SetIC1Prescaler(TIM1_ICPrescaler);
    }
}


/**
  * @brief Enables or disables the TIM1 peripheral.
  * @param[in] NewState new state of the TIM1 peripheral.
	* This parameter can be ENABLE or DISABLE.
  * @retval None
  */
void TIM1_Cmd(FunctionalState NewState)
{
    /* Check the parameters */
    assert_param(IS_FUNCTIONALSTATE_OK(NewState));

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


/**
  * @brief Enables or disables the TIM1 peripheral Main Outputs.
  * @param[in] NewState new state of the TIM1 peripheral.
	* This parameter can be ENABLE or DISABLE.
  * @retval None
  */
void TIM1_CtrlPWMOutputs(FunctionalState NewState)
{
    /* Check the parameters */
    assert_param(IS_FUNCTIONALSTATE_OK(NewState));

    /* Set or Reset the MOE Bit */

    if (NewState != DISABLE)
    {
        TIM1->BKR |= TIM1_BKR_MOE;
    }
    else
    {
        TIM1->BKR &= (u8)(~TIM1_BKR_MOE);
    }
}


/**
  * @brief Enables or disables the specified TIM1 interrupts.
  * @param[in] NewState new state of the TIM1 peripheral.
  * This parameter can be: ENABLE or DISABLE.
  * @param[in] TIM1_IT specifies the TIM1 interrupts sources to be enabled or disabled.
  * This parameter can be any combination of the following values:
  *  - TIM1_IT_UPDATE: TIM1 update Interrupt source
  *  - TIM1_IT_CC1: TIM1 Capture Compare 1 Interrupt source
  *  - TIM1_IT_CC2: TIM1 Capture Compare 2 Interrupt source
  *  - TIM1_IT_CC3: TIM1 Capture Compare 3 Interrupt source
  *  - TIM1_IT_CC4: TIM1 Capture Compare 4 Interrupt source
  *  - TIM1_IT_CCUpdate: TIM1 Capture Compare Update Interrupt source
  *  - TIM1_IT_TRIGGER: TIM1 Trigger Interrupt source
  *  - TIM1_IT_BREAK: TIM1 Break Interrupt source
  * @param[in] NewState new state of the TIM1 peripheral.
  * @retval None
  */
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 None
  */
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 None
  */
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 None
  */
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 None
  */
void TIM1_ETRConfig(TIM1_ExtTRGPSC_TypeDef TIM1_ExtTRGPrescaler,
                    TIM1_ExtTRGPolarity_TypeDef TIM1_ExtTRGPolarity,
                    u8 ExtTRGFilter)
{
    /* Check the parameters */
    assert_param(IS_TIM1_EXT_TRG_FILTER_OK(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 None
  * @par Required preconditions:
  * TI1_Config
  * TI2_Config
  * TIM1_SelectInputTrigger
  */
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((u8)TIM1_ICPolarity, (u8)TIM1_ICSELECTION_DIRECTTI, (u8)ICFilter);
    }
    else
    {
        TI1_Config((u8)TIM1_ICPolarity, (u8)TIM1_ICSELECTION_DIRECTTI, (u8)ICFilter);
    }

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

    /* Select the External clock mode1 */

⌨️ 快捷键说明

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