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

📄 stm8s_tim5.c

📁 STM8s
💻 C
📖 第 1 页 / 共 3 页
字号:
/**
  ******************************************************************************
  * @file stm8s_tim5.c
  * @brief This file contains all the functions for the TIM5 peripheral.
  * @author STMicroelectronics - MCD Application Team
  * @version V1.1.1
  * @date 06/05/2009
  ******************************************************************************
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2009 STMicroelectronics</center></h2>
  * @image html logo.bmp
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "stm8s_tim5.h"


/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void TI1_Config(u8 TIM5_ICPolarity, u8 TIM5_ICSelection, u8 TIM5_ICFilter);
static void TI2_Config(u8 TIM5_ICPolarity, u8 TIM5_ICSelection, u8 TIM5_ICFilter);
static void TI3_Config(u8 TIM5_ICPolarity, u8 TIM5_ICSelection, u8 TIM5_ICFilter);
/**
  * @addtogroup TIM5_Public_Functions
  * @{
  */

/**
  * @brief Deinitializes the TIM5 peripheral registers to their default reset values.
  * @param[in] :
  * None
  * @retval None
  */
void TIM5_DeInit(void)
{

    TIM5->CR1 = (u8)TIM5_CR1_RESET_VALUE;
    TIM5->CR2 = TIM5_CR2_RESET_VALUE;
    TIM5->SMCR = TIM5_SMCR_RESET_VALUE;
    TIM5->IER = (u8)TIM5_IER_RESET_VALUE;
    TIM5->SR2 = (u8)TIM5_SR2_RESET_VALUE;

    /* Disable channels */
    TIM5->CCER1 = (u8)TIM5_CCER1_RESET_VALUE;
    TIM5->CCER2 = (u8)TIM5_CCER2_RESET_VALUE;


    /* Then reset channel registers: it also works if lock level is equal to 2 or 3 */
    TIM5->CCER1 = (u8)TIM5_CCER1_RESET_VALUE;
    TIM5->CCER2 = (u8)TIM5_CCER2_RESET_VALUE;
    TIM5->CCMR1 = (u8)TIM5_CCMR1_RESET_VALUE;
    TIM5->CCMR2 = (u8)TIM5_CCMR2_RESET_VALUE;
    TIM5->CCMR3 = (u8)TIM5_CCMR3_RESET_VALUE;
    TIM5->CNTRH = (u8)TIM5_CNTRH_RESET_VALUE;
    TIM5->CNTRL = (u8)TIM5_CNTRL_RESET_VALUE;
    TIM5->PSCR	= (u8)TIM5_PSCR_RESET_VALUE;
    TIM5->ARRH 	= (u8)TIM5_ARRH_RESET_VALUE;
    TIM5->ARRL 	= (u8)TIM5_ARRL_RESET_VALUE;
    TIM5->CCR1H = (u8)TIM5_CCR1H_RESET_VALUE;
    TIM5->CCR1L = (u8)TIM5_CCR1L_RESET_VALUE;
    TIM5->CCR2H = (u8)TIM5_CCR2H_RESET_VALUE;
    TIM5->CCR2L = (u8)TIM5_CCR2L_RESET_VALUE;
    TIM5->CCR3H = (u8)TIM5_CCR3H_RESET_VALUE;
    TIM5->CCR3L = (u8)TIM5_CCR3L_RESET_VALUE;
    TIM5->SR1 = (u8)TIM5_SR1_RESET_VALUE;
}


/**
  * @brief Initializes the TIM5 Time Base Unit according to the specified parameters.
  * @param[in]  TIM5_Prescaler specifies the Prescaler from TIM5_Prescaler_TypeDef.
  * @param[in]  TIM5_Period specifies the Period value.
  * @retval None
  */
void TIM5_TimeBaseInit( TIM5_Prescaler_TypeDef TIM5_Prescaler,
                        u16 TIM5_Period)
{
    /* Set the Prescaler value */
    TIM5->PSCR = (u8)(TIM5_Prescaler);
    /* Set the Autoreload value */
    TIM5->ARRH = (u8)(TIM5_Period >> 8) ;
    TIM5->ARRL = (u8)(TIM5_Period);
}


