STM32 input capture working process (channel 1 as an example)
Summarize the working process in one sentence: By detecting the edge signal on TIMx_CHx , when the edge signal jumps (such as rising edge / falling edge), store the current timer value ( TIMx_CNT ) into the corresponding capture / compare register ( TIMx_CCRx ) to complete a capture.
Step 1 Set the input capture filter (channel 1 as an example)
Step 2: Set input capture polarity (channel 1 as an example)
Step 3: Set the input capture mapping channel (channel 1 as an example)
Step 4: Set the input capture divider (channel 1 as an example)
typedef struct
{
uint16_t TIM_Channel; //捕获通道1-4
uint16_t TIM_ICPolarity; //捕获极性
uint16_t TIM_ICSelection; //映射关系
uint16_t TIM_ICPrescaler; //分频系数
uint16_t TIM_ICFilter; //滤波器
} TIM_ICInitTypeDef;
TIM5_ICInitStructure.TIM_Channel = TIM_Channel_1; TIM5_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;
TIM5_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;
TIM5_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;
TIM5_ICInitStructure.TIM_ICFilter = 0x00;
TIM_ICInit(TIM5, &TIM5_ICInitStructure);
void TIM_OCxPolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity);
uint32_t TIM_GetCapture1(TIM_TypeDef* TIMx);
General configuration steps for input capture:
① Initialize the timer and the clock of the IO corresponding to the channel .
② Initialize the IO port, the mode is input: GPIO_Init ();
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PA0 输入
③Initialize timer ARR , PSC
TIM_TimeBaseInit ();
④Initialize input capture channel
TIM_ICInit ();
⑤If you want to enable capture interrupt,
TIM_ITConfig();
NVIC_Init();
⑥Enable timer: TIM_Cmd ();
⑦Write the interrupt service function: TIMx_IRQHandler ();
Experiment purpose: measure the pulse width of the signal
Important code:
timer.c:
main.c: