STM32 input capture function

table of Contents

01, STM32 capture function

02, input capture process

03, code configuration


This article will introduce the timer input capture through STM32. If you are not familiar with timers, you can read the previous article " STM32 Basic Timer Detailed Explanation ". The basic functions of timers will not be explained in detail. The capture function of timer1 will be explained below.

01, STM32 capture function

Seen from the timer block diagram of STM32

① Some basic timer modules have been explained in " STM32 Basic Timer Detailed Explanation ".

②Part of the capture/comparison channel module has been explained in " Timer's PWM Function ".

③Part is the focus of this article, the input capture module.

It can be seen from the figure above that Timer 1 has 4 input capture channels.

 

The following takes the capture channel 2 of timer 1 as an example to explain.

First, determine the corresponding GPIO. From the Alternatefunction mapping in the STM32F207 data manual, it can be seen that the GPIOs corresponding to channel 2 of timer 1 are PA9 and PE11, and PE11 will be used below.

02, input capture process

The input stage samples the corresponding input TIx to generate the filtered signal TIxF. Then the polarity selection edge detector generates a signal (TIxFPx), which can be used as a trigger input for the service mode controller or as a capture command. It is prescaled before the capture register.

The output stage produces an intermediate waveform, which is used as a reference: OCxRef (active high). The end of the chain determines the polarity. Capture/compare channel 1 main circuit.

① is mainly to set the input capture filter, the input stage samples the corresponding input TIx to generate the filtered signal TIxF. In general application scenarios, no filtering is performed. Set the ICF[3:0] of TIMx_CCMR1 to 0000. As long as the rising edge is collected, the capture is triggered.

②The main point is to set the input capture polarity and set the CC1P or CC1NP bit of TIMx_CCER. If the channel is configured as output, 0 means high level is valid, 1 means low level is valid; if configured as input mode, the bit selection is yes IC1 is also the inverted signal of IC1 as the trigger or capture signal. 0 means no inversion, 1 means inversion.

③Set the input capture mapping channel, set the CC1S[1:0] bits of TIMx_CCMR1, these 2 bits define the direction of the channel (input/output), and the selection of the input pin:

0o: CC1 channel is configured as output;

01: The CC1 channel is configured as an input, and IC1 is mapped on Tl1;

10: The CC1 channel is configured as an input, and IC1 is mapped on TI2;

11: The CC1 channel is configured as an input, and IC1 is mapped on the TRC. This mode only works when the internal trigger input is selected (selected by the TS bit in the TIMx_SMCR register).

④ Set the input capture divider, ICPS[1:0] bit of TIMx_CCMR1 and CC1E bit of TIMx_CCER.

 

ICPS[1:0] of TIMx_CCMR1 defines the prescaler coefficient of CC1 input (IC1). Once CC1E='O (in the TIMx_CCER register), the prescaler is reset.

00: No prescaler, every edge detected on the capture input port triggers a capture;

01: Trigger a capture every 2 events;

10: Trigger a capture every 4 events;

11: Trigger a capture every 8 events.

 

CC1E bit of TIMx_CCER

CC1 channel is configured as output:

0: Turn off an OC1 and disable output.

1: Turn on an OC1 signal to output to the corresponding output pin.

CC1 channel is configured as input:

This bit determines whether the value of the counter can be captured into the TIMx_CCR1 register.

0: Capture prohibited;

0: Capture enable.

 

Finally, we also need to set up an interrupt to make the system respond quickly to the input capture signal, mainly the TIMx_DIER register, as follows:

Enable to allow update interrupt and channel 2 interrupt

03, code configuration

Set GPIO multiplexing

/* TIM1 channel 2 pin (PE.11) configuration*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed =GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType =GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd =GPIO_PuPd_NOPULL;
GPIO_Init(GPIOE, &GPIO_InitStructure);

/*Connect TIM pins to AF2 */
GPIO_PinAFConfig(GPIOE,GPIO_PinSource11, GPIO_AF_TIM1);

Set timing basic functions

/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period =0XFFFF;
TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t)((SystemCoreClock) / 1000000) -1;
TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode =TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);

Configure channel 2

TIM_ICInitStructure.TIM_Channel =TIM_Channel_2;
TIM_ICInitStructure.TIM_ICPolarity =TIM_ICPolarity_Falling;
TIM_ICInitStructure.TIM_ICSelection =TIM_ICSelection_DirectTI;
TIM_ICInitStructure.TIM_ICPrescaler =TIM_ICPSC_DIV1;
TIM_ICInitStructure.TIM_ICFilter =0x0;

TIM_ICInit(TIM1, &TIM_ICInitStructure);

Enable interrupt

/* Enable the TIM1 global Interrupt*/
NVIC_InitStructure.NVIC_IRQChannel =TIM1_CC_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority =1;
NVIC_InitStructure.NVIC_IRQChannelCmd =ENABLE;
NVIC_Init(&NVIC_InitStructure);
 
NVIC_InitStructure.NVIC_IRQChannel =TIM1_UP_TIM10_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority= 2;
NVIC_Init(&NVIC_InitStructure);

/* Enablethe CC2 Interrupt Request */
TIM_ITConfig(TIM1,TIM_IT_CC2|TIM_IT_Update, ENABLE);

Enable timer 1

/* TIM enable counter */
TIM_Cmd(TIM1, ENABLE);

Download code verification test

 

Hardware and software open source address:

https://github.com/strongercjd/STM32F207VCT6

 

Click to view the album where this article is located, STM32F207 tutorial

 

Pay attention to the official account, and receive article updates as soon as possible . The comment area cannot be seen in time, you can go to the official account to communicate if you need to communicate

Guess you like

Origin blog.csdn.net/Firefly_cjd/article/details/108375440