stm8l15x_tim2.c
来自「STM8L的tim4定时器使用」· C语言 代码 · 共 1,504 行 · 第 1/4 页
C
1,504 行
TIM2_Prescaler_TypeDef TIM2_GetPrescaler(void)
{
/* Get the Prescaler Register value */
return ((TIM2_Prescaler_TypeDef)TIM2->PSCR);
}
/**
* @brief Enables or Disables the TIM2 Update event.
* @param NewState: The new state of the TIM2 peripheral Preload register.
* This parameter can be ENABLE or DISABLE
* @retval None
*/
void TIM2_UpdateDisableConfig(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* Set or Reset the UDIS Bit */
if (NewState != DISABLE)
{
TIM2->CR1 |= TIM_CR1_UDIS;
}
else
{
TIM2->CR1 &= (uint8_t)(~TIM_CR1_UDIS);
}
}
/**
* @brief Selects the TIM2 Update Request Interrupt source.
* @param TIM2_UpdateSource: Specifies the Update source.
* This parameter can be one of the following values:
* @arg TIM2_UpdateSource_Global: Global Update request source
* @arg TIM2_UpdateSource_Regular: Regular Update request source
* @retval None
*/
void TIM2_UpdateRequestConfig(TIM2_UpdateSource_TypeDef TIM2_UpdateSource)
{
/* Check the parameters */
assert_param(IS_TIM2_UPDATE_SOURCE(TIM2_UpdateSource));
/* Set or Reset the URS Bit */
if (TIM2_UpdateSource == TIM2_UpdateSource_Regular)
{
TIM2->CR1 |= TIM_CR1_URS ;
}
else
{
TIM2->CR1 &= (uint8_t)(~TIM_CR1_URS);
}
}
/**
* @brief Enables or disables TIM2 peripheral Preload register on ARR.
* @param NewState: The new state of the TIM2 peripheral Preload register.
* This parameter can be ENABLE or DISABLE
* @retval None
*/
void TIM2_ARRPreloadConfig(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* Set or Reset the ARPE Bit */
if (NewState != DISABLE)
{
TIM2->CR1 |= TIM_CR1_ARPE;
}
else
{
TIM2->CR1 &= (uint8_t)(~TIM_CR1_ARPE);
}
}
/**
* @brief Selects the TIM抯 One Pulse Mode.
* @param TIM2_OPMode: Specifies the OPM Mode to be used.
* This parameter can be one of the following values:
* @arg TIM2_OPMode_Single: Single one Pulse mode (OPM Active)
* @arg TIM2_OPMode_Repetitive: Single one Pulse mode (OPM Active)
* @retval None
*/
void TIM2_SelectOnePulseMode(TIM2_OPMode_TypeDef TIM2_OPMode)
{
/* Check the parameters */
assert_param(IS_TIM2_OPM_MODE(TIM2_OPMode));
/* Set or Reset the OPM Bit */
if (TIM2_OPMode == TIM2_OPMode_Single)
{
TIM2->CR1 |= TIM_CR1_OPM ;
}
else
{
TIM2->CR1 &= (uint8_t)(~TIM_CR1_OPM);
}
}
/**
* @brief Enables or disables the TIM2 peripheral.
* @param NewState: The new state of the TIM2 peripheral.
* This parameter can be ENABLE or DISABLE
* @retval None
*/
void TIM2_Cmd(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* set or Reset the CEN Bit */
if (NewState != DISABLE)
{
TIM2->CR1 |= TIM_CR1_CEN;
}
else
{
TIM2->CR1 &= (uint8_t)(~TIM_CR1_CEN);
}
}
/**
* @}
*/
/** @defgroup TIM2_Group2 Output Compare management functions
* @brief Output Compare management functions
*
@verbatim
===============================================================================
Output Compare management functions
===============================================================================
===================================================================
TIM2 Driver: how to use it in Output Compare Mode
===================================================================
To use the Timer in Output Compare mode, the following steps are mandatory:
1. Enable TIM2 clock using CLK_PeripheralClockConfig(CLK_Peripheral_TIM2, ENABLE) function.
2. Configure the TIM2 pins in output 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 = 0x0000
- Counter mode = Up counting
4. Call TIM2_OCxInit() to configure the channel x with the desired parameters
including:
- TIM2 Output Compare mode: TIM2_OCMode
- TIM2 Output State: TIM2_OutputState
- TIM2 Pulse value: TIM2_Pulse
- TIM2 Output Compare Polarity: TIM2_OCPolarity
- TIM2 Output Idle State: TIM2_OCIdleState
5. Call the TIM2_Cmd(ENABLE) function to enable the TIM2 counter.
Note1: All other functions can be used separately to modify, if needed,
a specific feature of the Timer.
Note2: If the corresponding interrupt or DMA request are needed, the user should:
1. Enable global interrupts (or the DMA) to use the TIM2 interrupts (or DMA requests).
2. Enable the corresponding interrupt (or DMA request) using the function
TIM2_ITConfig(TIM2_IT_CCx) (or TIM2_DMACmd(TIM2_DMASource_CCx))
@endverbatim
* @{
*/
/**
* @brief Initializes the TIM2 Channel1 according to the specified parameters.
* @param TIM2_OCMode: Output Compare Mode
* This parameter can be one of the following values:
* @arg TIM2_OCMode_Timing: Timing (Frozen) Mode
* @arg TIM2_OCMode_Active: Active Mode
* @arg TIM2_OCMode_Inactive: Inactive Mode
* @arg TIM2_OCMode_Toggle: Toggle Mode
* @arg TIM2_OCMode_PWM1: PWM Mode 1
* @arg TIM2_OCMode_PWM2: PWM Mode 2
* @param TIM2_OutputState: Output state
* This parameter can be one of the following values:
* @arg TIM2_OutputState_Disable: Output compare State disabled (channel output disabled)
* @arg TIM2_OutputState_Enable: Output compare State enabled (channel output enabled)
* @param TIM2_Pulse: This parameter must be a value between 0x0000 and 0xFFFF.
* @param TIM2_OCPolarity: Polarity
* This parameter can be one of the following values:
* @arg TIM2_OCPolarity_High: Output compare polarity = High
* @arg TIM2_OCPolarity_Low: Output compare polarity = Low
* @param TIM2_OCIdleState: Output Compare Idle State
* This parameter can be one of the following values:
* @arg TIM2_OCIdleState_Reset: Output Compare Idle state = Reset
* @arg TIM2_OCIdleState_Set: Output Compare Idle state = Set
* @retval None
*/
void TIM2_OC1Init(TIM2_OCMode_TypeDef TIM2_OCMode,
TIM2_OutputState_TypeDef TIM2_OutputState,
uint16_t TIM2_Pulse,
TIM2_OCPolarity_TypeDef TIM2_OCPolarity,
TIM2_OCIdleState_TypeDef TIM2_OCIdleState)
{
uint8_t tmpccmr1 = 0;
/* Check the parameters */
assert_param(IS_TIM2_OC_MODE(TIM2_OCMode));
assert_param(IS_TIM2_OUTPUT_STATE(TIM2_OutputState));
assert_param(IS_TIM2_OC_POLARITY(TIM2_OCPolarity));
assert_param(IS_TIM2_OCIDLE_STATE(TIM2_OCIdleState));
tmpccmr1 = TIM2->CCMR1;
/* Disable the Channel 1: Reset the CCE Bit */
TIM2->CCER1 &= (uint8_t)(~TIM_CCER1_CC1E);
/* Reset the Output Compare Bits */
tmpccmr1 &= (uint8_t)(~TIM_CCMR_OCM);
/* Set the Output Compare Mode */
tmpccmr1 |= (uint8_t)TIM2_OCMode;
TIM2->CCMR1 = tmpccmr1;
/* Set the Output State */
if (TIM2_OutputState == TIM2_OutputState_Enable)
{
TIM2->CCER1 |= TIM_CCER1_CC1E;
}
else
{
TIM2->CCER1 &= (uint8_t)(~TIM_CCER1_CC1E);
}
/* Set the Output Polarity */
if (TIM2_OCPolarity == TIM2_OCPolarity_Low)
{
TIM2->CCER1 |= TIM_CCER1_CC1P;
}
else
{
TIM2->CCER1 &= (uint8_t)(~TIM_CCER1_CC1P);
}
/* Set the Output Idle state */
if (TIM2_OCIdleState == TIM2_OCIdleState_Set)
{
TIM2->OISR |= TIM_OISR_OIS1;
}
else
{
TIM2->OISR &= (uint8_t)(~TIM_OISR_OIS1);
}
/* Set the Pulse value */
TIM2->CCR1H = (uint8_t)(TIM2_Pulse >> 8);
TIM2->CCR1L = (uint8_t)(TIM2_Pulse);
}
/**
* @brief Initializes the TIM2 Channel2 according to the specified parameters.
* @param TIM2_OCMode: Output Compare Mode
* This parameter can be one of the following values:
* @arg TIM2_OCMode_Timing: Timing (Frozen) Mode
* @arg TIM2_OCMode_Active: Active Mode
* @arg TIM2_OCMode_Inactive: Inactive Mode
* @arg TIM2_OCMode_Toggle: Toggle Mode
* @arg TIM2_OCMode_PWM1: PWM Mode 1
* @arg TIM2_OCMode_PWM2: PWM Mode 2
* @param TIM2_OutputState: Output state
* This parameter can be one of the following values:
* @arg TIM2_OutputState_Disable: Output compare State disabled (channel output disabled)
* @arg TIM2_OutputState_Enable: Output compare State enabled (channel output enabled)
* @param TIM2_Pulse: This parameter must be a value between 0x0000 and 0xFFFF.
* @param TIM2_OCPolarity: Polarity
* This parameter can be one of the following values:
* @arg TIM2_OCPolarity_High: Output compare polarity = High
* @arg TIM2_OCPolarity_Low: Output compare polarity = Low
* @param TIM2_OCIdleState: Output Compare Idle State
* This parameter can be one of the following values:
* @arg TIM2_OCIdleState_Reset: Output Compare Idle state = Reset
* @arg TIM2_OCIdleState_Set: Output Compare Idle state = Set
* @retval None
*/
void TIM2_OC2Init(TIM2_OCMode_TypeDef TIM2_OCMode,
TIM2_OutputState_TypeDef TIM2_OutputState,
uint16_t TIM2_Pulse,
TIM2_OCPolarity_TypeDef TIM2_OCPolarity,
TIM2_OCIdleState_TypeDef TIM2_OCIdleState)
{
uint8_t tmpccmr2 = 0;
/* Check the parameters */
assert_param(IS_TIM2_OC_MODE(TIM2_OCMode));
assert_param(IS_TIM2_OUTPUT_STATE(TIM2_OutputState));
assert_param(IS_TIM2_OC_POLARITY(TIM2_OCPolarity));
assert_param(IS_TIM2_OCIDLE_STATE(TIM2_OCIdleState));
tmpccmr2 = TIM2->CCMR2;
/* Disable the Channel 2: Reset the CCE Bit */
TIM2->CCER1 &= (uint8_t)(~TIM_CCER1_CC2E);
/* Reset the Output Compare Bits */
tmpccmr2 &= (uint8_t)(~TIM_CCMR_OCM);
/* Set the Output Compare Mode */
tmpccmr2 |= (uint8_t)TIM2_OCMode;
TIM2->CCMR2 = tmpccmr2;
/* Set the Output State */
if (TIM2_OutputState == TIM2_OutputState_Enable)
{
TIM2->CCER1 |= TIM_CCER1_CC2E;
}
else
{
TIM2->CCER1 &= (uint8_t)(~TIM_CCER1_CC2E);
}
/* Set the Output Polarity */
if (TIM2_OCPolarity == TIM2_OCPolarity_Low)
{
TIM2->CCER1 |= TIM_CCER1_CC2P;
}
else
{
TIM2->CCER1 &= (uint8_t)(~TIM_CCER1_CC2P);
}
/* Set the Output Idle state */
if (TIM2_OCIdleState == TIM2_OCIdleState_Set)
{
TIM2->OISR |= TIM_OISR_OIS2;
}
else
{
TIM2->OISR &= (uint8_t)(~TIM_OISR_OIS2);
}
/* Set the Pulse value */
TIM2->CCR2H = (uint8_t)(TIM2_Pulse >> 8);
TIM2->CCR2L = (uint8_t)(TIM2_Pulse);
}
/**
* @brief Configures the Break feature, dead time, Lock level, the OSSI,
* and the AOE(automatic output enable).
* @param TIM2_OSSIState: Off-State Selection for Idle mode states.
* This parameter can be one of the following values:
* @arg TIM2_OSSIState_Enable: Off-State Selection for Idle mode enabled
* @arg TIM2_OSSIState_Disable: Off-State Selection for Idle mode disabled
* @param TIM2_LockLevel: Lock level.
* This parameter can be one of the following values:
* @arg TIM2_LockLevel_Off: Lock option disabled
* @arg TIM2_LockLevel_1: Select Lock Level 1
* @arg TIM2_LockLevel_2: Select Lock Level 2
* @arg TIM2_LockLevel_3: Select Lock Level 3
* @param TIM2_BreakState: Break Input enable/disable .
* This parameter can be one of the following values:
* @arg TIM2_BreakState_Disable: Break State disabled (break option disabled)
* @arg TIM2_BreakState_Enable: Break State enabled (break option enabled)
* @param TIM2_BreakPolarity: Break Polarity.
* This parameter can be one of the following values:
* @arg TIM2_BreakPolarity_High: if Break, channel polarity = High
* @arg TIM2_BreakPolarity_Low: if Break, channel polarity = Low
* @param TIM2_AutomaticOutput: TIM2 AOE Bit Set/Reset .
* This parameter can be one of the following values:
* @arg TIM2_AutomaticOutput_Enable: Automatic Output option enabled
* @arg TIM2_AutomaticOutput_Disable: Automatic Output option disabled
* @retval None
*/
void TIM2_BKRConfig(TIM2_OSSIState_TypeDef TIM2_OSSIState,
TIM2_LockLevel_TypeDef TIM2_LockLevel,
TIM2_BreakState_TypeDef TIM2_BreakState,
TIM2_BreakPolarity_TypeDef TIM2_BreakPolarity,
TIM2_AutomaticOutput_TypeDef TIM2_AutomaticOutput)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?