Summary of stm32 PWM Learning

Source: Internet
Author: User

Because the chip used to participate in the computer mouse is the stm32 series, coupled with relatively tight time, we basically learn while doing the module. I encountered a lot of difficulties, but I still felt that I had learned a lot and wanted to share it with me .. Okay, let's just talk about it.

Pulse Width Modulation (PWM), short for "Pulse Width Modulation, it is a very effective technology that uses the digital output of a microprocessor to control analog circuits. Simply put, it is to control the pulse width.
In addition to tim6 and 7, the stm32 Timer. Other timers can be used to generate PWM output. Among them, the advanced timer tim1 and tim8 can generate up to 7 PWM outputs at the same time. The general timer can also generate up to four PWM outputs at the same time, so that stm32 can generate up to 30 PWM outputs at the same time! Here we only use CH2 of tim3 to generate one PWM output.
To make the common timer timx of stm32 generate PWM output, we will use three registers to control PWM. These three registers are: capture/compare mode register (timx_ccmr1/2), capture/compare enable register (timx_ccer), capture/compare register (timx_ccr1 ~ 4 ). (Note that there is also an arr register for timx to control the output frequency of PWM)

The first is the capture/compare mode register (timx_ccmr1/2). There are two registers in total, timx _ ccmr1 and timx _ ccmr2. Timx_ccmr1 controls limit and 2, while timx_ccmr2 controls CH3 and 4.

The second is the capture/compare enable register (timx_ccer), which controls the switches of each input/output channel.

The last is the capture/compare register (timx_ccr1 ~ 4), this register has a total of 4, corresponding to 4 transmission channels ~ 4. The four registers are similar. To put it simply, this register is used to set the duty cycle of PWM.

(For details about the bit of each register, see the STM chip manual .)

As mentioned above, the timx_arr register is used to set the PWM frequency. How does it work?

 

 

A good news for our novice cainiao is that stm32 provides us with a set of library functions, which is much easier than directly operating registers. Below is our PWM configuration function

Void pwm_configuration (void)
{
Gpio_inittypedef gpio_initstructure;

Tim_timebaseinittypedef tim_timebasestructure;

/* Gpiob clock enable */
// RCC clock enabling for gpiob
Rcc_apb2periphclockcmd (rcc_apb2periph_gpiob | rcc_apb2periph_afio, enable );
// Configure the gpio used
Gpio_initstructure.gpio_pin = gpio_pin_8 | gpio_pin_9;
Gpio_initstructure.gpio_mode = gpio_mode_af_pp; // gpio_mode_out_pp ;//;
Gpio_initstructure.gpio_speed = gpio_speed_50mhz;
Gpio_init (gpiob, & gpio_initstructure );
Rcc_apb1periphclockcmd (rcc_apb1periph_tim4, enable );
/* Time base configuration */

Tim_timebasestructure.tim_period = 100-1; // 1000; // set the value of 80 K for the automatic reload register cycle in the next update event loading activity

(. _ Period is the value of timx_arr, that is, the PWM frequency)
Tim_timebasestructure.tim_prescaler = 0; // The pre-division value used as the divisor of the timx clock frequency.
Tim_timebasestructure.tim_clockdivision = 0; // set the clock Division: tdts = tck_tim
Tim_timebasestructure.tim_countermode = tim_countermode_up; // the mode in which the TIM count up
Tim_timebaseinit (tim4, & tim_timebasestructure); // initialize the time base unit of timx Based on the parameter specified in tim_timebaseinitstruct
/* Output compare active mode configuration: channel1 */
Tim_ocinitstructure.tim_ocmode = tim_ocmode_pwm2; // select the timer mode: Tim pulse width modulation mode 2
Tim_ocinitstructure.tim_outputstate = tim_outputstate_enable; // compare the output
Tim_ocinitstructure.tim_pulse = 0; // set the pulse value of the Compare register to be loaded and set the duty cycle
Tim_ocinitstructure.tim_ocpolarity = tim_ocpolarity_high; // output polarity: The Tim output is relatively polar.
Tim_oc3init (tim4, & tim_ocinitstructure); // initialize the peripheral timx according to the parameter specified in tim_ocinitstruct.
Tim_oc3preloadconfig (tim4, tim_ocpreload_enable); // enables timx to pre-load registers on CCR3

Tim_arrpreloadconfig (tim4, enable); // enable the pre-loading register of timx on arr
/* Tim4 enable counter */
Tim_cmd (tim4, enable); // enable timx peripherals
}

For details about how to use each function, see the use of the stm32 Firmware Library.

So how does the value of the timx_arr register determine the PWM frequency? The size of tim_period (that is, the value of the timx_arr register) actually indicates that
An update or interruption occurs only after tim_period count. Next, you need to set the pre-frequency of the clock.
Number of tim_prescaler, here is a formula, we illustrate: for example, the clock frequency = 72 MHz/(hour
Minute pre-division + 1 ). (Assume that 72 MHz is the system operating frequency, and the clock frequency here is the frequency of the generated PWM clock) It indicates that the currently set tim_prescaler directly determines the timer clock frequency.
In layman's terms, it is how many times a second can be counted. For example, the calculated clock frequency is 2000, that is
If tim_period is set to 2000, it will be interrupted once after 4000 counts. Since the clock frequency is counted 2000 times per second, as long as 2 seconds
Disconnected once.
Another thing to note is that we generally adopt the upward counting mode.

 

Okay. If you are tired, write it here !!!

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.