Objective
As I said earlier, STM32 's timer function is very powerful, today to summarize its other "powerful" function: Tim's comparison output function, Output adjustable PWM waveform. Directly calling the function interface "TIM2_CH1_PWM (uint32_t Freq, uint16_t dutycycle)" The incoming frequency and duty ratio can output the specified waveform.
The direct call I provide for software engineering is relatively simple to achieve the desired PWM waveform. However, if you are a learner, it is advisable to go into the function to get a clear picture of every detail, something that may be helpful to you in mastering Tim.
based on the principle of free sharing, if you feel that the content that you share is useful to you, approve the article that I share, please use the search " Embedddeveloper "or scan the QR code below, pay attention, and have more exciting content waiting for you.
Download
The article provides the "Software engineering" are on the hardware board to conduct multiple tests, and ensure that no problem before uploading to 360 cloud disk, please feel free to download the test, if there is a problem, please check your board if there is a problem.
St Standard Peripheral Library and reference manuals, data sheets, etc. can be downloaded from the St official website , you can also go to my 360 cloud disk download. There are several versions of the reference manual for the F0 series of chips (for F0 different chips), but there is a generic version, which is "stm32f0x128 reference manual V8 (English) 2015-07" recommended reference to this manual, later if you switch to a type of chip is also easy to understand.
Today's Software engineering (360 cloud disk):
https://yunpan.cn/cSitPxgQKsET5 access Password 5231
STM32F0XX information can be downloaded from my 360 cloud disk:
https://yunpan.cn/cS2PVuHn6X2Bj access password 8c37
Preparatory work
Recommended to prepare F0 Reference manuals and data sheets, easy to find relevant knowledge, no please go to ST official website or to my cloud disk download.
Today's summary of software engineering is based on the "TIM Basic Delay configuration Detailed process" modified, so you need to prepare the software engineering download. The source code for today's study is mainly added to the timer.c file.
Compare the output principle
Look at the following block diagram, the counter clock from the RCC, after the crossover to reach the counter, after comparison, to achieve the set value, resulting in an event corresponding to the pin rollover. The main thing is these three registers: the overloaded value, the count value, the comparison value. Speaking of which, if you do not understand, please think carefully.
Configuration process Details
①RCC Clock
The function is located under the BSP. c file;
I'm personally accustomed to the first step of configuring the clock, ST 's officially provided routines also put the configuration clock in front. The configuration of the RCC clock is important, several times I just forgot to configure the corresponding RCC clock, let me find a long time problem, finally found that the RCC The clock is not configured.
Attention:
Peripheral clocks should not be arbitrarily added, such as:RCC_APB1 Peripherals do not configure in the rcc_apb2 Clock " such as:Rcc_apb2periphclockcmd (RCC _AHBPERIPH_DMA1, ENABLE); This can be compiled, but the wrong "
I warn the RCC clock every time because many people are having trouble with the software because of the clock, so remind more people to be careful about configuring RCC.
② Output PIN Configuration
The function is located under the timer. c file;
Attention:
Multiplexing function must be configured, the above two, one is not configured, the corresponding PIN will not output PWM waveform.
③tim Comparison Output configuration
The function is located under the timer. c file;
Attention:
The decision output frequency and duty ratio are determined mainly by these three parameters (Tim2_prescaler_value, Tim2_period, Tim2_pulse).
With today's project output 1khz,20% to illustrate:
Tim2_prescaler_value = 72m/36m-1 = 1
Tim2_period = 36000-1
Tim2_pulse = 7200
These three how to get to see the code, today's project output waveform such as:
④ Focus
A. Careful study of the Code of people will find tim_ocmode_pwm1, there is a tim_ocmode_pwm2, what is the difference between the two? The answer is: the polarity of the output (i.e. the direction) is different.
Then someone asked: what is the relationship between the output polarity Tim_ocpolarity_high and the above? The answer is: the two parameters together with the output determine the polarity of the PWM waveform.
That is to say, different parameters can achieve the same effect, the configuration of parameters can also output the same waveform.
Interested friends can try to modify the effect of testing.
B. Today's project can set a periodic interrupt (that is, 1ms interrupts once), interested friends can try to write. Use the serial port printing test, try to lower the frequency, or the serial port response does not come over.
Description
Perhaps your hardware chip is not to provide the chip inside the project, but stm32f0 chip software compatibility is very good, can be applied to F0 many other models of chips, even F2, F4 and other chips (see the manual, or personally test).
The software engineering provided in this article is based on the St standard Peripheral Library, rather than using STM32CUBEMX to establish the project. Personally feel that using ST's standard Peripheral library suitable for learners, STM32CUBEMX building engineering structure complex, for learners, especially beginners are estimated to be headache.
Today's project is based on the project "stm32f0xx_tim Basic Delay configuration Detailed process" modified, the above examples are summarized for reference only, if there is wrong, please understand.
At last
If you like what I share, and you want to know more about it, please follow the public number at the beginning of the article, the new content continues to be updated, and more exciting content will appear later.
Read the original
Stm32f0xx_tim Output PWM Configuration detailed procedure