STM32 general-purpose timer realizes output of two complementary PWMs with adjustable duty cycle and frequency

Publisher:TranquilSilenceLatest update time:2017-09-30 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

MCU:STM32F334C8T6

PWM, or pulse width modulation, can be used to drive motors, full-bridge circuits, etc. Those who have used STM32 know that its timer can easily achieve PWM output, and the advanced timer's TIMx_CHy and TIMx_CHyN can easily achieve the output of complementary PWM waveforms.

Advanced timer resources are limited. This article uses general-purpose timers to implement complementary PWM output, which is a good method when advanced timer resources are insufficient.

The STM32 timer PWM has two modes: PWM mode 1 and PWM mode 2

working principle:

PWM mode 1 - In upcounting, channel 1 is active as long as TIMx_CNT

In downcounting, channel 1 is inactive (OC1REF='0') as long as TIMx_CNT>TIMx_CCR1 else active (OC1REF='1').

 PWM mode 2 - In upcounting, channel 1 is inactive as long as TIMx_CNT

In downcounting, channel 1 is active as long as TIMx_CNT>TIMx_CCR1 else inactive.

The official manual describes channel 1. Other channels are similar. Consider the upward counting mode.

method one:

Assume that the high level is the effective level, that is, the high level is active, use timer 3

PWM mode 1: TIM3_CNTTIM3_CCR1 outputs low

PWM mode 2: TIM3_CNTTIM3_CCR1 outputs high

It can be seen that no matter mode1 or mode2, the level flip is when the value in the counter TIM3_CNT reaches the value in TIM3_CCR1 (the number of times can control the duty cycle, see below)

Based on this, the two channels of TIM (such as TIM3_CH1 and TIM3_CH2) can be configured as mode1 and mode2 respectively, so that two complementary PWMs can be output. This is method 1.

Method Two:

In method 1, it is assumed that the high level is the active state. In fact, the active state can also be a low level. In this case, consider the same mode (mode1)

acive high: TIM3_CNTTIM3_CCR1 output low

Active low: TIM3_CNTTIM3_CCR1 outputs high

Therefore, in the same mode, by configuring the effective levels of the two channels to high and low respectively, complementary PWM output can also be achieved. This is method 2.

The principle is similar in down counting mode and will not be explained here.

Frequency and duty cycle adjustment:

Two registers are mentioned above: CNT and CCR1 (channel x corresponds to CRx)

CNT is the current count value of the timer, and CCR1 is the value used for comparison. When CNT reaches the value of CCR1, a level transition will occur.

Another register ARR, the auto-load register, stores the auto-load value. When CNT reaches the value of ARR during up counting, it will be reset and start again from 0. When CNT reaches 0 during down counting, the value in ARR will be automatically loaded into CNT and start decrementing again. That is to say, the value in ARR is the counting cycle (the center's counting mode is not considered here)

Assuming that the frequency we need is freq, the duty cycle is dutycycle, and the timer uses the system frequency SYSCLK, there is the following relationship:

ARR = SYSCLK/freq, dutycycle=CCR1/ARR

It can be seen that the frequency can be adjusted by changing ARR, and the duty cycle can be adjusted by changing CCR1.

Part of the code:


  1. uint16_t period=0,pulsewidth=0;  

  2.       

  3. GPIO_InitTypeDef GPIO_InitStruct;  

  4. TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;  

  5. TIM_OCInitTypeDef TIM_OCInitStruct;  

  6.   

  7. period = 72*1000000/(100*1000); //Counting period, system frequency 72M, PWM output frequency 100k  

  8. pulsewidth = 45*period/100; //Pulse width, duty cycle 45%  

  9.   

  10. // Enable the peripheral clock  

  11. //Configure GPIO  

  12.   

  13. TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;  

  14. TIM_TimeBaseInitStruct.TIM_Period = period - 1;//ARR  


  1. //Fill in other parameters of TIM_TimeBaseInitStruct  


  1. TIM_TimeBaseInit(TIM3, & TIM_TimeBaseInitStruct);  


  1. //OCInit structure initialization, fill in all parameters  

  2. TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;  

  3. TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;  

  4. TIM_OCInitStruct.TIM_Pulse = pulsewidth; //CCR1  

  5. TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;  

  6. //TIM_OC1Init() turns on channel 1  

  7.   

  8. //OC2 Method 1: Modify Mode  

  9. TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2;  

  10. TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;  

  11. /**********************************  

  12. //OC2 Method 2: Modify Polarity  

  13. TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;  

  14. TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;  

  15. *************************************/  

  16. //TIM_OC2Init() turns on channel 2  

  17.   

  18. //Finally turn on the clock  

  19. TIM_Cmd(TIM3, ENABLE);  


The attached picture shows the results of personal testing. The two methods have the same effect.

The test is based on STM32F334C8T6, frequency 100k, duty cycle 45%, complementary wave duty cycle 55%

Download the complete code:

http://download.csdn.net/detail/wind4study/8559157


Keywords:STM32 Reference address:STM32 general-purpose timer realizes output of two complementary PWMs with adjustable duty cycle and frequency

Previous article:STM32 realizes arbitrary angle phase-shifted full-bridge PWM
Next article:STM32F334 uses the serial port to send and receive data

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号