stm8l15x_tim1.c
来自「STM8L的tim4定时器使用」· C语言 代码 · 共 1,720 行 · 第 1/5 页
C
1,720 行
{
TIM1->CCER2 |= TIM1_CCER2_CC3P;
}
else
{
TIM1->CCER2 &= (uint8_t)(~TIM1_CCER2_CC3P);
}
}
/**
* @brief Configures the TIM1 Channel 3N polarity.
* @param TIM1_OCNPolarity specifies the OC3N Polarity.
* This parameter can be one of the following values:
* @arg TIM1_OCNPolarity_High: Complementary Output Compare active low
* @arg TIM1_OCNPolarity_Low: Complementary Output Compare active high
* @retval None
*/
void TIM1_OC3NPolarityConfig(TIM1_OCNPolarity_TypeDef TIM1_OCNPolarity)
{
/* Check the parameters */
assert_param(IS_TIM1_OCN_POLARITY(TIM1_OCNPolarity));
/* Set or Reset the CC3P Bit */
if (TIM1_OCNPolarity != TIM1_OCNPolarity_High)
{
TIM1->CCER2 |= TIM1_CCER2_CC3NP;
}
else
{
TIM1->CCER2 &= (uint8_t)(~TIM1_CCER2_CC3NP);
}
}
/**
* @brief Selects the OCReference Clear source.
* @param TIM1_OCReferenceClear: specifies the OCReference Clear source.
* This parameter can be one of the following values:
* @arg TIM1_OCReferenceClear_ETRF: OCReference Clear source ETR
* @arg TIM1_OCReferenceClear_OCREFCLR: OCReference Clear source OCREF
* @retval None
*/
void TIM1_SelectOCREFClear(TIM1_OCReferenceClear_TypeDef TIM1_OCReferenceClear)
{
/* Check the parameters */
assert_param(IS_TIM1_OCREFERENCECECLEAR_SOURCE(TIM1_OCReferenceClear));
/* Set the TIM1_OCReferenceClear source */
TIM1->SMCR &= (uint8_t) (~TIM1_SMCR_OCCS);
TIM1->SMCR |= (uint8_t) TIM1_OCReferenceClear;
}
/**
* @brief Selects the TIM1 peripheral Commutation event.
* @param NewState new state of the Commutation event.
* This parameter can be ENABLE or DISABLE.
* @retval None
*/
void TIM1_SelectCOM(FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_FUNCTIONAL_STATE(NewState));
/* Set or Reset the CCUS Bit */
if (NewState != DISABLE)
{
TIM1->CR2 |= TIM1_CR2_CCUS;
}
else
{
TIM1->CR2 &= (uint8_t)(~TIM1_CR2_CCUS);
}
}
/**
* @brief Enables or disables the TIM1 Capture Compare Channel x (x=1,..,4).
* @param TIM1_Channel specifies the TIM1 Channel.
* This parameter can be one of the following values:
* @arg TIM1_Channel_1: TIM1 Channel1
* @arg TIM1_Channel_2: TIM1 Channel2
* @arg TIM1_Channel_3: TIM1 Channel3
* @arg TIM1_CHANNEL_4: TIM1 Channel4
* @param NewState specifies the TIM1 Channel CCxE bit new state.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void TIM1_CCxCmd(TIM1_Channel_TypeDef TIM1_Channel, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_TIM1_CHANNEL(TIM1_Channel));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (TIM1_Channel == TIM1_Channel_1)
{
/* Set or Reset the CC1E Bit */
if (NewState != DISABLE)
{
TIM1->CCER1 |= TIM1_CCER1_CC1E;
}
else
{
TIM1->CCER1 &= (uint8_t)(~TIM1_CCER1_CC1E);
}
}
else if (TIM1_Channel == TIM1_Channel_2)
{
/* Set or Reset the CC2E Bit */
if (NewState != DISABLE)
{
TIM1->CCER1 |= TIM1_CCER1_CC2E;
}
else
{
TIM1->CCER1 &= (uint8_t)(~TIM1_CCER1_CC2E);
}
}
else if (TIM1_Channel == TIM1_Channel_3)
{
/* Set or Reset the CC3E Bit */
if (NewState != DISABLE)
{
TIM1->CCER2 |= TIM1_CCER2_CC3E;
}
else
{
TIM1->CCER2 &= (uint8_t)(~TIM1_CCER2_CC3E);
}
}
else
{
/* Set or Reset the CC4E Bit */
if (NewState != DISABLE)
{
TIM1->CCER2 |= TIM1_CCER2_CC4E;
}
else
{
TIM1->CCER2 &= (uint8_t)(~TIM1_CCER2_CC4E);
}
}
}
/**
* @brief Enables or disables the TIM1 Capture Compare Channel xN (xN=1,..,3).
* @param TIM1_Channel specifies the TIM1 Channel.
* This parameter can be one of the following values:
* @arg TIM1_Channel_1: TIM1 Channel1
* @arg TIM1_Channel_2: TIM1 Channel2
* @arg TIM1_Channel_3: TIM1 Channel3
* @param NewState specifies the TIM1 Channel CCxNE bit new state.
* This parameter can be: ENABLE or DISABLE.
* @retval None
*/
void TIM1_CCxNCmd(TIM1_Channel_TypeDef TIM1_Channel, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_TIM1_COMPLEMENTARY_CHANNEL(TIM1_Channel));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (TIM1_Channel == TIM1_Channel_1)
{
/* Set or Reset the CC1NE Bit */
if (NewState != DISABLE)
{
TIM1->CCER1 |= TIM1_CCER1_CC1NE;
}
else
{
TIM1->CCER1 &= (uint8_t)(~TIM1_CCER1_CC1NE);
}
}
else if (TIM1_Channel == TIM1_Channel_2)
{
/* Set or Reset the CC2NE Bit */
if (NewState != DISABLE)
{
TIM1->CCER1 |= TIM1_CCER1_CC2NE;
}
else
{
TIM1->CCER1 &= (uint8_t)(~TIM1_CCER1_CC2NE);
}
}
else
{
/* Set or Reset the CC3NE Bit */
if (NewState != DISABLE)
{
TIM1->CCER2 |= TIM1_CCER2_CC3NE;
}
else
{
TIM1->CCER2 &= (uint8_t)(~TIM1_CCER2_CC3NE);
}
}
}
/**
* @}
*/
/** @defgroup TIM1_Group3 Input Capture management functions
* @brief Input Capture management functions
*
@verbatim
===============================================================================
Input Capture management functions
===============================================================================
===================================================================
TIM1 Driver: how to use it in Input Capture Mode
===================================================================
To use the Timer in Input Capture mode, the following steps are mandatory:
1. Enable TIM1 clock using CLK_PeripheralClockConfig(CLK_Peripheral_TIM1, ENABLE) function.
2. Configure the TIM1 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 = 0x0000
- Counter mode = Up counting
4. Call TIM1_ICInit() to configure the desired channel to measure only
frequency or duty cycle of the input signal using the corresponding configuration:
- TIM1 Channel: TIM1_Channel
- TIM1 Input Capture polarity: TIM1_ICPolarity
- TIM1 Input Capture selection: TIM1_ICSelection
- TIM1 Input Capture Prescaler: TIM1_ICPSC
- TIM1 Input Capture filter value
or,
Call TIM1_PWMIConfig() to configure the desired channels with the
corresponding configuration and to measure the frequency and the duty
cycle of the input signal.
5. Enable global interrupts or the DMA to read the measured frequency.
6. Enable the corresponding interrupt (or DMA request) to read the captured value,
using the function TIM1_ITConfig(TIM1_IT_CCx) (or TIM1_DMACmd(TIM1_DMASource_CCx))
7. Call the TIM1_Cmd(ENABLE) function to enable the TIM1 counter.
8. Use TIM1_GetCapturex() to read the captured value corresponding to
channel x.
Note1: All other functions can be used separately to modify, if needed,
a specific feature of the Timer.
@endverbatim
* @{
*/
/**
* @brief Initializes the TIM1 peripheral according to the specified parameters.
* @param TIM1_Channel specifies the input capture channel
* This parameter can be one of the following values:
* @arg TIM1_Channel_1: TIM1 Channel1
* @arg TIM1_Channel_2: TIM1 Channel2
* @arg TIM1_Channel_3: TIM1 Channel3
* @arg TIM1_Channel_4: TIM1 Channel4
* @param TIM1_ICPolarity specifies the Input capture polarity
* This parameter can be one of the following values:
* @arg TIM1_ICPolarity_Rising: Input capture polarity rising
* @arg TIM1_ICPolarity_Falling: Input capture polarity falling
* @param TIM1_ICSelection specifies the Input capture source selection
* This parameter can be one of the following values:
* @arg TIM1_ICSelection_DirectTI: TIM1 Input x is selected to be connected to ICx.
* @arg TIM1_ICSelection_IndirectTI: TIM1 Input x is selected to be connected to ICy.
* @arg TIM1_ICSelection_TRGI: TIM1 Input x is selected to be connected to the TRGI
* @param TIM1_ICPrescaler specifies the Input capture Prescaler
* This parameter can be one of the following values:
* @arg TIM1_ICPSC_DIV1: Input Capture Prescaler = 1 (one capture every 1 event)
* @arg TIM1_ICPSC_DIV2: Input Capture Prescaler = 2 (one capture every 2 events)
* @arg TIM1_ICPSC_DIV4: Input Capture Prescaler = 4 (one capture every 4 events)
* @arg TIM1_ICPSC_DIV8: Input Capture Prescaler = 8 (one capture every 8 events)
* @param TIM1_ICFilter specifies the Input capture filter value.
* @note If the channel 3 is selected the TIM1_ICSelection_IndirectTI parameter is forbidden.
* @note If the channel 4 is selected the TIM1_ICSelection_DirectTI parameter is forbidden.
* @retval None
*/
void TIM1_ICInit(TIM1_Channel_TypeDef TIM1_Channel,
TIM1_ICPolarity_TypeDef TIM1_ICPolarity,
TIM1_ICSelection_TypeDef TIM1_ICSelection,
TIM1_ICPSC_TypeDef TIM1_ICPrescaler,
uint8_t TIM1_ICFilter)
{
/* Check the parameters */
assert_param(IS_TIM1_CHANNEL(TIM1_Channel));
assert_param(IS_TIM1_IC_POLARITY(TIM1_ICPolarity));
assert_param(IS_TIM1_IC_SELECTION(TIM1_ICSelection));
assert_param(IS_TIM1_IC_PRESCALER(TIM1_ICPrescaler));
assert_param(IS_TIM1_IC_FILTER(TIM1_ICFilter));
if (TIM1_Channel == TIM1_Channel_1)
{
/* TI1 Configuration */
TI1_Config(TIM1_ICPolarity, TIM1_ICSelection, TIM1_ICFilter);
/* Set the Input Capture Prescaler value */
TIM1_SetIC1Prescaler(TIM1_ICPrescaler);
}
else if (TIM1_Channel == TIM1_Channel_2)
{
/* TI2 Configuration */
TI2_Config(TIM1_ICPolarity, TIM1_ICSelection, TIM1_ICFilter);
/* Set the Input Capture Prescaler value */
TIM1_SetIC2Prescaler(TIM1_ICPrescaler);
}
else if (TIM1_Channel == TIM1_Channel_3)
{
/* TI3 Configuration */
TI3_Config(TIM1_ICPolarity, TIM1_ICSelection, TIM1_ICFilter);
/* Set the Input Capture Prescaler value */
TIM1_SetIC3Prescaler(TIM1_ICPrescaler);
}
else
{
/* TI4 Configuration */
TI4_Config(TIM1_ICPolarity, TIM1_ICSelection, TIM1_ICFilter);
/* Set the Input Capture Prescaler value */
TIM1_SetIC4Prescaler(TIM1_ICPrescaler);
}
}
/**
* @brief Configures the TIM1 peripheral in PWM Input Mode according to the
* specified parameters.
* @param TIM1_Channel specifies the input capture channel
* This parameter can be one of the following values:
* @arg TIM1_Channel_1: TIM1 Channel1
* @arg TIM1_Channel_2: TIM1 Channel2
* @param TIM1_ICPolarity specifies the Input capture polarity
* This parameter can be one of the following values:
* @arg TIM1_ICPolarity_Rising: Input capture polarity rising
* @arg TIM1_ICPolarity_Falling: Input capture polarity falling
* @param TIM1_ICSelection specifies the Input capture source selection
* This parameter can be one of the following values:
* @arg TIM1_ICSelection_DirectTI: TIM1 Input x is selected to be connected to ICx.
* @arg TIM1_ICSelection_IndirectTI: TIM1 Input x is selected to be connected to ICy.
* @arg TIM1_ICSelection_TRGI: TIM1 Input x is selected to be connected to the TRGI
* @param TIM1_ICPrescaler specifies the Input capture Prescaler
* This parameter can be one of the following values:
* @arg TIM1_ICPSC_DIV1: In
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?