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

📄 stm8s_tim2.c

📁 按键是比较复杂。可以用状态机表示。 每10mS执行一次键盘扫描任务 0、无键
💻 C
📖 第 1 页 / 共 3 页
字号:
/**
  ******************************************************************************
  * @file stm8s_tim2.c
  * @brief This file contains all the functions for the TIM2 peripheral.
  * @author STMicroelectronics - MCD Application Team
  * @version V1.1.0
  * @date 02/27/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_tim2.h"

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void TI1_Config(u8 TIM2_ICPolarity, u8 TIM2_ICSelection, u8 TIM2_ICFilter);
static void TI2_Config(u8 TIM2_ICPolarity, u8 TIM2_ICSelection, u8 TIM2_ICFilter);
static void TI3_Config(u8 TIM2_ICPolarity, u8 TIM2_ICSelection, u8 TIM2_ICFilter);
/**
  * @addtogroup TIM2_Public_Functions
  * @{
  */

/**
  * @brief Deinitializes the TIM2 peripheral registers to their default reset values.
  * @param[in] :
  * None
  * @retval None
  * @par Required preconditions:
  * None
  */
void TIM2_DeInit(void)
{

  TIM2->CR1 = (u8)TIM2_CR1_RESET_VALUE;
  TIM2->IER = (u8)TIM2_IER_RESET_VALUE;
  TIM2->SR2 = (u8)TIM2_SR2_RESET_VALUE;

  /* Disable channels */
  TIM2->CCER1 = (u8)TIM2_CCER1_RESET_VALUE;
  TIM2->CCER2 = (u8)TIM2_CCER2_RESET_VALUE;


  /* Then reset channel registers: it also works if lock level is equal to 2 or 3 */
  TIM2->CCER1 = (u8)TIM2_CCER1_RESET_VALUE;
  TIM2->CCER2 = (u8)TIM2_CCER2_RESET_VALUE;
  TIM2->CCMR1 = (u8)TIM2_CCMR1_RESET_VALUE;
  TIM2->CCMR2 = (u8)TIM2_CCMR2_RESET_VALUE;
  TIM2->CCMR3 = (u8)TIM2_CCMR3_RESET_VALUE;
  TIM2->CNTRH = (u8)TIM2_CNTRH_RESET_VALUE;
  TIM2->CNTRL = (u8)TIM2_CNTRL_RESET_VALUE;
  TIM2->PSCR = (u8)TIM2_PSCR_RESET_VALUE;
  TIM2->ARRH  = (u8)TIM2_ARRH_RESET_VALUE;
  TIM2->ARRL  = (u8)TIM2_ARRL_RESET_VALUE;
  TIM2->CCR1H = (u8)TIM2_CCR1H_RESET_VALUE;
  TIM2->CCR1L = (u8)TIM2_CCR1L_RESET_VALUE;
  TIM2->CCR2H = (u8)TIM2_CCR2H_RESET_VALUE;
  TIM2->CCR2L = (u8)TIM2_CCR2L_RESET_VALUE;
  TIM2->CCR3H = (u8)TIM2_CCR3H_RESET_VALUE;
  TIM2->CCR3L = (u8)TIM2_CCR3L_RESET_VALUE;
  TIM2->SR1 = (u8)TIM2_SR1_RESET_VALUE;
}


/**
  * @brief Initializes the TIM2 Time Base Unit according to the specified parameters.
  * @param[in]  TIM2_Prescaler specifies the Prescaler from TIM2_Prescaler_TypeDef.
  * @param[in]  TIM2_Period specifies the Period value.
  * @retval None
  * @par Required preconditions:
  * None
  */
void TIM2_TimeBaseInit( TIM2_Prescaler_TypeDef TIM2_Prescaler,
                        u16 TIM2_Period)
{
  /* Set the Prescaler value */
  TIM2->PSCR = (u8)(TIM2_Prescaler);
  /* Set the Autoreload value */
  TIM2->ARRH = (u8)(TIM2_Period >> 8);
  TIM2->ARRL = (u8)(TIM2_Period);
}


/**
  * @brief Initializes the TIM2 Channel1 according to the specified parameters.
  * @param[in] TIM2_OCMode specifies the Output Compare mode  from @ref TIM2_OCMode_TypeDef.
  * @param[in] TIM2_OutputState specifies the Output State  from @ref TIM2_OutputState_TypeDef.
  * @param[in] TIM2_Pulse specifies the Pulse width  value.
  * @param[in] TIM2_OCPolarity specifies the Output Compare Polarity  from @ref TIM2_OCPolarity_TypeDef.
  * @retval None
  * @par Required preconditions:
  * None
  */
void TIM2_OC1Init(TIM2_OCMode_TypeDef TIM2_OCMode,
                  TIM2_OutputState_TypeDef TIM2_OutputState,
                  u16 TIM2_Pulse,
                  TIM2_OCPolarity_TypeDef TIM2_OCPolarity)
{
  /* Check the parameters */
  assert_param(IS_TIM2_OC_MODE_OK(TIM2_OCMode));
  assert_param(IS_TIM2_OUTPUT_STATE_OK(TIM2_OutputState));
  assert_param(IS_TIM2_OC_POLARITY_OK(TIM2_OCPolarity));

  /* Disable the Channel 1: Reset the CCE Bit, Set the Output State , the Output Polarity */
  TIM2->CCER1 &= (u8)(~( TIM2_CCER1_CC1E | TIM2_CCER1_CC1P));
  /* Set the Output State &  Set the Output Polarity  */
  TIM2->CCER1 |= (u8)((TIM2_OutputState  & TIM2_CCER1_CC1E   ) | (TIM2_OCPolarity   & TIM2_CCER1_CC1P   ));

  /* Reset the Output Compare Bits  & Set the Ouput Compare Mode */
  TIM2->CCMR1 = (u8)((TIM2->CCMR1 & (u8)(~TIM2_CCMR_OCM)) | (u8)TIM2_OCMode);

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


/**
  * @brief Initializes the TIM2 Channel2 according to the specified parameters.
  * @param[in] TIM2_OCMode specifies the Output Compare mode  from @ref TIM2_OCMode_TypeDef.
  * @param[in] TIM2_OutputState specifies the Output State  from @ref TIM2_OutputState_TypeDef.
  * @param[in] TIM2_Pulse specifies the Pulse width  value.
  * @param[in] TIM2_OCPolarity specifies the Output Compare Polarity  from @ref TIM2_OCPolarity_TypeDef.
  * @retval None
  * @par Required preconditions:
  * None
  */
void TIM2_OC2Init(TIM2_OCMode_TypeDef TIM2_OCMode,
                  TIM2_OutputState_TypeDef TIM2_OutputState,
                  u16 TIM2_Pulse,
                  TIM2_OCPolarity_TypeDef TIM2_OCPolarity)
{
  /* Check the parameters */
  assert_param(IS_TIM2_OC_MODE_OK(TIM2_OCMode));
  assert_param(IS_TIM2_OUTPUT_STATE_OK(TIM2_OutputState));
  assert_param(IS_TIM2_OC_POLARITY_OK(TIM2_OCPolarity));


  /* Disable the Channel 1: Reset the CCE Bit, Set the Output State, the Output Polarity */
  TIM2->CCER1 &= (u8)(~( TIM2_CCER1_CC2E |  TIM2_CCER1_CC2P ));
  /* Set the Output State & Set the Output Polarity */
  TIM2->CCER1 |= (u8)((TIM2_OutputState  & TIM2_CCER1_CC2E   ) | \
                      (TIM2_OCPolarity   & TIM2_CCER1_CC2P   ));


  /* Reset the Output Compare Bits & Set the Output Compare Mode */
  TIM2->CCMR2 = (u8)((TIM2->CCMR2 & (u8)(~TIM2_CCMR_OCM)) | (u8)TIM2_OCMode);


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


/**
  * @brief Initializes the TIM2 Channel3 according to the specified parameters.
  * @param[in] TIM2_OCMode specifies the Output Compare mode from @ref TIM2_OCMode_TypeDef.
  * @param[in] TIM2_OutputState specifies the Output State from @ref TIM2_OutputState_TypeDef.
  * @param[in] TIM2_Pulse specifies the Pulse width value.
  * @param[in] TIM2_OCPolarity specifies the Output Compare Polarity  from @ref TIM2_OCPolarity_TypeDef.
  * @retval None
  * @par Required preconditions:
  * None
  */
void TIM2_OC3Init(TIM2_OCMode_TypeDef TIM2_OCMode,
                  TIM2_OutputState_TypeDef TIM2_OutputState,
                  u16 TIM2_Pulse,
                  TIM2_OCPolarity_TypeDef TIM2_OCPolarity)
{
  /* Check the parameters */
  assert_param(IS_TIM2_OC_MODE_OK(TIM2_OCMode));
  assert_param(IS_TIM2_OUTPUT_STATE_OK(TIM2_OutputState));
  assert_param(IS_TIM2_OC_POLARITY_OK(TIM2_OCPolarity));
  /* Disable the Channel 1: Reset the CCE Bit, Set the Output State, the Output Polarity */
  TIM2->CCER2 &= (u8)(~( TIM2_CCER2_CC3E  | TIM2_CCER2_CC3P));
  /* Set the Output State & Set the Output Polarity */
  TIM2->CCER2 |= (u8)((TIM2_OutputState  & TIM2_CCER2_CC3E   ) |  (TIM2_OCPolarity   & TIM2_CCER2_CC3P   ));

  /* Reset the Output Compare Bits & Set the Output Compare Mode */
  TIM2->CCMR3 = (u8)((TIM2->CCMR3 & (u8)(~TIM2_CCMR_OCM)) | (u8)TIM2_OCMode);

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

}


/**
  * @brief Initializes the TIM2 peripheral according to the specified parameters.
  * @param[in]  TIM2_Channel specifies the Input Capture Channel from @ref TIM2_Channel_TypeDef.
  * @param[in] TIM2_ICPolarity specifies the Input Capture Polarity from @ref TIM2_ICPolarity_TypeDef.
  * @param[in] TIM2_ICSelection specifies the Input Capture Selection from @ref TIM2_ICSelection_TypeDef.
  * @param[in] TIM2_ICPrescaler specifies the Input Capture Prescaler from @ref TIM2_ICPSC_TypeDef.
  * @param[in] TIM2_ICFilter specifies the Input Capture Filter value (value can be an integer from 0x00 to 0x0F).
  * @retval None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * TI1_Config
  * TI2_Config
  * TI3_Config
  * TIM2_SetIC1Prescaler
  * TIM2_SetIC2Prescaler
  * TIM2_SetIC3Prescaler
  */
void TIM2_ICInit(TIM2_Channel_TypeDef TIM2_Channel,
                 TIM2_ICPolarity_TypeDef TIM2_ICPolarity,
                 TIM2_ICSelection_TypeDef TIM2_ICSelection,
                 TIM2_ICPSC_TypeDef TIM2_ICPrescaler,
                 u8 TIM2_ICFilter)
{
  /* Check the parameters */
  assert_param(IS_TIM2_CHANNEL_OK(TIM2_Channel));
  assert_param(IS_TIM2_IC_POLARITY_OK(TIM2_ICPolarity));
  assert_param(IS_TIM2_IC_SELECTION_OK(TIM2_ICSelection));
  assert_param(IS_TIM2_IC_PRESCALER_OK(TIM2_ICPrescaler));
  assert_param(IS_TIM2_IC_FILTER_OK(TIM2_ICFilter));

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

    /* Set the Input Capture Prescaler value */
    TIM2_SetIC1Prescaler(TIM2_ICPrescaler);
  }
  else if (TIM2_Channel == TIM2_CHANNEL_2)
  {
    /* TI2 Configuration */
    TI2_Config((u8)TIM2_ICPolarity,
               (u8)TIM2_ICSelection,
               (u8)TIM2_ICFilter);

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

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


/**
  * @brief Configures the TIM2 peripheral in PWM Input Mode according to the specified parameters.
    * @param[in]  TIM2_Channel specifies the Input Capture Channel from @ref TIM2_Channel_TypeDef.
  * @param[in] TIM2_ICPolarity specifies the Input Capture Polarity from @ref TIM2_ICPolarity_TypeDef.
  * @param[in] TIM2_ICSelection specifies the Input Capture Selection from @ref TIM2_ICSelection_TypeDef.
  * @param[in] TIM2_ICPrescaler specifies the Input Capture Prescaler from @ref TIM2_ICPSC_TypeDef.
  * @param[in] TIM2_ICFilter specifies the Input Capture Filter value (value can be an integer from 0x00 to 0x0F).
  * @retval None
  * @par Required preconditions:
  * None
  * @par Called functions:
  * TI1_Config
  * TI2_Config
  * TIM2_SetIC1Prescaler
  * TIM2_SetIC2Prescaler
  */
void TIM2_PWMIConfig(TIM2_Channel_TypeDef TIM2_Channel,
                     TIM2_ICPolarity_TypeDef TIM2_ICPolarity,
                     TIM2_ICSelection_TypeDef TIM2_ICSelection,
                     TIM2_ICPSC_TypeDef TIM2_ICPrescaler,
                     u8 TIM2_ICFilter)
{
  u8 icpolarity = (u8)TIM2_ICPOLARITY_RISING;
  u8 icselection = (u8)TIM2_ICSELECTION_DIRECTTI;

  /* Check the parameters */
  assert_param(IS_TIM2_PWMI_CHANNEL_OK(TIM2_Channel));
  assert_param(IS_TIM2_IC_POLARITY_OK(TIM2_ICPolarity));
  assert_param(IS_TIM2_IC_SELECTION_OK(TIM2_ICSelection));
  assert_param(IS_TIM2_IC_PRESCALER_OK(TIM2_ICPrescaler));

  /* Select the Opposite Input Polarity */
  if (TIM2_ICPolarity != TIM2_ICPOLARITY_FALLING)
  {
    icpolarity = (u8)TIM2_ICPOLARITY_FALLING;
  }
  else
  {
    icpolarity = (u8)TIM2_ICPOLARITY_RISING;
  }

  /* 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((u8)TIM2_ICPolarity, (u8)TIM2_ICSelection,
               (u8)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((u8)TIM2_ICPolarity, (u8)TIM2_ICSelection,
               (u8)TIM2_ICFilter);

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

    /* TI1 Configuration */
    TI1_Config((u8)icpolarity, icselection, (u8)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 None
  * @par Required preconditions:
  * None
  */
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 |= (u8)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 None
  * @par Required preconditions:
  * None
  */
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 |= (u8)TIM2_IT;
  }
  else
  {
    /* Disable the Interrupt sources */
    TIM2->IER &= (u8)(~TIM2_IT);

⌨️ 快捷键说明

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