📄 stm8l15x_tim5.c
字号:
{
/* Check the parameters */
assert_param(IS_TIM5_OSSI_STATE(TIM5_OSSIState));
assert_param(IS_TIM5_LOCK_LEVEL(TIM5_LockLevel));
assert_param(IS_TIM5_BREAK_STATE(TIM5_BreakState));
assert_param(IS_TIM5_BREAK_POLARITY(TIM5_BreakPolarity));
assert_param(IS_TIM5_AUTOMATIC_OUTPUT_STATE(TIM5_AutomaticOutput));
/* Set the Lock level, the Break enable Bit and the Ploarity, the OSSI State,
the dead time value and the Automatic Output Enable Bit */
TIM5->BKR = (uint8_t)((uint8_t)((uint8_t)((uint8_t)((uint8_t)TIM5_OSSIState | (uint8_t)TIM5_LockLevel) | \
(uint8_t)((uint8_t)TIM5_BreakState | (uint8_t)TIM5_BreakPolarity)) | \
TIM5_AutomaticOutput));
}
/**
* @brief Enables or disables the TIM5 peripheral Main Outputs.
* @param NewState: The new state of the TIM5 peripheral.
* This parameter can be ENABLE or DISABLE
* @retval None
*/
void TIM5_CtrlPWMOutputs(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* Set or Reset the MOE Bit */
if (NewState != DISABLE)
{
TIM5->BKR |= TIM_BKR_MOE ;
}
else
{
TIM5->BKR &= (uint8_t)(~TIM_BKR_MOE) ;
}
}
/**
* @brief Selects the TIM5 Output Compare Mode. This function disables the
* selected channel before changing the Output Compare Mode. User has to
* enable this channel using TIM5_CCxCmd and TIM5_CCxNCmd functions.
* @param TIM5_Channel: Specifies the TIM5 Channel.
* This parameter can be one of the following values:
* @arg TIM5_Channel_1: Channel 1
* @arg TIM5_Channel_2: Channel 2
* @param TIM5_OCMode: Specifies the TIM5 Output Compare Mode.
* This parameter can be one of the following values:
* @arg TIM5_OCMode_Timing: Timing (Frozen) Mode
* @arg TIM5_OCMode_Active: Active Mode
* @arg TIM5_OCMode_Inactive: Inactive Mode
* @arg TIM5_OCMode_Toggle: Toggle Mode
* @arg TIM5_OCMode_PWM1: PWM Mode 1
* @arg TIM5_OCMode_PWM2: PWM Mode 2
* @retval None
*/
void TIM5_SelectOCxM(TIM5_Channel_TypeDef TIM5_Channel,
TIM5_OCMode_TypeDef TIM5_OCMode)
{
/* Check the parameters */
assert_param(IS_TIM5_CHANNEL(TIM5_Channel));
assert_param(IS_TIM5_OCM(TIM5_OCMode));
if (TIM5_Channel == TIM5_Channel_1)
{
/* Disable the Channel 1: Reset the CCE Bit */
TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC1E);
/* Reset the Output Compare Bits */
TIM5->CCMR1 &= (uint8_t)(~TIM_CCMR_OCM);
/* Set the Output Compare Mode */
TIM5->CCMR1 |= (uint8_t)TIM5_OCMode;
}
else /* if (TIM5_Channel == TIM5_Channel_2) */
{
/* Disable the Channel 2: Reset the CCE Bit */
TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC2E);
/* Reset the Output Compare Bits */
TIM5->CCMR2 &= (uint8_t)(~TIM_CCMR_OCM);
/* Set the Output Compare Mode */
TIM5->CCMR2 |= (uint8_t)TIM5_OCMode;
}
}
/**
* @brief Sets the TIM5 Capture Compare1 Register value.
* @param Compare: Specifies the Capture Compare1 register new value.
* This parameter is between 0x0000 and 0xFFFF.
* @retval None
*/
void TIM5_SetCompare1(uint16_t Compare)
{
/* Set the Capture Compare1 Register value */
TIM5->CCR1H = (uint8_t)(Compare >> 8);
TIM5->CCR1L = (uint8_t)(Compare);
}
/**
* @brief Sets the TIM5 Capture Compare2 Register value.
* @param Compare: Specifies the Capture Compare2 register new value.
* This parameter is between 0x0000 and 0xFFFF.
* @retval None
*/
void TIM5_SetCompare2(uint16_t Compare)
{
/* Set the Capture Compare2 Register value */
TIM5->CCR2H = (uint8_t)(Compare >> 8);
TIM5->CCR2L = (uint8_t)(Compare);
}
/**
* @brief Forces the TIM5 Channel1 output waveform to active or inactive level.
* @param TIM5_ForcedAction: Specifies the forced Action to be set to the output waveform.
* This parameter can be one of the following values:
* @arg TIM5_ForcedAction_Active: Output Reference is forced low
* @arg TIM5_ForcedAction_Inactive: Output Reference is forced high
* @retval None
*/
void TIM5_ForcedOC1Config(TIM5_ForcedAction_TypeDef TIM5_ForcedAction)
{
uint8_t tmpccmr1 = 0;
/* Check the parameters */
assert_param(IS_TIM5_FORCED_ACTION(TIM5_ForcedAction));
tmpccmr1 = TIM5->CCMR1;
/* Reset the OCM Bits */
tmpccmr1 &= (uint8_t)(~TIM_CCMR_OCM);
/* Configure The Forced output Mode */
tmpccmr1 |= (uint8_t)TIM5_ForcedAction;
TIM5->CCMR1 = tmpccmr1;
}
/**
* @brief Forces the TIM5 Channel2 output waveform to active or inactive level.
* @param TIM5_ForcedAction: Specifies the forced Action to be set to the output waveform.
* This parameter can be one of the following values:
* @arg TIM5_ForcedAction_Active: Output Reference is forced low
* @arg TIM5_ForcedAction_Inactive: Output Reference is forced high
* @retval None
*/
void TIM5_ForcedOC2Config(TIM5_ForcedAction_TypeDef TIM5_ForcedAction)
{
uint8_t tmpccmr2 = 0;
/* Check the parameters */
assert_param(IS_TIM5_FORCED_ACTION(TIM5_ForcedAction));
tmpccmr2 = TIM5->CCMR2;
/* Reset the OCM Bits */
tmpccmr2 &= (uint8_t)(~TIM_CCMR_OCM);
/* Configure The Forced output Mode */
tmpccmr2 |= (uint8_t)TIM5_ForcedAction;
TIM5->CCMR2 = tmpccmr2;
}
/**
* @brief Enables or disables the TIM5 peripheral Preload Register on CCR1.
* @param NewState: The new state of the Capture Compare Preload register.
* This parameter can be ENABLE or DISABLE
* @retval None
*/
void TIM5_OC1PreloadConfig(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* Set or Reset the OC1PE Bit */
if (NewState != DISABLE)
{
TIM5->CCMR1 |= TIM_CCMR_OCxPE ;
}
else
{
TIM5->CCMR1 &= (uint8_t)(~TIM_CCMR_OCxPE) ;
}
}
/**
* @brief Enables or disables the TIM5 peripheral Preload Register on CCR2.
* @param NewState: The new state of the Capture Compare Preload register.
* This parameter can be ENABLE or DISABLE
* @retval None
*/
void TIM5_OC2PreloadConfig(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* Set or Reset the OC2PE Bit */
if (NewState != DISABLE)
{
TIM5->CCMR2 |= TIM_CCMR_OCxPE ;
}
else
{
TIM5->CCMR2 &= (uint8_t)(~TIM_CCMR_OCxPE) ;
}
}
/**
* @brief Configures the TIM5 Capture Compare 1 Fast feature.
* @param NewState: The new state of the Output Compare Fast Enable bit.
* This parameter can be ENABLE or DISABLE
* @retval None
*/
void TIM5_OC1FastConfig(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* Set or Reset the OC1FE Bit */
if (NewState != DISABLE)
{
TIM5->CCMR1 |= TIM_CCMR_OCxFE ;
}
else
{
TIM5->CCMR1 &= (uint8_t)(~TIM_CCMR_OCxFE) ;
}
}
/**
* @brief Configures the TIM5 Capture Compare 2 Fast feature.
* @param NewState: The new state of the Output Compare Fast Enable bit.
* This parameter can be ENABLE or DISABLE
* @retval None
*/
void TIM5_OC2FastConfig(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* Set or Reset the OC2FE Bit */
if (NewState != DISABLE)
{
TIM5->CCMR2 |= TIM_CCMR_OCxFE ;
}
else
{
TIM5->CCMR2 &= (uint8_t)(~TIM_CCMR_OCxFE) ;
}
}
/**
* @brief Configures the TIM5 Channel 1 polarity.
* @param TIM5_OCPolarity: Specifies the OC1 Polarity.
* This parameter can be one of the following values:
* @arg TIM5_OCPolarity_High: Output compare polarity = High
* @arg TIM5_OCPolarity_Low: Output compare polarity = Low
* @retval None
*/
void TIM5_OC1PolarityConfig(TIM5_OCPolarity_TypeDef TIM5_OCPolarity)
{
/* Check the parameters */
assert_param(IS_TIM5_OC_POLARITY(TIM5_OCPolarity));
/* Set or Reset the CC1P Bit */
if (TIM5_OCPolarity == TIM5_OCPolarity_Low)
{
TIM5->CCER1 |= TIM_CCER1_CC1P ;
}
else
{
TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC1P) ;
}
}
/**
* @brief Configures the TIM5 Channel 2 polarity.
* @param TIM5_OCPolarity: Specifies the OC2 Polarity.
* This parameter can be one of the following values:
* @arg TIM5_OCPolarity_High: Output compare polarity = High
* @arg TIM5_OCPolarity_Low: Output compare polarity = Low
* @retval None
*/
void TIM5_OC2PolarityConfig(TIM5_OCPolarity_TypeDef TIM5_OCPolarity)
{
/* Check the parameters */
assert_param(IS_TIM5_OC_POLARITY(TIM5_OCPolarity));
/* Set or Reset the CC2P Bit */
if (TIM5_OCPolarity == TIM5_OCPolarity_Low)
{
TIM5->CCER1 |= TIM_CCER1_CC2P ;
}
else
{
TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC2P) ;
}
}
/**
* @brief Enables or disables the TIM5 Capture Compare Channel x.
* @param TIM5_Channel: Specifies the TIM5 Channel.
* This parameter can be one of the following values:
* @arg TIM5_Channel_1: Channel 1
* @arg TIM5_Channel_2: Channel 2
* @param NewState: Specifies the TIM5 Channel CCxE bit new state.
* This parameter can be ENABLE or DISABLE
* @retval None
*/
void TIM5_CCxCmd(TIM5_Channel_TypeDef TIM5_Channel,
FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_TIM5_CHANNEL(TIM5_Channel));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (TIM5_Channel == TIM5_Channel_1)
{
/* Set or Reset the CC1E Bit */
if (NewState != DISABLE)
{
TIM5->CCER1 |= TIM_CCER1_CC1E ;
}
else
{
TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC1E) ;
}
}
else /* if (TIM5_Channel == TIM5_Channel_2) */
{
/* Set or Reset the CC2E Bit */
if (NewState != DISABLE)
{
TIM5->CCER1 |= TIM_CCER1_CC2E;
}
else
{
TIM5->CCER1 &= (uint8_t)(~TIM_CCER1_CC2E) ;
}
}
}
/** @defgroup TIM5_Group3 Input Capture management functions
* @brief Input Capture management functions
*
@verbatim
===============================================================================
Input Capture management functions
===============================================================================
===================================================================
TIM5 Driver: how to use it in Input Capture Mode
===================================================================
To use the Timer in Input Capture mode, the following steps are mandatory:
1. Enable TIM5 clock using CLK_PeripheralClockConfig(CLK_Peripheral_TIM5, ENABLE) function.
2. Configure the TIM5 pins in input mode by configuring the corresponding GPIO pins
3. Configure the Time base unit as described in the first part of this driver, if needed,
otherwise the Timer will run with the default configuration:
- Autoreload value = 0xFFFF
- Prescaler value = 0x0
- Counter mode = Up counting
4. Call TIM5_ICInit() to configure the desired channel to measure only
frequency or duty cycle of the input signal using the corresponding configuration:
- TIM5 Channel: TIM5_Channel
- TIM5 Input Capture polarity: TIM5_ICPolarity
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -