(v) Reproduced: Universal Timer PWM output

Source: Internet
Author: User

1. TIMER output PWM basic Concept

Pulse width modulation (PWM), an abbreviation for the English "pulse width modulation", is called pulse width modulation, which is a very effective technique for controlling analog circuits using the digital output of a microprocessor. The simple point is the control of the pulse width. Generally used to control the speed of stepper motor and so on.

STM32 timers, in addition to the TIM6 and TIM7, can be used to generate PWM outputs, where the advanced timer TIM1 and TIM8 can simultaneously generate a 7-way PWM output, while the universal timer can simultaneously generate a 4-way PWM output.

1.1 PWM output mode

The PWM output of the STM32 has two modes, mode 1 and Mode 2, determined by the OCXM bit in the TIMX_CCMRX register ("110" is mode 1, "111" is mode 2). The differences between pattern 1 and pattern 2 are as follows:

110:PWM Mode 1-When counting up, once the TIMX_CNT<TIMX_CCR1 Channel 1 is a valid level, otherwise invalid level, when the down count, once the TIMX_CNT>TIMX_CCR1 Channel 1 is invalid level (oc1ref=0), Otherwise, the active level (oc1ref=1).

111:PWM Mode 2-when counting up, once the TIMX_CNT<TIMX_CCR1 Channel 1 is invalid level, otherwise the active level, when the down count, once the TIMX_CNT>TIMX_CCR1 Channel 1 is the active level, otherwise the invalid level.

As a result, mode 1 and pattern 2 are complementary and opposite, so the difference in use is not too large.

And from the Count mode, PWM also and timx in as timer, also has up counting mode, down counting mode and center alignment mode, about 3 modes of specific information, you can view the STM32 reference manual "14.3.9 PWM Mode" section, here is not detailed.

1.2 PWM output pin

The PWM output pin is OK, and the specific pin function can be viewed in the section "8.3.7 Timer multiplexing function remapping" of the STM32 reference manual. It should be emphasized here that different timx have different pins assigned, but given the function of pin multiplexing, STM32 proposes a re-image concept, which means that PWM can also be output on other non-original specified pins by setting some relevant registers. But these re-image pins are also given by reference manuals. For example, the 2nd channel of TIM3, in the absence of a re-image, the specified pin is pa.7, if the partial re-image is set, the output of TIM3_CH2 is mapped to pb.5, if a full re-image is set, the TIM3_CH2 output is mapped to pc.7.

1.3 PWM output Signal

PWM output is a square wave signal, the frequency of the signal is determined by the TIMX clock frequency and the Timx_arr Prescaler, the specific setting method in the previous study note has a detailed account. The duty-free ratio of the output signal is determined by the TIMX_CRRX register. The formula is "duty-ratio = (Timx_crrx/timx_arr) *100%", so you can output the square wave signal of your desired frequency and duty ratio by filling in the appropriate number in the CRR.

2. TIMER output PWM Implementation steps

1. Set the RCC clock;

2. Set the Gpio clock;

3. Set the relevant register of the TIMX timer;

4. Set the PWM correlation register for the TIMX timer.

The 1th step of setting up the RCC clock already gives the detailed code in the previous article, which is not to say much more. It is important to note that the universal Timer TIMX is provided by the APB1 clock, while the GPIO is provided by the APB2 clock. Note that if you need to re-image the output of the PWM, you also need to turn on the pin multiplexing clock afio.

In the 2nd step, when setting the Gpio clock, the GPIO mode should be set to re-use push-pull output gpio_mode_af_pp, if the pin is required to re-image, you need to use the Gpio_pinremapconfig () function to set.

The 3rd step sets the relevant register of the TIMX timer, as well as the previous study note, setting up the relevant TIMX clock and technical mode, etc. Refer to the "Timer Basic timer function" study notes.

The 4th step is to set the PWM correlation register, first to set the PWM mode (PWM is frozen by default), and then set the duty ratio (calculated according to the formula described above), and then set the output comparison polarity: When set to high, the output signal is not reversed, when set to low, the output signal is reversed and then output. The most important thing is to enable the output state of the TIMX and enable the TIMX PWM output to enable.

Once the setting is complete, the TIMX timer can be switched on by Tim_cmd () to get the PWM output.

3. TIMER output PWM Source code

Since I am now in the hands of the struggle Development Board is to be pb.5 to the LED, so need to use the TIM3 CH2 channel, and to make a pin re-image. After opening the TIM3, the PWM output allows the led to illuminate, and the LED brightness can be adjusted by changing the duty ratio in the pwm_cfg ().

#include "Stm32f10x_lib.h"

void Rcc_cfg ();

void Gpio_cfg ();

void Timer_cfg ();

void Pwm_cfg ();

Duty ratio with a value range of 0-100

int dutyfactor = 50;

int main ()

{

int Temp;

Rcc_cfg ();

Gpio_cfg ();

Timer_cfg ();

Pwm_cfg ();

Enable TIM3 timer, start output PWM

Tim_cmd (TIM3, ENABLE);

while (1);

}

void Rcc_cfg ()

{

Defining Error state variables

ErrorStatus Hsestartupstatus;

Reset the RCC register to the default value

Rcc_deinit ();

Open external high-speed clock oscillator

Rcc_hseconfig (rcc_hse_on);

Waiting for external high-speed clock crystal operation

Hsestartupstatus = Rcc_waitforhsestartup ();

if (Hsestartupstatus = = SUCCESS)

{

Set the AHB clock (HCLK) to the system clock

Rcc_hclkconfig (RCC_SYSCLK_DIV1);

Set the high-speed AHB clock (APB2) to HCLK clock

Rcc_pclk2config (RCC_HCLK_DIV1);

Set the low-speed AHB clock (APB1) to 2 crossover of HCLK

Rcc_pclk1config (RCC_HCLK_DIV2);

Set Flash code delay

Flash_setlatency (flash_latency_2);

Enable prefetch refers to cache

Flash_prefetchbuffercmd (flash_prefetchbuffer_enable);

Set the PLL clock to 9 times Octave 8MHz * 9 = 72MHz for HSE

Rcc_pllconfig (Rcc_pllsource_hse_div1, rcc_pllmul_9);

Enable PLL

Rcc_pllcmd (ENABLE);

Wait for the PLL to be ready

while (Rcc_getflagstatus (rcc_flag_pllrdy) = = RESET);

Setting the PLL to the system clock source

Rcc_sysclkconfig (RCC_SYSCLKSOURCE_PLLCLK);

Determine if the PLL is a system clock

while (Rcc_getsysclksource ()! = 0x08);

}

Turn on the TIM3 clock

Rcc_apb1periphclockcmd (rcc_apb1periph_tim3,enable);

Turn on the GPIOB clock and multiplexing function

Rcc_apb2periphclockcmd (Rcc_apb2periph_gpiob | rcc_apb2periph_afio,enable);

}

void Gpio_cfg ()

{

Gpio_inittypedef gpio_initstructure;

Partial mapping, mapping TIM3_CH2 to PB5

Gpio_pinremapconfig (GPIO_FULLREMAP_TIM3, ENABLE);

Gpio_pinremapconfig (GPIO_PARTIALREMAP_TIM3, ENABLE);

Select Pin 5

Gpio_initstructure.gpio_pin = Gpio_pin_5;

Output frequency Max 50MHz

Gpio_initstructure.gpio_speed = Gpio_speed_50mhz;

multiplexed push-Pull output

Gpio_initstructure.gpio_mode = gpio_mode_af_pp;

Gpio_init (gpiob,&gpio_initstructure);

}

void Timer_cfg ()

{

Tim_timebaseinittypedef tim_timebasestructure;

To reset the timer to the default value

Tim_deinit (TIM3);

Clock source supplied to TIM3 with internal clock

Tim_internalclockconfig (TIM3);

The Prescaler factor is 0, that is, no prescaler, at which time the timer frequency is 72MHz

Tim_timebasestructure.tim_prescaler = 0;

Set up Clock segmentation

Tim_timebasestructure.tim_clockdivision = TIM_CKD_DIV1;

Set counter mode to up counting mode

Tim_timebasestructure.tim_countermode = tim_countermode_up;

Set the count overflow size, which generates an update event for each 7,200 number, which is the PWM output frequency of 10kHz

Tim_timebasestructure.tim_period = 7200-1;

To apply a configuration to TIM3

Tim_timebaseinit (tim3,&tim_timebasestructure);

}

void Pwm_cfg ()

{

Tim_ocinittypedef timocinitstructure;

Set default values

Tim_ocstructinit (&timocinitstructure);

PWM Mode 1 output

Timocinitstructure.tim_ocmode = TIM_OCMODE_PWM1;

Set the duty ratio, duty ratio = (Ccrx/arr) *100% or (tim_pulse/tim_period) *100%

Timocinitstructure.tim_pulse = Dutyfactor * 7200/100;

High polarity of Tim output

timocinitstructure.tim_ocpolarity = Tim_ocpolarity_high;

Enable to output state

Timocinitstructure.tim_outputstate = tim_outputstate_enable;

CH2 output of TIM3

Tim_oc2init (TIM3, &timocinitstructure);

Set the PWM output of the TIM3 to enable

Tim_ctrlpwmoutputs (tim3,enable);

}

(v) Reproduced: Universal Timer PWM output

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.