📄 stm8l15x_tim3.c
字号:
- TIM3 Input Capture selection: TIM3_ICSelection
- TIM3 Input Capture Prescaler: TIM3_ICPSC
- TIM3 Input Capture filter value
or,
Call TIM3_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 TIM3_ITConfig(TIM3_IT_CCx) (or TIM3_DMACmd(TIM3_DMASource_CCx))
7. Call the TIM3_Cmd(ENABLE) function to enable the TIM3 counter.
8. Use TIM3_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 TIM3 peripheral according to the specified parameters.
* @param TIM3_Channel: TIM3 Channel
* This parameter can be one of the following values:
* @arg TIM3_Channel_1: Channel 1
* @arg TIM3_Channel_2: Channel 2
* @param TIM3_ICPolarity: Input Capture Polarity
* This parameter can be one of the following values:
* @arg TIM3_ICPolarity_Rising: Input Capture on Rising Edge
* @arg TIM3_ICPolarity_Falling: Input Capture on Falling Edge
* @param TIM3_ICSelection: Input Capture Selection
* This parameter can be one of the following values:
* @arg TIM3_ICSelection_DirectTI: Input Capture mapped on the direct input
* @arg TIM3_ICSelection_IndirectTI: Input Capture mapped on the indirect input
* @arg TIM3_ICSelection_TRGI: Input Capture mapped on the Trigger Input
* @param TIM3_ICPrescaler: Input Capture Prescaler
* This parameter can be one of the following values:
* @arg TIM3_ICPSC_DIV1: Input Capture Prescaler = 1 (one capture every 1 event)
* @arg TIM3_ICPSC_DIV2: Input Capture Prescaler = 2 (one capture every 2 events)
* @arg TIM3_ICPSC_DIV4: Input Capture Prescaler = 4 (one capture every 4 events)
* @arg TIM3_ICPSC_DIV8: Input Capture Prescaler = 8 (one capture every 8 events)
* @param TIM3_ICFilter: This parameter must be a value between 0x00 and 0x0F.
* @retval None
*/
void TIM3_ICInit(TIM3_Channel_TypeDef TIM3_Channel,
TIM3_ICPolarity_TypeDef TIM3_ICPolarity,
TIM3_ICSelection_TypeDef TIM3_ICSelection,
TIM3_ICPSC_TypeDef TIM3_ICPrescaler,
uint8_t TIM3_ICFilter)
{
/* Check the parameters */
assert_param(IS_TIM3_CHANNEL(TIM3_Channel));
if (TIM3_Channel == TIM3_Channel_1)
{
/* TI1 Configuration */
TI1_Config(TIM3_ICPolarity, TIM3_ICSelection, TIM3_ICFilter);
/* Set the Input Capture Prescaler value */
TIM3_SetIC1Prescaler(TIM3_ICPrescaler);
}
else /* if (TIM3_Channel == TIM3_Channel_2) */
{
/* TI2 Configuration */
TI2_Config(TIM3_ICPolarity, TIM3_ICSelection, TIM3_ICFilter);
/* Set the Input Capture Prescaler value */
TIM3_SetIC2Prescaler(TIM3_ICPrescaler);
}
}
/**
* @brief Configures the TIM3 peripheral in PWM Input Mode according to the
* specified parameters.
* @param TIM3_Channel: TIM3 Channel
* This parameter can be one of the following values:
* @arg TIM3_Channel_1: Channel 1
* @arg TIM3_Channel_2: Channel 2
* @param TIM3_ICPolarity: Input Capture Polarity
* This parameter can be one of the following values:
* @arg TIM3_ICPolarity_Rising: Input Capture on Rising Edge
* @arg TIM3_ICPolarity_Falling: Input Capture on Falling Edge
* @param TIM3_ICSelection: Input Capture Selection
* This parameter can be one of the following values:
* @arg TIM3_ICSelection_DirectTI: Input Capture mapped on the direct input
* @arg TIM3_ICSelection_IndirectTI: Input Capture mapped on the indirect input
* @arg TIM3_ICSelection_TRGI: Input Capture mapped on the Trigger Input
* @param TIM3_ICPrescaler: Input Capture Prescaler
* This parameter can be one of the following values:
* @arg TIM3_ICPSC_DIV1: Input Capture Prescaler = 1 (one capture every 1 event)
* @arg TIM3_ICPSC_DIV2: Input Capture Prescaler = 2 (one capture every 2 events)
* @arg TIM3_ICPSC_DIV4: Input Capture Prescaler = 4 (one capture every 4 events)
* @arg TIM3_ICPSC_DIV8: Input Capture Prescaler = 8 (one capture every 8 events)
* @retval None
*/
void TIM3_PWMIConfig(TIM3_Channel_TypeDef TIM3_Channel,
TIM3_ICPolarity_TypeDef TIM3_ICPolarity,
TIM3_ICSelection_TypeDef TIM3_ICSelection,
TIM3_ICPSC_TypeDef TIM3_ICPrescaler,
uint8_t TIM3_ICFilter)
{
uint8_t icpolarity = TIM3_ICPolarity_Rising;
uint8_t icselection = TIM3_ICSelection_DirectTI;
/* Check the parameters */
assert_param(IS_TIM3_CHANNEL(TIM3_Channel));
/* Select the Opposite Input Polarity */
if (TIM3_ICPolarity == TIM3_ICPolarity_Rising)
{
icpolarity = TIM3_ICPolarity_Falling;
}
else
{
icpolarity = TIM3_ICPolarity_Rising;
}
/* Select the Opposite Input */
if (TIM3_ICSelection == TIM3_ICSelection_DirectTI)
{
icselection = TIM3_ICSelection_IndirectTI;
}
else
{
icselection = TIM3_ICSelection_DirectTI;
}
if (TIM3_Channel == TIM3_Channel_1)
{
/* TI1 Configuration */
TI1_Config(TIM3_ICPolarity, TIM3_ICSelection,
TIM3_ICFilter);
/* Set the Input Capture Prescaler value */
TIM3_SetIC1Prescaler(TIM3_ICPrescaler);
/* TI2 Configuration */
TI2_Config((TIM3_ICPolarity_TypeDef)icpolarity, (TIM3_ICSelection_TypeDef)icselection, TIM3_ICFilter);
/* Set the Input Capture Prescaler value */
TIM3_SetIC2Prescaler(TIM3_ICPrescaler);
}
else
{
/* TI2 Configuration */
TI2_Config(TIM3_ICPolarity, TIM3_ICSelection,
TIM3_ICFilter);
/* Set the Input Capture Prescaler value */
TIM3_SetIC2Prescaler(TIM3_ICPrescaler);
/* TI1 Configuration */
TI1_Config((TIM3_ICPolarity_TypeDef)icpolarity, (TIM3_ICSelection_TypeDef)icselection, TIM3_ICFilter);
/* Set the Input Capture Prescaler value */
TIM3_SetIC1Prescaler(TIM3_ICPrescaler);
}
}
/**
* @brief Gets the TIM3 Input Capture 1 value.
* @param None
* @retval Capture Compare 1 Register value.
*/
uint16_t TIM3_GetCapture1(void)
{
uint16_t tmpccr1 = 0;
uint8_t tmpccr1l, tmpccr1h;
tmpccr1h = TIM3->CCR1H;
tmpccr1l = TIM3->CCR1L;
tmpccr1 = (uint16_t)(tmpccr1l);
tmpccr1 |= (uint16_t)((uint16_t)tmpccr1h << 8);
/* Get the Capture 1 Register value */
return ((uint16_t)tmpccr1);
}
/**
* @brief Gets the TIM3 Input Capture 2 value.
* @param None
* @retval Capture Compare 2 Register value.
*/
uint16_t TIM3_GetCapture2(void)
{
uint16_t tmpccr2 = 0;
uint8_t tmpccr2l, tmpccr2h;
tmpccr2h = TIM3->CCR2H;
tmpccr2l = TIM3->CCR2L;
tmpccr2 = (uint16_t)(tmpccr2l);
tmpccr2 |= (uint16_t)((uint16_t)tmpccr2h << 8);
/* Get the Capture 2 Register value */
return ((uint16_t)tmpccr2);
}
/**
* @brief Sets the TIM3 Input Capture 1 prescaler.
* @param TIM3_IC1Prescaler: Specifies the Input Capture prescaler new value
* This parameter can be one of the following values:
* @arg TIM3_ICPSC_DIV1: Input Capture Prescaler = 1 (one capture every 1 event)
* @arg TIM3_ICPSC_DIV2: Input Capture Prescaler = 2 (one capture every 2 events)
* @arg TIM3_ICPSC_DIV4: Input Capture Prescaler = 4 (one capture every 4 events)
* @arg TIM3_ICPSC_DIV8: Input Capture Prescaler = 8 (one capture every 8 events)
* @retval None
*/
void TIM3_SetIC1Prescaler(TIM3_ICPSC_TypeDef TIM3_IC1Prescaler)
{
uint8_t tmpccmr1 = 0;
/* Check the parameters */
assert_param(IS_TIM3_IC_PRESCALER(TIM3_IC1Prescaler));
tmpccmr1 = TIM3->CCMR1;
/* Reset the IC1PSC Bits */
tmpccmr1 &= (uint8_t)(~TIM_CCMR_ICxPSC);
/* Set the IC1PSC value */
tmpccmr1 |= (uint8_t)TIM3_IC1Prescaler;
TIM3->CCMR1 = tmpccmr1;
}
/**
* @brief Sets the TIM3 Input Capture 2 prescaler.
* @param TIM3_IC2Prescaler: Specifies the Input Capture prescaler new value
* This parameter can be one of the following values:
* @arg TIM3_ICPSC_DIV1: Input Capture Prescaler = 1 (one capture every 1 event)
* @arg TIM3_ICPSC_DIV2: Input Capture Prescaler = 2 (one capture every 2 events)
* @arg TIM3_ICPSC_DIV4: Input Capture Prescaler = 4 (one capture every 4 events)
* @arg TIM3_ICPSC_DIV8: Input Capture Prescaler = 8 (one capture every 8 events)
* @retval None
*/
void TIM3_SetIC2Prescaler(TIM3_ICPSC_TypeDef TIM3_IC2Prescaler)
{
uint8_t tmpccmr2 = 0;
/* Check the parameters */
assert_param(IS_TIM3_IC_PRESCALER(TIM3_IC2Prescaler));
tmpccmr2 = TIM3->CCMR2;
/* Reset the IC2PSC Bits */
tmpccmr2 &= (uint8_t)(~TIM_CCMR_ICxPSC);
/* Set the IC2PSC value */
tmpccmr2 |= (uint8_t)TIM3_IC2Prescaler;
TIM3->CCMR2 = tmpccmr2;
}
/**
* @}
*/
/** @defgroup TIM3_Group4 Interrupts DMA and flags management functions
* @brief Interrupts, DMA and flags management functions
*
@verbatim
===============================================================================
Interrupts, DMA and flags management functions
===============================================================================
@endverbatim
* @{
*/
/**
* @brief Enables or disables the specified TIM3 interrupts.
* @param TIM3_IT: Specifies the TIM3 interrupts sources to be enabled or disabled.
* This parameter can be any combination of the following values:
* @arg TIM3_IT_Update: Update
* @arg TIM3_IT_CC1: Capture Compare Channel1
* @arg TIM3_IT_CC2: Capture Compare Channel2
* @arg TIM3_IT_Trigger: Trigger
* @arg TIM3_IT_Break: Break
* @param NewState: The new state of the TIM3 peripheral.
* This parameter can be ENABLE or DISABLE
* @retval None
*/
void TIM3_ITConfig(TIM3_IT_TypeDef TIM3_IT, FunctionalState NewState)
{
/* Check the parameters */
assert_param(IS_TIM3_IT(TIM3_IT));
assert_param(IS_FUNCTIONAL_STATE(NewState));
if (NewState != DISABLE)
{
/* Enable the Interrupt sources */
TIM3->IER |= (uint8_t)TIM3_IT;
}
else
{
/* Disable the Interrupt sources */
TIM3->IER &= (uint8_t)(~(uint8_t)TIM3_IT);
}
}
/**
* @brief Configures the TIM3 event to be generated by software.
* @param TIM3_EventSource: Specifies the event source.
* This parameter can be any combination of the following values:
* @arg TIM3_EventSource_Update: Update
* @arg TIM3_EventSource_CC1: Capture Compare Channel1
* @arg TIM3_EventSource_CC2: Capture Compare Channel2
* @arg TIM3_EventSource_Trigger: Trigger
* @arg TIM3_EventSource_Break: Break
* @retval None
*/
void TIM3_GenerateEvent(TIM3_EventSource_TypeDef TIM3_EventSource)
{
/* Check the parameters */
assert_param(IS_TIM3_EVENT_SOURCE((uint8_t)TIM3_EventSource));
/* Set the event sources */
TIM3->EGR |= (uint8_t)TIM3_EventSource;
}
/**
* @brief Checks whether the specified TIM3 flag is set or not.
* @param TIM3_FLAG: Specifies the flag to check.
* This parameter can be any combination of the following values:
* @arg TIM3_FLAG_Update: Update
* @arg TIM3_FLAG_CC1: Capture Compare Channel1
* @arg TIM3_FLAG_CC2: Capture Compare Channel2
* @arg TIM3_FLAG_Trigger: Trigger
* @arg TIM3_FLAG_Break: Break
* @arg TIM3_FLAG_CC1OF: Capture compare 1 over capture
* @arg TIM3_FLAG_CC2OF: Capture compare 2 over capture
* @retval FlagStatus: The new state of TIM3_FLAG (SET or RESET)
*/
FlagStatus TIM3_GetFlagStatus(TIM3_FLAG_TypeDef TIM3_FLAG)
{
FlagStatus bitstatus = RESET;
uint8_t tim3_flag_l = 0, tim3_flag_h = 0;
/* Check the parameters */
assert_param(IS_TIM3_GET_FLAG(TIM3_FLAG));
tim3_flag_l = (uint8_t)(TIM3->SR1 & (uint8_t)(TIM3_FLAG));
tim3_flag_h = (uint8_t)(TIM3->SR2 & (uint8_t)((uint16_t)TIM3_FLAG >> 8));
if ((uint8_t)(tim3_flag_l | tim3_flag_h) != 0)
{
bitstatus = SET;
}
else
{
bitstatus = RESET;
}
return ((FlagStatus)bitstatus);
}
/**
* @brief Clears the TIM抯 pending flags.
* @param TIM3_FLAG: Specifies the flag to clear.
* This parameter can be any combination of the following values:
* @arg TIM3_FLAG_Update: Update
* @arg TIM3_FLAG_CC1: Capture Compare Channel1
* @arg TIM3_FLAG_CC2: Capture Compare Channel2
* @arg TIM3_FLAG_Trigger: Trigger
* @arg TIM3_FLAG_Break: Break
* @retval None
*/
void TIM3_ClearFlag(TIM3_FLAG_TypeDef TIM3_FLAG)
{
/* Check the parameters */
assert_param(IS_TIM3_CLEAR_FLAG((uint16_t)TIM3_FLAG));
/* Clear the flags (rc_w0) clear this bit by writing 0. Writing
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -