STM32 realizes arbitrary angle phase-shifted full-bridge PWM

Publisher:WanderlustSoulLatest 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

Recently, for some reasons, a PWM waveform with arbitrary phase shift is needed to drive a full-bridge circuit. This article records the implementation process.

Similar to a full-bridge rectifier, replacing the four diodes with switching devices creates a full-bridge inverter. The two bridge arms need to be driven by four, two sets of complementary PWM waveforms. However, in this requirement, the two sets of complementary PWM waveforms are not exactly the same, but have a certain phase difference, and this phase difference may be an arbitrary value.

The generation of complementary PWM waves is relatively simple. The previous article talked about the method of using a general timer. This article uses the TIMx_CHy and TIMx_CHyN channels of an advanced timer or a general timer with complementary output function. The two timers generate two sets of complementary PWM waves, and the duty cycle and frequency can be adjusted.

(See the previous article: How to use the STM32 general-purpose timer to output two complementary PWMs with adjustable duty cycle and frequency  )

This article focuses on how to produce phase shift at any angle.

So I started reading the official manual of STM32F334, looking for the functions of timer synchronization and master-slave mode!

Slave mode: Gated mode

The counter can be enabled depending on the level of a selected input.

This means that a timer can be enabled or disabled using an input signal.

An example is given in the official manual:

Using one timer to enable another timer
In this example, we control the enable of TIM2 with the output compare 1 of Timer 3. Refer
to Figure 205 for connections. TIM2 counts on the divided internal clock only when OC1REF
of TIM3 is high. Both counter clock frequencies are divided by 3 by the prescaler compared
to CK_INT (fCK_CNT = fCK_INT/3).
1. Configure TIM3 master mode to send its Output Compare 1 Reference (OC1REF)
signal as trigger output (MMS=100 in the TIM3_CR2 register).
2. Configure the TIM3 OC1REF waveform (TIM3_CCMR1 register).
3. Configure TIM2 to get the input trigger from TIM3 (TS=000 in the TIM2_SMCR
register).
4. Configure TIM2 in gated mode (SMS=101 in TIM2_SMCR register).
5. Enable TIM2 by writing ‘1 in the CEN bit (TIM2_CR1 register).
6. Start TIM3 by writing ‘1 in the CEN bit (TIM3_CR1 register).
Note: The counter 2 clock is not synchronized with counter 1, this mode only affects the TIM2
counter enable signal.

In the STM32F334C8T6 chip, TIM2 and TIM3 do not have complementary PWM output function. The advanced timer TIM1 and general timer TIM15, 16, 17 have complementary output function.

To connect two timers using the master-slave mode, there needs to be output and input signals between the two.



From these three tables, we can see that only TIM1 and TIM15 can be used, and TIM15 is the master and TIM1 is the slave.

Use CH1 and CH1N of TIM1 and TIM15 to output a set of complementary PWM respectively, and use TIM15_CH2 to output the trigger signal OC2REF


After determining the timer to be used, the next step is to write the program. The program probably has the following parts:


  1. Calculation parameters //See previous article

  2. Configuring Output Pins

  3. Basic clock configuration //See the previous article

  4. Output mode and parameter configuration //See previous article

  5. Complementary Output Configuration

  6. Master-slave mode and trigger signal configuration

  7. Enable clock

  8. Modify the trigger signal //It is very important, otherwise the output will be wrong

The steps to configure the master-slave mode are as follows:

1. Configure the Master Timers using the following functions:
 void TIM_SelectOutputTrigger(TIM_TypeDef* TIMx, uint16_t
TIM_TRGOSource);
 void TIM_SelectMasterSlaveMode(TIM_TypeDef* TIMx, uint16_t
TIM_MasterSlaveMode);
2. Configure the Slave Timers using the following functions:
 void TIM_SelectInputTrigger(TIM_TypeDef* TIMx, uint16_t
TIM_InputTriggerSource);
 void TIM_SelectSlaveMode(TIM_TypeDef* TIMx, uint16_t TIM_SlaveMode);

Part of the code:


  1. //#include "***"  

  2. //function name  

  3. {  

  4. uint16_t pulse=0 ,Period=0,shiftphase=0,deadtime=0;  

  5. uint16_t freq = 100000; //Required frequency, set to 100k  

  6.   

  7. //Calculate each parameter and assign it to the corresponding variable. Do not use dead zone, so deadtime=0  

  8.     Period = 72*1000000 / freq; //Total number of counts in one PWM cycle when the timer clock is 72MHz  

  9.     pulse= Period * 45 /100; // Pulse width count times, = total times * duty cycle, duty cycle 45?, adjustable  

  10.     shiftphase = Period *90/360; //Set the phase shift to 90 degrees  

  11.     shiftphase += 340*72/1000; //Phase compensation, here the compensation is 0.34us, see Note 1  

  12.   

  13. //Configure GPIO pins and corresponding multiplexing functions  

  14.   

  15. //Basic clock configuration, see the previous article  

  16.     TIM_TimeBaseInit(PWM12_TIM, & TIM_TimeBaseInitStruct);  

  17.     TIM_TimeBaseInit(PWM34_TIM, & TIM_TimeBaseInitStruct);  

  18. //For output mode and parameter configuration, see the previous article  

  19.     TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;  

  20.     TIM_OCInitStruct.TIM_Pulse = pulse ; //Pulse width  

  21.     TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;  

  22.     //..........Write other parameters  

  23.     //PWM1,2  

  24.     TIM_OC1Init(TIM15, & TIM_OCInitStruct);  

  25.     TIM_OC1PreloadConfig(TIM15, TIM_OCPreload_Enable);  

  26.   

  27.     //PWM3,4  

  28.     TIM_OC1Init(TIM1, & TIM_OCInitStruct);  

  29.     TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  

  30.   

  31.     //Trigger signal  

  32.     TIM_OCInitStruct.TIM_Pulse =shiftphase; //The pulse width of the trigger signal, that is, the amount of phase shift  

  33.     TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;  

  34.     TIM_OC2Init(TIM15, &TIM_OCInitStruct);  

  35.     TIM_OC2PreloadConfig(TIM15, TIM_OCPreload_Enable);  

  36.   

  37.     //Configure master-slave mode  

  38.         //Master slave mode  

  39.     TIM_SelectOutputTrigger(TIM15, TIM_TRGOSource_OC2Ref);  

  40.     TIM_SelectMasterSlaveMode(TIM15, TIM_MasterSlaveMode_Enable);  

  41.     TIM_SelectInputTrigger(TIM1, TIM_TS_ITR0);  

  42.     TIM_SelectSlaveMode(TIM1, TIM_SlaveMode_Gated);  

  43.   

  44.     // Enable timer and PWM output  

  45.     //TIM_Cmd();  

  46.     //TIM_CtrlPWMOutputs();  

  47.   

  48.     // Make the trigger signal always valid, a very important step, see Note 2  

  49.     while(TIM_GetFlagStatus(TIM15, TIM_FLAG_Trigger) == RESET);  

  50.     TIM_OCInitStruct.TIM_Pulse =Period;  

  51.     TIM_OC2Init(PWM12_TIM, &TIM_OCInitStruct);  

  52.   

  53.     //Finish  

  54. }  


Note 1: Phase shift compensation. Since it takes time to turn on the clock and execute the code, the phase shift will be inaccurate, so the phase shift value needs to be corrected, that is, compensated. The value 0.34us given in the code is the compensation value obtained by measurement at a frequency of 100K. This value has a high accuracy in the range of 0~300 degrees and can reach +-0.003us (or +-0.02 degrees). The compensation value is affected by many factors, such as frequency, code execution efficiency (high assembly efficiency), chip core energy, etc.

The minimum phase shift is 1/720

Note 2: Since TIM1 works in the gatmode of slave mode, the counter will work only when the trigger signal is high. Therefore, once TIM1 is triggered, the trigger signal clock must be in a high state to ensure the normal operation of TIM1, otherwise the output frequency and duty cycle will change! (According to my understanding, TIM_Pulse=0 should be set here in the case of PWM_MODE1 and TIM_OCPolarity_Low;, but in fact it is correct to set it to TIM_Pulse=Period. I haven't figured out why yet)

Complete code:

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

Effect diagram: Phase shift 90 degrees


Keywords:STM32 Reference address:STM32 realizes arbitrary angle phase-shifted full-bridge PWM

Previous article:STM32 asymmetric PWM mode realizes dynamic phase shift
Next article:STM32 general-purpose timer realizes output of two complementary PWMs with adjustable duty cycle and frequency

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号