Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement input capture function in STM32

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to achieve STM32 input capture function, I hope you have something to gain after reading this article, let's discuss it together!

01. STM32 capture function

From the timer block diagram of STM32

① part of the basic timer module, has been explained in the "STM32 basic timer detailed explanation".

The ② section captures / compares the channel module, which has been explained in "timer's PWM function".

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

From the figure above, you can see that timer 1 has four input capture channels.

Below, take the capture channel 2 of timer 1 as an example.

First of all, determine the corresponding GPIO. From the Alternatefunction mapping in the STM32F207 data manual, we can see that the GPIO corresponding to timer 1, channel 2 has PA9 and PE11, and PE11 will be used below.

02. Input capture process

The input TIx corresponding to the input phase sampling is used to generate the filtered signal TIxF. The polarity selection edge detector then generates a signal (TIxFPx) that can be used as a trigger input to the service mode controller or as a capture command. The frequency is pre-divided before capturing the register.

The output phase produces an intermediate waveform that is used for reference: OCxRef (high level effective). The end of the chain determines the extreme zero. Capture / compare channel 1 main circuit.

The input capture filter is mainly set at the ①, and the input TIx corresponding to the input phase is sampled to generate the filtered signal TIxF. In general, there is no filtering in the application scenario. Set the ICF of TIMx_CCMR1 [3:0] = 0000. As long as the rising edge is collected, the capture will be triggered.

At ②, the polarity of input capture is mainly set, and the CC1P or CC1NP bit of TIMx_CCER is set. If the channel is configured as output, 0 means high-level effective, 1 means low-level effective; if configured to input mode, the bit chooses IC1 or IC1 inverter signal as trigger or capture signal. 0 means no inversion, and 1 means inversion.

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

The 0o:CC1 channel is configured to output

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

10: the CC1 channel is configured as input, and IC1 is mapped on TI2

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

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

TIMx_CCMR1 's ICPS [1:0] these two bits define the pre-division coefficient of the CC1 input (IC1). Once CC1E='O (in the TIMx_CCER register), the prescaler resets.

00: no prescaler, each edge detected on the capture input triggers a capture

01: capture is triggered every 2 events

10: capture is triggered every 4 events

11: every 8 events trigger a capture.

CC1E bit of TIMx_CCER

The CC1 channel is configured to output:

0: turn off an OC1 to disable output.

1: open an OC1 signal output to the corresponding output pin.

The CC1 channel is configured to enter:

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

0: capture prohibition

0: capture enable.

Finally, we need to set up an interrupt to make the system respond quickly 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 up GPIO reuse

/ * 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 function

/ * Time base configuration * / TIM_TimeBaseStructure.TIM_Period = 0XFFFF _ TIM_CKD_DIV1;TIM_TimeBaseStructure.TIM_CounterMode _ 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

/ * Enablethe TIM1 global Interrupt*/NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= NVICS InitStructure. NVICS IRQChannelSubpriority = 1 * NVICS IRQChannelSubpriority = 1. NVICS IRQChannelCmd = ENABLE;NVIC_Init (& NVIC_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = TIM1_UP_TIM10_IRQn;NVIC_InitStructure.NVIC_IRQChannelSubPriority= 2; NVICS 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 the code verification test

After reading this article, I believe you have a certain understanding of "how to achieve input capture function in STM32". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report