📄 stm8s_tim2.c
字号:
/**
******************************************************************************
* @file stm8s_tim2.c
* @brief This file contains all the functions for the TIM2 peripheral.
* @author STMicroelectronics - MCD Application Team
* @version V1.0.1
* @date 09/22/2008
******************************************************************************
*
* 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>© COPYRIGHT 2008 STMicroelectronics</center></h2>
* @image html logo.bmp
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm8s_tim2.h"
/* LINKER SECTIONS DEFINITION FOR THIS FILE ONLY */
#ifdef USE_COSMIC_SECTIONS
#pragma section (TIM2_CODE)
#pragma section const {TIM2_CONST}
#pragma section @near [TIM2_URAM]
#pragma section @near {TIM2_IRAM}
#pragma section @tiny [TIM2_UZRAM]
#pragma section @tiny {TIM2_IZRAM}
#endif
/* 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 void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Initialize TIM2 registers to their reset values.
* @code
* TIM2_DeInit();
* @endcode
*/
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 void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Initialize TIM2 registers according to the specified parameters.
* @code
* TIM2_Prescaler_TypeDef My_Prescaler = TIM2_PRESCALER_1;
* u16 My_Period = 0xFF55;
* TIM2_TimeBaseInit(My_Prescaler, My_Period);
* @endcode
*/
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 void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Initialize the TIM2 Channel1 according to the specified parameters.
* @code
* TIM2_OCMode_TypeDef My_OCMode = TIM2_OCMODE_PWM1;
* TIM2_OutputState_TypeDef My_OutputState = TIM2_OUTPUTSTATE_ENABLE;
* u16 My_Pulse = 0x7FFF;
* TIM2_OCPolarity_TypeDef My_OCPolarity = TIM2_OCPOLARITY_HIGH;
* TIM2_OC1Init( My_OCMode, My_OutputState, My_Pulse, My_OCPolarity);
* @endcode
*/
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 void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Initialize the TIM2 Channel2 according to the specified parameters.
* @code
* TIM2_OCMode_TypeDef My_OCMode = TIM2_OCMODE_PWM1;
* TIM2_OutputState_TypeDef My_OutputState = TIM2_OUTPUTSTATE_ENABLE;
* u16 My_Pulse = 0x7FFF;
* TIM2_OCPolarity_TypeDef My_OCPolarity = TIM2_OCPOLARITY_HIGH;
* TIM2_OC2Init( My_OCMode, My_OutputState, My_Pulse, My_OCPolarity);
* @endcode
*/
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 void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Initialize the TIM2 Channel3 according to the specified parameters.
* @code
* TIM2_OCMode_TypeDef My_OCMode = TIM2_OCMODE_PWM1;
* TIM2_OutputState_TypeDef My_OutputState = TIM2_OUTPUTSTATE_ENABLE;
* u16 My_Pulse = 0x7FFF;
* TIM2_OCPolarity_TypeDef My_OCPolarity = TIM2_OCPOLARITY_HIGH;
* TIM2_OC3Init( My_OCMode, My_OutputState, My_Pulse, My_OCPolarity);
* @endcode
*/
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 void None
* @par Required preconditions:
* None
* @par Called functions:
* TI1_Config
* TI2_Config
* TI3_Config
* TIM2_SetIC1Prescaler
* TIM2_SetIC2Prescaler
* TIM2_SetIC3Prescaler
* @par Example:
* Initialize the TIM2 Channel1 according to the specified parameters.
* @code
* TIM2_Channel_TypeDef My_Channel =TIM2_CHANNEL_1;
* TIM2_ICPolarity_TypeDef My_ICPolarity=TIM2_ICPOLARITY_RISING;
* TIM2_ICSelection_TypeDef My_ICSelection= TIM2_ICSELECTION_DIRECTTI;
* TIM2_ICPSC_TypeDef My_ICPrescaler=TIM2_ICPSC_DIV1;
* u8 TIM2_ICFilter= 0x0F;
* TIM2_ICInit(My_Channel, My_ICPolarity, My_ICSelection, My_ICPrescaler, My_ICFilter);
* @endcode
*/
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(TIM2_ICPolarity,
TIM2_ICSelection,
TIM2_ICFilter);
/* Set the Input Capture Prescaler value */
TIM2_SetIC1Prescaler(TIM2_ICPrescaler);
}
else if (TIM2_Channel == TIM2_CHANNEL_2)
{
/* TI2 Configuration */
TI2_Config(TIM2_ICPolarity,
TIM2_ICSelection,
TIM2_ICFilter);
/* Set the Input Capture Prescaler value */
TIM2_SetIC2Prescaler(TIM2_ICPrescaler);
}
else
{
/* TI3 Configuration */
TI3_Config(TIM2_ICPolarity,
TIM2_ICSelection,
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 void None
* @par Required preconditions:
* None
* @par Called functions:
* TI1_Config
* TI2_Config
* TIM2_SetIC1Prescaler
* TIM2_SetIC2Prescaler
* @par Example:
* Configure the TIM2 peripheral in PWM Input Mode according to the specified parameters.
* @code
* TIM2_Channel_TypeDef My_Channel =TIM2_CHANNEL_1;
* TIM2_ICPolarity_TypeDef My_ICPolarity=TIM2_ICPOLARITY_RISING;
* TIM2_ICSelection_TypeDef My_ICSelection= TIM2_ICSELECTION_DIRECTTI;
* TIM2_ICPSC_TypeDef My_ICPrescaler=TIM2_ICPSC_DIV1;
* u8 TIM2_ICFilter= 0x0F;
* TIM2_PWMIConfig(My_Channel, My_ICPolarity, My_ICSelection, My_ICPrescaler, My_ICFilter);
* @endcode
*/
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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -