📄 stm8s_tim2.c
字号:
* @par Example:
* Set the TIM2 Autoreload Register value to 0xFFEE.
* @code
* TIM2_SetAutoreload(0xFFEE);
* @endcode
*/
void TIM2_SetAutoreload(u16 Autoreload)
{
/* Set the Autoreload Register value */
TIM2->ARRH = (u8)(Autoreload >> 8);
TIM2->ARRL = (u8)(Autoreload);
}
/**
* @brief Sets the TIM2 Capture Compare1 Register value.
* @param[in] Compare1 specifies the Capture Compare1 register new value.
* This parameter is between 0x0000 and 0xFFFF.
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Set the TIM2 Capture Compare1 Register value to 0xFFEE.
* @code
* TIM2_SetCompare1(0xFFEE);
* @endcode
*/
void TIM2_SetCompare1(u16 Compare1)
{
/* Set the Capture Compare1 Register value */
TIM2->CCR1H = (u8)(Compare1 >> 8);
TIM2->CCR1L = (u8)(Compare1);
}
/**
* @brief Sets the TIM2 Capture Compare2 Register value.
* @param[in] Compare2 specifies the Capture Compare2 register new value.
* This parameter is between 0x0000 and 0xFFFF.
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Set the TIM2 Capture Compare2 Register value to 0xFFEE.
* @code
* TIM2_SetCompare2(0xFFEE);
* @endcode
*/
void TIM2_SetCompare2(u16 Compare2)
{
/* Set the Capture Compare2 Register value */
TIM2->CCR2H = (u8)(Compare2 >> 8);
TIM2->CCR2L = (u8)(Compare2);
}
/**
* @brief Sets the TIM2 Capture Compare3 Register value.
* @param[in] Compare3 specifies the Capture Compare3 register new value.
* This parameter is between 0x0000 and 0xFFFF.
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Set the TIM2 Capture Compare3 Register value to 0xFFEE.
* @code
* TIM2_SetCompare3(0xFFEE);
* @endcode
*/
void TIM2_SetCompare3(u16 Compare3)
{
/* Set the Capture Compare3 Register value */
TIM2->CCR3H = (u8)(Compare3 >> 8);
TIM2->CCR3L = (u8)(Compare3);
}
/**
* @brief Sets the TIM2 Input Capture 1 Prescaler.
* @param[in] TIM2_IC1Prescaler specifies the Input Capture prescaler new value
* This parameter can be one of the following values:
* - TIM2_ICPSC_DIV1: no prescaler
* - TIM2_ICPSC_DIV2: capture is done once every 2 events
* - TIM2_ICPSC_DIV4: capture is done once every 4 events
* - TIM2_ICPSC_DIV8: capture is done once every 8 events
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Set the TIM2 Input Capture 1 prescaler to do a capture every 8 events.
* @code
* TIM2_SetIC1Prescaler(TIM2_ICPSC_DIV8);
* @endcode
*/
void TIM2_SetIC1Prescaler(TIM2_ICPSC_TypeDef TIM2_IC1Prescaler)
{
/* Check the parameters */
assert_param(IS_TIM2_IC_PRESCALER_OK(TIM2_IC1Prescaler));
/* Reset the IC1PSC Bits &Set the IC1PSC value */
TIM2->CCMR1 = (u8)((TIM2->CCMR1 & (u8)(~TIM2_CCMR_ICxPSC)) | (u8)TIM2_IC1Prescaler);
}
/**
* @brief Sets the TIM2 Input Capture 2 prescaler.
* @param[in] TIM2_IC2Prescaler specifies the Input Capture prescaler new value
* This parameter can be one of the following values:
* - TIM2_ICPSC_DIV1: no prescaler
* - TIM2_ICPSC_DIV2: capture is done once every 2 events
* - TIM2_ICPSC_DIV4: capture is done once every 4 events
* - TIM2_ICPSC_DIV8: capture is done once every 8 events
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Set the TIM2 Input Capture 2 prescaler to do a capture every 8 events.
* @code
* TIM2_SetIC2Prescaler(TIM2_ICPSC_DIV8);
* @endcode
*/
void TIM2_SetIC2Prescaler(TIM2_ICPSC_TypeDef TIM2_IC2Prescaler)
{
/* Check the parameters */
assert_param(IS_TIM2_IC_PRESCALER_OK(TIM2_IC2Prescaler));
/* Reset the IC1PSC Bits &Set the IC1PSC value */
TIM2->CCMR2 = (u8)((TIM2->CCMR2 & (u8)(~TIM2_CCMR_ICxPSC)) | (u8)TIM2_IC2Prescaler);
}
/**
* @brief Sets the TIM2 Input Capture 3 prescaler.
* @param[in] TIM2_IC3Prescaler specifies the Input Capture prescaler new value
* This parameter can be one of the following values:
* - TIM2_ICPSC_DIV1: no prescaler
* - TIM2_ICPSC_DIV2: capture is done once every 2 events
* - TIM2_ICPSC_DIV4: capture is done once every 4 events
* - TIM2_ICPSC_DIV8: capture is done once every 8 events
* @retval void None
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Set the TIM2 Input Capture 3 prescaler to do a capture every 8 events.
* @code
* TIM2_SetIC3Prescaler(TIM2_ICPSC_DIV8);
* @endcode
*/
void TIM2_SetIC3Prescaler(TIM2_ICPSC_TypeDef TIM2_IC3Prescaler)
{
/* Check the parameters */
assert_param(IS_TIM2_IC_PRESCALER_OK(TIM2_IC3Prescaler));
/* Reset the IC1PSC Bits &Set the IC1PSC value */
TIM2->CCMR3 = (u8)((TIM2->CCMR3 & (u8)(~TIM2_CCMR_ICxPSC)) | (u8)TIM2_IC3Prescaler);
}
/**
* @brief Gets the TIM2 Input Capture 1 value.
* @param[in] :
* None
* @retval Capture Compare 1 Register value.
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Get the TIM2 Input Capture 1 value.
* @code
* u16 Value;
* Value = TIM2_GetCapture1();
* @endcode
*/
u16 TIM2_GetCapture1(void)
{
/* Get the Capture 1 Register value */
u16 tmpccr1 = 0;
u8 tmpccr1l=0, tmpccr1h=0;
tmpccr1h = TIM2->CCR1H;
tmpccr1l = TIM2->CCR1L;
tmpccr1 = (u16)(tmpccr1l);
tmpccr1 |= (u16)((u16)tmpccr1h << 8);
/* Get the Capture 1 Register value */
return (u16)tmpccr1;
}
/**
* @brief Gets the TIM2 Input Capture 2 value.
* @param[in] :
* None
* @retval Capture Compare 2 Register value.
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Get the TIM2 Input Capture 2 value.
* @code
* u16 Value;
* Value = TIM2_GetCapture2();
* @endcode
*/
u16 TIM2_GetCapture2(void)
{
/* Get the Capture 2 Register value */
u16 tmpccr2 = 0;
u8 tmpccr2l=0, tmpccr2h=0;
tmpccr2h = TIM2->CCR2H;
tmpccr2l = TIM2->CCR2L;
tmpccr2 = (u16)(tmpccr2l);
tmpccr2 |= (u16)((u16)tmpccr2h << 8);
/* Get the Capture 2 Register value */
return (u16)tmpccr2;
}
/**
* @brief Gets the TIM2 Input Capture 3 value.
* @param[in] :
* None
* @retval Capture Compare 3 Register value.
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Get the TIM2 Input Capture 3 value.
* @code
* u16 Value;
* Value = TIM2_GetCapture3();
* @endcode
*/
u16 TIM2_GetCapture3(void)
{
/* Get the Capture 3 Register value */
u16 tmpccr3 = 0;
u8 tmpccr3l=0, tmpccr3h=0;
tmpccr3h = TIM2->CCR3H;
tmpccr3l = TIM2->CCR3L;
tmpccr3 = (u16)(tmpccr3l);
tmpccr3 |= (u16)((u16)tmpccr3h << 8);
/* Get the Capture 3 Register value */
return (u16)tmpccr3;
}
/**
* @brief Gets the TIM2 Counter value.
* @param[in] :
* None
* @retval Counter Register value.
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Get the TIM2 Counter value.
* @code
* u16 Value;
* Value = TIM2_GetCounter();
* @endcode
*/
u16 TIM2_GetCounter(void)
{
/* Get the Counter Register value */
return (u16)(((u16)TIM2->CNTRH << 8) | (u16)(TIM2->CNTRL));
}
/**
* @brief Gets the TIM2 Prescaler value.
* @param[in] :
* None
* @retval Prescaler Register configuration value @ref TIM2_Prescaler_TypeDef.
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Get the TIM2 Prescaler configurartion value.
* @code
* TIM2_Prescaler_TypeDef Prescaler;
* Prescaler = TIM2_GetPrescaler();
* @endcode
*/
TIM2_Prescaler_TypeDef TIM2_GetPrescaler(void)
{
/* Get the Prescaler Register value */
return (TIM2_Prescaler_TypeDef)(TIM2->PSCR);
}
/**
* @brief Checks whether the specified TIM2 flag is set or not.
* @param[in] TIM2_FLAG specifies the flag to check.
* This parameter can be one of the following values:
* - TIM2_FLAG_UPDATE: TIM2 update Flag
* - TIM2_FLAG_CC1: TIM2 Capture Compare 1 Flag
* - TIM2_FLAG_CC2: TIM2 Capture Compare 2 Flag
* - TIM2_FLAG_CC3: TIM2 Capture Compare 3 Flag
* - TIM2_FLAG_CC1OF: TIM2 Capture Compare 1 over capture Flag
* - TIM2_FLAG_CC2OF: TIM2 Capture Compare 2 over capture Flag
* - TIM2_FLAG_CC3OF: TIM2 Capture Compare 3 over capture Flag
* @retval FlagStatus The new state of TIM2_FLAG (SET or RESET).
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Check whether the TIM2_FLAG_UPDATE flag is set or not.
* @code
* FlagStatus FlagValue;
* FlagValue = TIM2_GetFlagStatus(TIM2_FLAG_UPDATE);
* @endcode
*/
FlagStatus TIM2_GetFlagStatus(TIM2_FLAG_TypeDef TIM2_FLAG)
{
FlagStatus bitstatus = RESET;
u8 tim2_flag_l, tim2_flag_h;
/* Check the parameters */
assert_param(IS_TIM2_GET_FLAG_OK(TIM2_FLAG));
tim2_flag_l = (u8)(TIM2_FLAG);
tim2_flag_h = (u8)(TIM2_FLAG >> 8);
if (((TIM2->SR1 & tim2_flag_l) | (TIM2->SR2 & tim2_flag_h)) != (u8)RESET )
{
bitstatus = SET;
}
else
{
bitstatus = RESET;
}
return (FlagStatus)bitstatus;
}
/**
* @brief Clears the TIM2抯 pending flags.
* @param[in] TIM2_FLAG specifies the flag to clear.
* This parameter can be one of the following values:
* - TIM2_FLAG_UPDATE: TIM2 update Flag
* - TIM2_FLAG_CC1: TIM2 Capture Compare 1 Flag
* - TIM2_FLAG_CC2: TIM2 Capture Compare 2 Flag
* - TIM2_FLAG_CC3: TIM2 Capture Compare 3 Flag
* - TIM2_FLAG_CC1OF: TIM2 Capture Compare 1 over capture Flag
* - TIM2_FLAG_CC2OF: TIM2 Capture Compare 2 over capture Flag
* - TIM2_FLAG_CC3OF: TIM2 Capture Compare 3 over capture Flag
* @retval void None.
* @par Required preconditions:
* None
* @par Called functions:
* None
* @par Example:
* Clear the TIM2_FLAG_UPDATE flag.
* @code
* TIM2_ClearFlag(TIM2_FLAG_UPDATE);
* @endcode
*/
void TIM2_ClearFlag(TIM2_FLAG_TypeDef TIM2_FLAG)
{
/* Check the parameters */
assert_param(IS_TIM2_CLEAR_FLAG_OK(TIM2_FLAG));
/* Clear the flags (rc_w0) clear this bit by writing 0. Writing
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -