📄 stm8s_tim3.c
字号:
/**
******************************************************************************
* @file stm8s_tim3.c
* @brief This file contains all the functions for the TIM3 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>© COPYRIGHT 2009 STMicroelectronics</center></h2>
* @image html logo.bmp
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm8s_tim3.h"
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
static void TI1_Config(u8 TIM3_ICPolarity, u8 TIM3_ICSelection, u8 TIM3_ICFilter);
static void TI2_Config(u8 TIM3_ICPolarity, u8 TIM3_ICSelection, u8 TIM3_ICFilter);
/**
* @addtogroup TIM3_Public_Functions
* @{
*/
/**
* @brief Deinitializes the TIM3 peripheral registers to their default reset values.
* @param[in] :
* None
* @retval None
* @par Required preconditions:
* None
*/
void TIM3_DeInit(void)
{
TIM3->CR1 = (u8)TIM3_CR1_RESET_VALUE;
TIM3->IER = (u8)TIM3_IER_RESET_VALUE;
TIM3->SR2 = (u8)TIM3_SR2_RESET_VALUE;
/* Disable channels */
TIM3->CCER1 = (u8)TIM3_CCER1_RESET_VALUE;
/* Then reset channel registers: it also works if lock level is equal to 2 or 3 */
TIM3->CCER1 = (u8)TIM3_CCER1_RESET_VALUE;
TIM3->CCMR1 = (u8)TIM3_CCMR1_RESET_VALUE;
TIM3->CCMR2 = (u8)TIM3_CCMR2_RESET_VALUE;
TIM3->CNTRH = (u8)TIM3_CNTRH_RESET_VALUE;
TIM3->CNTRL = (u8)TIM3_CNTRL_RESET_VALUE;
TIM3->PSCR = (u8)TIM3_PSCR_RESET_VALUE;
TIM3->ARRH = (u8)TIM3_ARRH_RESET_VALUE;
TIM3->ARRL = (u8)TIM3_ARRL_RESET_VALUE;
TIM3->CCR1H = (u8)TIM3_CCR1H_RESET_VALUE;
TIM3->CCR1L = (u8)TIM3_CCR1L_RESET_VALUE;
TIM3->CCR2H = (u8)TIM3_CCR2H_RESET_VALUE;
TIM3->CCR2L = (u8)TIM3_CCR2L_RESET_VALUE;
TIM3->SR1 = (u8)TIM3_SR1_RESET_VALUE;
}
/**
* @brief Initializes the TIM3 Time Base Unit according to the specified parameters.
* @param[in] TIM3_Prescaler specifies the Prescaler from TIM3_Prescaler_TypeDef.
* @param[in] TIM3_Period specifies the Period value.
* @retval None
* @par Required preconditions:
* None
*/
void TIM3_TimeBaseInit( TIM3_Prescaler_TypeDef TIM3_Prescaler,
u16 TIM3_Period)
{
/* Set the Prescaler value */
TIM3->PSCR = (u8)(TIM3_Prescaler);
/* Set the Autoreload value */
TIM3->ARRH = (u8)(TIM3_Period >> 8);
TIM3->ARRL = (u8)(TIM3_Period);
}
/**
* @brief Initializes the TIM3 Channel1 according to the specified parameters.
* @param[in] TIM3_OCMode specifies the Output Compare mode from @ref TIM3_OCMode_TypeDef.
* @param[in] TIM3_OutputState specifies the Output State from @ref TIM3_OutputState_TypeDef.
* @param[in] TIM3_Pulse specifies the Pulse width value.
* @param[in] TIM3_OCPolarity specifies the Output Compare Polarity from @ref TIM3_OCPolarity_TypeDef.
* @retval None
* @par Required preconditions:
* None
*/
void TIM3_OC1Init(TIM3_OCMode_TypeDef TIM3_OCMode,
TIM3_OutputState_TypeDef TIM3_OutputState,
u16 TIM3_Pulse,
TIM3_OCPolarity_TypeDef TIM3_OCPolarity)
{
/* Check the parameters */
assert_param(IS_TIM3_OC_MODE_OK(TIM3_OCMode));
assert_param(IS_TIM3_OUTPUT_STATE_OK(TIM3_OutputState));
assert_param(IS_TIM3_OC_POLARITY_OK(TIM3_OCPolarity));
/* Disable the Channel 1: Reset the CCE Bit, Set the Output State , the Output Polarity */
TIM3->CCER1 &= (u8)(~( TIM3_CCER1_CC1E | TIM3_CCER1_CC1P));
/* Set the Output State & Set the Output Polarity */
TIM3->CCER1 |= (u8)((TIM3_OutputState & TIM3_CCER1_CC1E ) | (TIM3_OCPolarity & TIM3_CCER1_CC1P ));
/* Reset the Output Compare Bits & Set the Output Compare Mode */
TIM3->CCMR1 = (u8)((TIM3->CCMR1 & (u8)(~TIM3_CCMR_OCM)) | (u8)TIM3_OCMode);
/* Set the Pulse value */
TIM3->CCR1H = (u8)(TIM3_Pulse >> 8);
TIM3->CCR1L = (u8)(TIM3_Pulse);
}
/**
* @brief Initializes the TIM3 Channel2 according to the specified parameters.
* @param[in] TIM3_OCMode specifies the Output Compare mode from @ref TIM3_OCMode_TypeDef.
* @param[in] TIM3_OutputState specifies the Output State from @ref TIM3_OutputState_TypeDef.
* @param[in] TIM3_Pulse specifies the Pulse width value.
* @param[in] TIM3_OCPolarity specifies the Output Compare Polarity from @ref TIM3_OCPolarity_TypeDef.
* @retval None
* @par Required preconditions:
* None
*/
void TIM3_OC2Init(TIM3_OCMode_TypeDef TIM3_OCMode,
TIM3_OutputState_TypeDef TIM3_OutputState,
u16 TIM3_Pulse,
TIM3_OCPolarity_TypeDef TIM3_OCPolarity)
{
/* Check the parameters */
assert_param(IS_TIM3_OC_MODE_OK(TIM3_OCMode));
assert_param(IS_TIM3_OUTPUT_STATE_OK(TIM3_OutputState));
assert_param(IS_TIM3_OC_POLARITY_OK(TIM3_OCPolarity));
/* Disable the Channel 1: Reset the CCE Bit, Set the Output State, the Output Polarity */
TIM3->CCER1 &= (u8)(~( TIM3_CCER1_CC2E | TIM3_CCER1_CC2P ));
/* Set the Output State & Set the Output Polarity */
TIM3->CCER1 |= (u8)((TIM3_OutputState & TIM3_CCER1_CC2E ) | (TIM3_OCPolarity & TIM3_CCER1_CC2P ));
/* Reset the Output Compare Bits & Set the Output Compare Mode */
TIM3->CCMR2 = (u8)((TIM3->CCMR2 & (u8)(~TIM3_CCMR_OCM)) | (u8)TIM3_OCMode);
/* Set the Pulse value */
TIM3->CCR2H = (u8)(TIM3_Pulse >> 8);
TIM3->CCR2L = (u8)(TIM3_Pulse);
}
/**
* @brief Initializes the TIM3 peripheral according to the specified parameters.
* @param[in] TIM3_Channel specifies the Input Capture Channel from @ref TIM3_Channel_TypeDef.
* @param[in] TIM3_ICPolarity specifies the Input Capture Polarity from @ref TIM3_ICPolarity_TypeDef.
* @param[in] TIM3_ICSelection specifies the Input Capture Selection from @ref TIM3_ICSelection_TypeDef.
* @param[in] TIM3_ICPrescaler specifies the Input Capture Prescaler from @ref TIM3_ICPSC_TypeDef.
* @param[in] TIM3_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
* TIM3_SetIC1Prescaler
* TIM3_SetIC2Prescaler
* TIM3_SetIC3Prescaler
*/
void TIM3_ICInit(TIM3_Channel_TypeDef TIM3_Channel,
TIM3_ICPolarity_TypeDef TIM3_ICPolarity,
TIM3_ICSelection_TypeDef TIM3_ICSelection,
TIM3_ICPSC_TypeDef TIM3_ICPrescaler,
u8 TIM3_ICFilter)
{
/* Check the parameters */
assert_param(IS_TIM3_CHANNEL_OK(TIM3_Channel));
assert_param(IS_TIM3_IC_POLARITY_OK(TIM3_ICPolarity));
assert_param(IS_TIM3_IC_SELECTION_OK(TIM3_ICSelection));
assert_param(IS_TIM3_IC_PRESCALER_OK(TIM3_ICPrescaler));
assert_param(IS_TIM3_IC_FILTER_OK(TIM3_ICFilter));
if (TIM3_Channel != TIM3_CHANNEL_2)
{
/* TI1 Configuration */
TI1_Config((u8)TIM3_ICPolarity,
(u8)TIM3_ICSelection,
(u8)TIM3_ICFilter);
/* Set the Input Capture Prescaler value */
TIM3_SetIC1Prescaler(TIM3_ICPrescaler);
}
else
{
/* TI2 Configuration */
TI2_Config((u8)TIM3_ICPolarity,
(u8)TIM3_ICSelection,
(u8)TIM3_ICFilter);
/* Set the Input Capture Prescaler value */
TIM3_SetIC2Prescaler(TIM3_ICPrescaler);
}
}
/**
* @brief Configures the TIM3 peripheral in PWM Input Mode according to the specified parameters.
* @param[in] TIM3_Channel specifies the Input Capture Channel from @ref TIM3_Channel_TypeDef.
* @param[in] TIM3_ICPolarity specifies the Input Capture Polarity from @ref TIM3_ICPolarity_TypeDef.
* @param[in] TIM3_ICSelection specifies theInput Capture Selection from @ref TIM3_ICSelection_TypeDef.
* @param[in] TIM3_ICPrescaler specifies the Input Capture Prescaler from @ref TIM3_ICPSC_TypeDef.
* @param[in] TIM3_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
* TIM3_SetIC1Prescaler
* TIM3_SetIC2Prescaler
*/
void TIM3_PWMIConfig(TIM3_Channel_TypeDef TIM3_Channel,
TIM3_ICPolarity_TypeDef TIM3_ICPolarity,
TIM3_ICSelection_TypeDef TIM3_ICSelection,
TIM3_ICPSC_TypeDef TIM3_ICPrescaler,
u8 TIM3_ICFilter)
{
u8 icpolarity = (u8)TIM3_ICPOLARITY_RISING;
u8 icselection = (u8)TIM3_ICSELECTION_DIRECTTI;
/* Check the parameters */
assert_param(IS_TIM3_PWMI_CHANNEL_OK(TIM3_Channel));
assert_param(IS_TIM3_IC_POLARITY_OK(TIM3_ICPolarity));
assert_param(IS_TIM3_IC_SELECTION_OK(TIM3_ICSelection));
assert_param(IS_TIM3_IC_PRESCALER_OK(TIM3_ICPrescaler));
/* Select the Opposite Input Polarity */
if (TIM3_ICPolarity != TIM3_ICPOLARITY_FALLING)
{
icpolarity = (u8)TIM3_ICPOLARITY_FALLING;
}
else
{
icpolarity = (u8)TIM3_ICPOLARITY_RISING;
}
/* Select the Opposite Input */
if (TIM3_ICSelection == TIM3_ICSELECTION_DIRECTTI)
{
icselection = (u8)TIM3_ICSELECTION_INDIRECTTI;
}
else
{
icselection = (u8)TIM3_ICSELECTION_DIRECTTI;
}
if (TIM3_Channel != TIM3_CHANNEL_2)
{
/* TI1 Configuration */
TI1_Config((u8)TIM3_ICPolarity, (u8)TIM3_ICSelection,
(u8)TIM3_ICFilter);
/* Set the Input Capture Prescaler value */
TIM3_SetIC1Prescaler(TIM3_ICPrescaler);
/* TI2 Configuration */
TI2_Config(icpolarity, icselection, TIM3_ICFilter);
/* Set the Input Capture Prescaler value */
TIM3_SetIC2Prescaler(TIM3_ICPrescaler);
}
else
{
/* TI2 Configuration */
TI2_Config((u8)TIM3_ICPolarity, (u8)TIM3_ICSelection,
(u8)TIM3_ICFilter);
/* Set the Input Capture Prescaler value */
TIM3_SetIC2Prescaler(TIM3_ICPrescaler);
/* TI1 Configuration */
TI1_Config(icpolarity, icselection, TIM3_ICFilter);
/* Set the Input Capture Prescaler value */
TIM3_SetIC1Prescaler(TIM3_ICPrescaler);
}
}
/**
* @brief Enables or disables the TIM3 peripheral.
* @param[in] NewState new state of the TIM3 peripheral. This parameter can
* be ENABLE or DISABLE.
* @retval None
* @par Required preconditions:
* None
*/
void TIM3_Cmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONALSTATE_OK(NewState));
/* set or Reset the CEN Bit */
if (NewState != DISABLE)
{
TIM3->CR1 |= (u8)TIM3_CR1_CEN;
}
else
{
TIM3->CR1 &= (u8)(~TIM3_CR1_CEN);
}
}
/**
* @brief Enables or disables the specified TIM3 interrupts.
* @param[in] NewState new state of the TIM3 peripheral.
* This parameter can be: ENABLE or DISABLE.
* @param[in] TIM3_IT specifies the TIM3 interrupts sources to be enabled or disabled.
* This parameter can be any combination of the following values:
* - TIM3_IT_UPDATE: TIM3 update Interrupt source
* - TIM3_IT_CC1: TIM3 Capture Compare 1 Interrupt source
* - TIM3_IT_CC2: TIM3 Capture Compare 2 Interrupt source
* - TIM3_IT_CC3: TIM3 Capture Compare 3 Interrupt source
* @param[in] NewState new state of the TIM3 peripheral. * @retval None
* @par Required preconditions:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -