STM32 input capture function

Time:2021-11-25

This article will introduce the timer input capture through STM32. If you are not familiar with the timer, you can see the previous article《Detailed explanation of STM32 basic timer》, the basic functions of the timer will not be explained in detail. The capture function of Timer1 will be explained below.

01. STM32 capture function

From the timer block diagram of stm32

① Some basic timer modules have been《Detailed explanation of STM32 basic timer》Explained.

② Partial capture / comparison channel module, already in《PWM function of timer》Explained.

③ The input capture module is the focus of this paper.

As can be seen from the above figure, timer 1 has four input capture channels.

The capture channel 2 of timer 1 is explained below as an example.

First, determine the corresponding GPIO. From the alternative function mapping in the stm32f207 data manual, the GPIO corresponding to channel 2 of timer 1 includes PA9 and PE11. PE11 will be used below.

02. Input capture process

The input stage samples the corresponding pair of input tix to generate the filtered signal tixf. The polarity selection edge detector then generates a signal (tixfpx), which can be used as a trigger input of the service mode controller or as a capture command. It is prescaled before the capture register.

The output phase generates an intermediate waveform, which is used for reference: ocxref (active at high level). The end of the chain determines the polarity 0. Capture / compare channel 1   Main circuit.

① The input capture filter is mainly set at the input stage to sample the corresponding pair of input tix to generate the filtered signal tixf. In general application scenarios, timx is set instead of filtering_ ICF of ccmr1 [3:0] = 0000. As long as the rising edge is collected, the acquisition is triggered.

② It mainly sets the input capture polarity and timx_ Cc1p or cc1np bit of CCER, if the channel is configured as output, 0 indicates that the high level is valid, and 1 indicates that the low level is valid; If the input mode is configured, this bit selects the inverse signal of IC1 or IC1 as the trigger or capture signal. A value of 0 indicates no inversion, and a value of 1 indicates inversion.

③ Set the input capture mapping channel at and set timx_ Cc1s [1:0] bit of ccmr1, which defines the direction of the channel (input / output) and the selection of input pin:

0o: CC1 channel is configured as output;

01: CC1 channel is configured as input, and IC1 is mapped on TL1;

10: CC1 channel is configured as input, and IC1 is mapped on Ti2;

11: CC1 channel is configured as input, and IC1 is mapped on TRC. This mode operates only when the internal trigger input is selected (selected by the TS bit of the timx_smcr register).

④ Set the input acquisition divider at, timx_ ICPs [1:0] bit and timx of ccmr1_ Cc1e bit of CCER.

TIMx_ The ICPs [1:0] bits of ccmr1 define the prescaler coefficient of CC1 input (IC1). Once cc1e =’O (in the timx_ccer register), the prescaler resets.

00: without prescaler, each edge detected on the acquisition input port triggers acquisition once;

01: capture is triggered every 2 events;

10: Capture is triggered every 4 events;

11: Capture is triggered every 8 events.

TIMx_ Cc1e bit of CCER

CC1 channel is configured as output:

0: turn off one OC1 disable output.

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

CC1 channel is configured as input:

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

0: capture prohibited;

0: capture enabled.

Finally, we also need to set the interrupt to enable the system to quickly respond to the input capture signal, mainly this timx_ Dier register, as follows:

Enable to allow update interrupts and channel 2 interrupts

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);

Setting timing basic function

/* 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 validation test

Hardware and software open source address:

https://github.com/strongercjd/STM32F207VCT6

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

Recommended Today

Vue、Three. JS implementation panorama

1、 First, we need to create a Vue project This paper mainly records the process of building panorama in detail, so building Vue project is not described too much. 2、 Install three js npm install three –save npm install three-trackballcontrols –save npm install three-orbit-controls –save npm i three-obj-mtl-loader –save npm i three-fbx-loader –save npm i […]