📄 stm8s_tim2.c
字号:
/* Check the parameters */
assert_param(IS_TIM2_CHANNEL_OK(TIM2_Channel));
assert_param(IS_FUNCTIONALSTATE_OK(NewState));
if (TIM2_Channel == TIM2_CHANNEL_1)
{
/* Set or Reset the CC1E Bit */
if (NewState != DISABLE)
{
TIM2->CCER1 |= (u8)TIM2_CCER1_CC1E;
}
else
{
TIM2->CCER1 &= (u8)(~TIM2_CCER1_CC1E);
}
}
else if (TIM2_Channel == TIM2_CHANNEL_2)
{
/* Set or Reset the CC2E Bit */
if (NewState != DISABLE)
{
TIM2->CCER1 |= (u8)TIM2_CCER1_CC2E;
}
else
{
TIM2->CCER1 &= (u8)(~TIM2_CCER1_CC2E);
}
}
else
{
/* Set or Reset the CC3E Bit */
if (NewState != DISABLE)
{
TIM2->CCER2 |= (u8)TIM2_CCER2_CC3E;
}
else
{
TIM2->CCER2 &= (u8)(~TIM2_CCER2_CC3E);
}
}
}
/**
* @brief Selects the TIM2 Output Compare Mode. This function disables the
* selected channel before changing the Output Compare Mode. User has to
* enable this channel using TIM2_CCxCmd and TIM2_CCxNCmd functions.
* @param[in] TIM2_Channel specifies the TIM2 Channel.
* This parameter can be one of the following values:
* - TIM2_CHANNEL_1: TIM2 Channel1
* - TIM2_CHANNEL_2: TIM2 Channel2
* - TIM2_CHANNEL_3: TIM2 Channel3
* @param[in] TIM2_OCMode specifies the TIM2 Output Compare Mode.
* This paramter can be one of the following values:
* - TIM2_OCMODE_TIMING
* - TIM2_OCMODE_ACTIVE
* - TIM2_OCMODE_TOGGLE
* - TIM2_OCMODE_PWM1
* - TIM2_OCMODE_PWM2
* - TIM2_FORCEDACTION_ACTIVE
* - TIM2_FORCEDACTION_INACTIVE
* @retval None
*/
void TIM2_SelectOCxM(TIM2_Channel_TypeDef TIM2_Channel, TIM2_OCMode_TypeDef TIM2_OCMode)
{
/* Check the parameters */
assert_param(IS_TIM2_CHANNEL_OK(TIM2_Channel));
assert_param(IS_TIM2_OCM_OK(TIM2_OCMode));
if (TIM2_Channel == TIM2_CHANNEL_1)
{
/* Disable the Channel 1: Reset the CCE Bit */
TIM2->CCER1 &= (u8)(~TIM2_CCER1_CC1E);
/* Reset the Output Compare Bits & Set the Output Compare Mode */
TIM2->CCMR1 = (u8)((TIM2->CCMR1 & (u8)(~TIM2_CCMR_OCM)) | (u8)TIM2_OCMode);
}
else if (TIM2_Channel == TIM2_CHANNEL_2)
{
/* Disable the Channel 2: Reset the CCE Bit */
TIM2->CCER1 &= (u8)(~TIM2_CCER1_CC2E);
/* Reset the Output Compare Bits & Set the Output Compare Mode */
TIM2->CCMR2 = (u8)((TIM2->CCMR2 & (u8)(~TIM2_CCMR_OCM)) | (u8)TIM2_OCMode);
}
else
{
/* Disable the Channel 3: Reset the CCE Bit */
TIM2->CCER2 &= (u8)(~TIM2_CCER2_CC3E);
/* Reset the Output Compare Bits & Set the Output Compare Mode */
TIM2->CCMR3 = (u8)((TIM2->CCMR3 & (u8)(~TIM2_CCMR_OCM)) | (u8)TIM2_OCMode);
}
}
/**
* @brief Sets the TIM2 Counter Register value.
* @param[in] Counter specifies the Counter register new value.
* This parameter is between 0x0000 and 0xFFFF.
* @retval None
*/
void TIM2_SetCounter(u16 Counter)
{
/* Set the Counter Register value */
TIM2->CNTRH = (u8)(Counter >> 8);
TIM2->CNTRL = (u8)(Counter);
}
/**
* @brief Sets the TIM2 Autoreload Register value.
* @param[in] Autoreload specifies the Autoreload register new value.
* This parameter is between 0x0000 and 0xFFFF.
* @retval None
*/
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 None
*/
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 None
*/
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 None
*/
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 None
*/
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 None
*/
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 None
*/
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.
*/
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.
*/
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.
*/
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.
*/
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.
*/
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).
*/
FlagStatus TIM2_GetFlagStatus(TIM2_FLAG_TypeDef TIM2_FLAG)
{
volatile FlagStatus bitstatus = RESET;
vu8 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 None.
*/
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 + -