/**
  * @brief Initializes the TIM5 Channel1 according to the specified parameters.
  * @param[in] TIM5_OCMode specifies the Output Compare mode  from @ref TIM5_OCMode_TypeDef.
  * @param[in] TIM5_OutputState specifies the Output State  from @ref TIM5_OutputState_TypeDef.
  * @param[in] TIM5_Pulse specifies the Pulse width  value.
  * @param[in] TIM5_OCPolarity specifies the Output Compare Polarity  from @ref TIM5_OCPolarity_TypeDef.
  * @retval None
  */
void TIM5_OC1Init(TIM5_OCMode_TypeDef TIM5_OCMode,
                  TIM5_OutputState_TypeDef TIM5_OutputState,
                  u16 TIM5_Pulse,
                  TIM5_OCPolarity_TypeDef TIM5_OCPolarity)
{
    /* Check the parameters */
    assert_param(IS_TIM5_OC_MODE_OK(TIM5_OCMode));
    assert_param(IS_TIM5_OUTPUT_STATE_OK(TIM5_OutputState));
    assert_param(IS_TIM5_OC_POLARITY_OK(TIM5_OCPolarity));

    /* Disable the Channel 1: Reset the CCE Bit, Set the Output State , the Output Polarity */
    TIM5->CCER1 &= (u8)(~( TIM5_CCER1_CC1E | TIM5_CCER1_CC1P));
    /* Set the Output State &  Set the Output Polarity  */
    TIM5->CCER1 |= (u8)((TIM5_OutputState  & TIM5_CCER1_CC1E   )| (TIM5_OCPolarity   & TIM5_CCER1_CC1P   ));

    /* Reset the Output Compare Bits  & Set the Ouput Compare Mode */
    TIM5->CCMR1 = (u8)((TIM5->CCMR1 & (u8)(~TIM5_CCMR_OCM)) | (u8)TIM5_OCMode);

    /* Set the Pulse value */
    TIM5->CCR1H = (u8)(TIM5_Pulse >> 8);
    TIM5->CCR1L = (u8)(TIM5_Pulse);
}


/**
  * @brief Initializes the TIM5 Channel2 according to the specified parameters.
  * @param[in] TIM5_OCMode specifies the Output Compare mode  from @ref TIM5_OCMode_TypeDef.
  * @param[in] TIM5_OutputState specifies the Output State  from @ref TIM5_OutputState_TypeDef.
  * @param[in] TIM5_Pulse specifies the Pulse width  value.
  * @param[in] TIM5_OCPolarity specifies the Output Compare Polarity  from @ref TIM5_OCPolarity_TypeDef.
  * @retval None
  */
void TIM5_OC2Init(TIM5_OCMode_TypeDef TIM5_OCMode,
                  TIM5_OutputState_TypeDef TIM5_OutputState,
                  u16 TIM5_Pulse,
                  TIM5_OCPolarity_TypeDef TIM5_OCPolarity)
{
    /* Check the parameters */
    assert_param(IS_TIM5_OC_MODE_OK(TIM5_OCMode));
    assert_param(IS_TIM5_OUTPUT_STATE_OK(TIM5_OutputState));
    assert_param(IS_TIM5_OC_POLARITY_OK(TIM5_OCPolarity));


    /* Disable the Channel 1: Reset the CCE Bit, Set the Output State , the Output Polarity */
    TIM5->CCER1 &= (u8)(~( TIM5_CCER1_CC2E |  TIM5_CCER1_CC2P ));
    /* Set the Output State & Set the Output Polarity  */
    TIM5->CCER1 |= (u8)((TIM5_OutputState  & TIM5_CCER1_CC2E   )| \
                        (TIM5_OCPolarity   & TIM5_CCER1_CC2P   ));


    /* Reset the Output Compare Bits  & Set the Ouput Compare Mode */
    TIM5->CCMR2 = (u8)((TIM5->CCMR2 & (u8)(~TIM5_CCMR_OCM)) | (u8)TIM5_OCMode);


    /* Set the Pulse value */
    TIM5->CCR2H = (u8)(TIM5_Pulse >> 8);
    TIM5->CCR2L = (u8)(TIM5_Pulse);
}


/**
  * @brief Initializes the TIM5 Channel3 according to the specified parameters.
  * @param[in] TIM5_OCMode specifies the Output Compare mode from @ref TIM5_OCMode_TypeDef.
  * @param[in] TIM5_OutputState specifies the Output State from @ref TIM5_OutputState_TypeDef.
  * @param[in] TIM5_Pulse specifies the Pulse width value.
  * @param[in] TIM5_OCPolarity specifies the Output Compare Polarity  from @ref TIM5_OCPolarity_TypeDef.
  * @retval None
  */
void TIM5_OC3Init(TIM5_OCMode_TypeDef TIM5_OCMode,
                  TIM5_OutputState_TypeDef TIM5_OutputState,
                  u16 TIM5_Pulse,
                  TIM5_OCPolarity_TypeDef TIM5_OCPolarity)
{
    /* Check the parameters */
    assert_param(IS_TIM5_OC_MODE_OK(TIM5_OCMode));
    assert_param(IS_TIM5_OUTPUT_STATE_OK(TIM5_OutputState));
    assert_param(IS_TIM5_OC_POLARITY_OK(TIM5_OCPolarity));
    /* Disable the Channel 1: Reset the CCE Bit, Set the Output State, the Output Polarity */
    TIM5->CCER2 &= (u8)(~( TIM5_CCER2_CC3E  | TIM5_CCER2_CC3P));
    /* Set the Output State & Set the Output Polarity  */
    TIM5->CCER2 |= (u8)((TIM5_OutputState  & TIM5_CCER2_CC3E   )|  (TIM5_OCPolarity   & TIM5_CCER2_CC3P   ));

    /* Reset the Output Compare Bits  & Set the Ouput Compare Mode */
    TIM5->CCMR3 = (u8)((TIM5->CCMR3 & (u8)(~TIM5_CCMR_OCM)) | (u8)TIM5_OCMode);

    /* Set the Pulse value */
    TIM5->CCR3H = (u8)(TIM5_Pulse >> 8);
    TIM5->CCR3L = (u8)(TIM5_Pulse);

}


/**
  * @brief Initializes the TIM5 peripheral according to the specified parameters.
  * @param[in]  TIM5_Channel specifies the Input Capture Channel from @ref TIM5_Channel_TypeDef.
  * @param[in] TIM5_ICPolarity specifies the Input Capture Polarity from @ref TIM5_ICPolarity_TypeDef.
  * @param[in] TIM5_ICSelection specifies theInput Capture Selection from @ref TIM5_ICSelection_TypeDef.
  * @param[in] TIM5_ICPrescaler specifies the Input Capture Prescaler from @ref TIM5_ICPSC_TypeDef.
  * @param[in] TIM5_ICFilter specifies the Input Capture Filter value (value can be an integer from 0x00 to 0x0F).
  * @retval None
  */
void TIM5_ICInit(TIM5_Channel_TypeDef TIM5_Channel,
                 TIM5_ICPolarity_TypeDef TIM5_ICPolarity,
                 TIM5_ICSelection_TypeDef TIM5_ICSelection,
                 TIM5_ICPSC_TypeDef TIM5_ICPrescaler,
                 u8 TIM5_ICFilter)
{
    /* Check the parameters */
    assert_param(IS_TIM5_CHANNEL_OK(TIM5_Channel));
    assert_param(IS_TIM5_IC_POLARITY_OK(TIM5_ICPolarity));
    assert_param(IS_TIM5_IC_SELECTION_OK(TIM5_ICSelection));
    assert_param(IS_TIM5_IC_PRESCALER_OK(TIM5_ICPrescaler));
    assert_param(IS_TIM5_IC_FILTER_OK(TIM5_ICFilter));

    if (TIM5_Channel == TIM5_CHANNEL_1)
    {
        /* TI1 Configuration */
        TI1_Config((u8)TIM5_ICPolarity,
                   (u8)TIM5_ICSelection,
                   (u8)TIM5_ICFilter);

        /* Set the Input Capture Prescaler value */
        TIM5_SetIC1Prescaler(TIM5_ICPrescaler);
    }
    else if (TIM5_Channel == TIM5_CHANNEL_2)
    {
        /* TI2 Configuration */
        TI2_Config((u8)TIM5_ICPolarity,
                   (u8)TIM5_ICSelection,
                   (u8)TIM5_ICFilter);

        /* Set the Input Capture Prescaler value */
        TIM5_SetIC2Prescaler(TIM5_ICPrescaler);
    }
    else
    {
        /* TI3 Configuration */
        TI3_Config((u8)TIM5_ICPolarity,
                   (u8)TIM5_ICSelection,
                   (u8)TIM5_ICFilter);

        /* Set the Input Capture Prescaler value */
        TIM5_SetIC3Prescaler(TIM5_ICPrescaler);
    }
}


