Ⅰ, overview
Previous article about STM32 Basic counting Principle understand, the article is on its basis to expand, tell about the function of STM32 comparison output , with output PWM waveform As an example to tell.
Provides a more practical function in the case engineering: just call the function, the parameters are frequency and duty ratio
void Tim2_ch2_pwm (uint32_t Freq, uint16_t dutycycle);
Let's take a look at the example 1KHz, 20% duty-ratio waveform TIM2_CH2_PWM (1000, 20);
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 Output PWM waveform (adjustable frequency, duty ratio) Example:
HTTPS://YUNPAN.CN/CBYTWTGSMHBFE access Password
STM32F4 Information:
https://yunpan.cn/cR2pxqF5x2d9c Access Password 53e7
Ⅲ, principle description
The previous article is about the "count" in the red section, that is, the beginning to the end of the time, the article is about in the middle of the Count plus a value (comparison), when counted to this comparison value of the output pin to reverse, until Jina (a cycle). Cycle down.
General Tim Block diagram:
The above two figure intercepts the "stm32f4x5, X7 reference manual" recommended download manual see.
The "comparison value" I said above is the "Capture/compare x register" comparison register in the Tim Block diagram, which determines the level of the output pin.
Ⅳ, source code Analysis
The author of the F4 Standard Peripheral library (also recommended for beginners to use the official standard peripheral library) for the establishment of the project, mainly in the form of libraries to tell.
1.RCC Clock
The function is located under the Bsp.c file;
Key notes:
A. The configuration of the peripheral RCC clock is preceded by its peripheral initialization;
B. Match the corresponding clock.
For example: RCC_APB2 peripherals are not configured in the RCC_APB1 clock
"such as: Rcc_apb1periphclockcmd (Rcc_apb2periph_usart1, enable); This will compile and pass, but this is the wrong code"
2.TIM comparison output configuration
The function is located under the Timer.c file;
TIM2 The time Base unit configuration is related to the previous article, is a timely configuration, in this case, the configuration of a cycle .
TIM2 Channel 2:pwm1 The mode configuration is the configuration of the duty ratio .
Tim2_period = tim2_counter_clock/freq-1;
Tim2_pulse = (tim2_period + 1) *dutycycle/100;
The start of the function is a conversion formula for the period and duty cycle, which is also relatively simple.
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 = 168M/2/42m-1 = 1 (i.e. 2 divide)
Tim2_period = 42000-1
Tim2_pulse = 8400
Frequency 1KHz = 42m/4200
duty ratio 20% = 8400 / 42000
Ⅴ, description
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 Output PWM waveform (adjustable frequency, duty ratio)