Stm32f4_tim input waveform capture (pulse frequency, duty ratio)

Source: Internet
Author: User

Ⅰ, overview

Based on the previous article "Tim Input waveform capture (Pulse frequency)" on the basis of expansion, the previous article is mainly to capture the frequency of waveforms, this paper mainly expands the capture waveform of the duty ratio .

The author tests the method and the same as the previous article, through the signal generator to generate PWM signal , through the serial port transmission frequency and duty ratio to the upper computer (host computer serial assistant display its value). (No signal generator friends can be combined with the previous article PWM output to do the signal source; You can also use different timers on the same board to connect the PWM output pin to the capture input pin)

experimental phenomenon : different frequency of experimental phenomena please look at an article (the article provided by the author also carried out a large number of different frequency testing, the error in a few Hz normal range).

1000Hz , 20%-80% duty-free phenomenon:

1000Hz , 51%-58% duty-free phenomenon:

For more details on this article, please look down.

Ⅱ, Case Engineering download

The author for beginners to provide routines are removed a lot of unnecessary features, streamlining the official code, for beginners to understand, a simple and clear engineering for everyone to learn.

I provide examples of the project is on the board after many tests and no problem before uploading to 360 cloud disk, Welcome to download test, reference learning.

Software engineering that provides downloads is stm32f417, but F4 other models are also applicable (for F4 other models: attention, reply to "modified model").

Stm32f4_tim input waveform capture (pulse frequency, duty ratio) instance:

Https://yunpan.cn/cB2iknrjczWXe access Password

STM32F4 Information:

https://yunpan.cn/cR2pxqF5x2d9c Access Password 53e7

Ⅲ, principle description

The author will capture the PWM waveform simply by drawing a:

From the above we can see that we need to capture three values (two rising and middle falling edges). Two rise is used to calculate the period of a waveform (that is, it can calculate its frequency), because the captured " period difference " is the number of pulses, and through the configuration, we can know the time of each pulse, so that we know the entire " period difference " time. The " Duty Cycle Difference" and " period difference " are obtained, and the duty ratio of the waveform is obtained.

The author divides the Tim into three chunks: the time Base section, the comparison output, and the input capture, see the General Tim Block diagram below.

The previous article has described some of the basics of "time base", the "Time Base section" function is more useful, it can be used in addition to delay (timing), it can also be taken to trigger other functions, such as: Trigger da conversion, ad capture and so on.

The previous article is about the comparison output section of the figure, compare the output part of the function is relatively simple.

This article mainly describes the "input capture" section, this part of the input channel 1 and 2, channel 3 and 4 can cooperate with each other. This article only uses channel 3 of the TIM5 to capture the input signal frequency.

General Tim Block diagram:

The above two figure intercepts the "stm32f4x5, X7 reference manual" recommended download manual see.

Ⅳ, source code Analysis

The author uses the F4 standard peripheral library (also suggests that beginners use the official standard peripheral library) as the foundation of the project, mainly in the way of library to tell.

Source code in many places is the same as the previous article source code, only modified a few places (in fact, the main increase in the capture duty-related configuration): The input pin configuration, Tim capture configuration and its interrupt, interrupt function processing, the main function to send data.

These easy-to-forget configurations are the same for RCC, Nvic, and more.

1.TIM input capture pin configuration

The function is located under the Timer.c file;

The main increase is the road pin.

Note 2 points:

A. Pin-to- channel correspondence : This need to refer to the "Data Sheet", the article TIM5 Channel 3 corresponding to the PIN is PA2.

B. multiplexing Configuration : Gpio_pinafconfig (Gpioa, Gpio_pinsource2, GPIO_AF_TIM5);

This place for F1 to F0, F4 and other friends need attention, more chip series and library of different, there are some differences between them.

2. TIM input capture configuration

The function is located under the Timer.c file;

And the previous article instance ratio: added configuration for TIM5 Channel 4. At the same time, the crossover value is also changed to not divide.

The "Time-based section" is described in the previous two articles, which are mainly understood in the "Input Capture" section, with the main 5 parameters:

Capture channel: Is the channel we capture;

Capture polarity: Is the high or low level of our capture pulses;

Capture selection: Is the direct capture of the channel, or the above-mentioned channel assisted capture;

Capture crossover: The frequency of the captured pulses;

Capture Filter: Filter out the waveform;

3. Calculate capture waveform Frequency principle

The function is located under the stm32f4xx_it.c file;

The function is the interrupt function of the TIM capture waveform, the same (TIM5) interrupt entry, there are two channel channels, is also the computation frequency and duty ratio important function;

In conjunction with the description of the principle "" You can know that we need to capture 3 values:V0,V2 , and V1.

V0 and V2 are captured in Channel 3:

Capture_value0 = (uint32_t) (TIM5->CCR3);

Capture_value2 = (uint32_t) (TIM5->CCR3);

V1 in Channel 4 capture:
Capture_value1 = (uint32_t) (TIM5->CCR4);

One of the flag bits flag_capture is used to flag the state we capture, and we capture V0 will flag_capture= 1 is to tell the program we want to capture the values of V1 and V2.

Ⅴ, description

This article is written in conjunction with previous articles and can be viewed in the history message.

About the author provides the software engineering example, may pay attention, in the Session box reply "About the project", has about the engineering structure description, the model revision and so on.

The above summary is for reference only, if has the wrong place, please understanding.

, Last

More wonderful articles I'll share the first time in the public number.

Based on the principle of free sharing, it is convenient for us to learn knowledge and share technical knowledge on the platform regularly. If you feel that the content you share is useful to you, and you want to learn more about the relevant articles, please use the search "Embedddeveloper" or scan the QR code below, attention, there will be more exciting content waiting for you.

Stm32f4_tim input waveform capture (pulse frequency, duty ratio)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.