/**
  * @brief Configures the TIM5 peripheral in PWM Input Mode according to the specified parameters.
    * @param[in]  TIM5_Channel specifies the Input Capture Channel from @ref TIM5_Channel_TypeDef.
  * @param[in] TIM5_ICPolarity specifies the Input Capture Polarity from @ref TIM5_ICPolarity_TypeDef.
  * @param[in] TIM5_ICSelection specifies theInput Capture Selection from @ref TIM5_ICSelection_TypeDef.
  * @param[in] TIM5_ICPrescaler specifies the Input Capture Prescaler from @ref TIM5_ICPSC_TypeDef.
  * @param[in] TIM5_ICFilter specifies the Input Capture Filter value (value can be an integer from 0x00 to 0x0F).
  * @retval None
  */
void TIM5_PWMIConfig(TIM5_Channel_TypeDef TIM5_Channel,
                     TIM5_ICPolarity_TypeDef TIM5_ICPolarity,
                     TIM5_ICSelection_TypeDef TIM5_ICSelection,
                     TIM5_ICPSC_TypeDef TIM5_ICPrescaler,
                     u8 TIM5_ICFilter)
{
    u8 icpolarity = (u8)TIM5_ICPOLARITY_RISING;
    u8 icselection = (u8)TIM5_ICSELECTION_DIRECTTI;

    /* Check the parameters */
    assert_param(IS_TIM5_PWMI_CHANNEL_OK(TIM5_Channel));
    assert_param(IS_TIM5_IC_POLARITY_OK(TIM5_ICPolarity));
    assert_param(IS_TIM5_IC_SELECTION_OK(TIM5_ICSelection));
    assert_param(IS_TIM5_IC_PRESCALER_OK(TIM5_ICPrescaler));

    /* Select the Opposite Input Polarity */
    if (TIM5_ICPolarity != TIM5_ICPOLARITY_FALLING)
    {
        icpolarity = (u8)TIM5_ICPOLARITY_FALLING;
    }
    else
    {
        icpolarity = (u8)TIM5_ICPOLARITY_RISING;
    }

    /* Select the Opposite Input */
    if (TIM5_ICSelection == TIM5_ICSELECTION_DIRECTTI)
    {
        icselection = (u8)TIM5_ICSELECTION_INDIRECTTI;
    }
    else
    {
        icselection = (u8)TIM5_ICSELECTION_DIRECTTI;
    }

    if (TIM5_Channel == TIM5_CHANNEL_1)
    {
        /* TI1 Configuration */
        TI1_Config((u8)TIM5_ICPolarity, (u8)TIM5_ICSelection,
                   (u8)TIM5_ICFilter);

        /* Set the Input Capture Prescaler value */
        TIM5_SetIC1Prescaler(TIM5_ICPrescaler);

        /* TI2 Configuration */
        TI2_Config((u8)icpolarity, (u8)icselection, (u8)TIM5_ICFilter);

        /* Set the Input Capture Prescaler value */
        TIM5_SetIC2Prescaler(TIM5_ICPrescaler);
    }
    else
    {
        /* TI2 Configuration */
        TI2_Config((u8)TIM5_ICPolarity, (u8)TIM5_ICSelection,
                   (u8)TIM5_ICFilter);

        /* Set the Input Capture Prescaler value */
        TIM5_SetIC2Prescaler(TIM5_ICPrescaler);

        /* TI1 Configuration */
        TI1_Config((u8)icpolarity, (u8)icselection, (u8)TIM5_ICFilter);

        /* Set the Input Capture Prescaler value */
        TIM5_SetIC1Prescaler(TIM5_ICPrescaler);
    }
}


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

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


/**
  * @brief Enables or disables the specified TIM5 interrupts.
  * @param[in] NewState new state of the TIM5 peripheral.
  * This parameter can be: ENABLE or DISABLE.
  * @param[in] TIM5_IT specifies the TIM5 interrupts sources to be enabled or disabled.
  * This parameter can be any combination of the following values:
  *                       - TIM5_IT_UPDATE: TIM5 update Interrupt source
  *                       - TIM5_IT_CC1: TIM5 Capture Compare 1 Interrupt source
  *                       - TIM5_IT_CC2: TIM5 Capture Compare 2 Interrupt source
  *                       - TIM5_IT_CC3: TIM5 Capture Compare 3 Interrupt source
  * @param[in] NewState new state of the TIM5 peripheral.
  * @retval None
  */
void TIM5_ITConfig(TIM5_IT_TypeDef TIM5_IT, FunctionalState NewState)
{
    /* Check the parameters */

⌨️ 快捷键说明

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