STM32 timer --- master-slave mode triggers PWM phase shift

Publisher:zhaodawei617Latest update time:2018-06-28 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

In this article, the trigger mode is used instead of the gate mode.


code show as below:


void TIM3_PWMShiftInit(void)  

{  

    TIM_TimeBaseInitTypeDef  TIM_TimeBaseInitStruct;  

    GPIO_InitTypeDef  GPIO_InitStruct;  

    TIM_OCInitTypeDef TIM_OCInitStruct;  

 

    /**********************TIM3 GPIO configuration****************************/  

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA, ENABLE);  

      

    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;  

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_13;  

    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;  

  

    GPIO_Init(GPIOB,&GPIO_InitStruct);  

    GPIO_InitStruct.GPIO_Pin = GPIO_Pin_8; 

    GPIO_Init(GPIOA,&GPIO_InitStruct);  

      

    /**********************Initialize TimBase structure****************************/  

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE); //Turn on TIM clock

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);

      

    TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; //Timer does not divide frequency

    TIM_TimeBaseInitStruct.TIM_CounterMode   = TIM_CounterMode_Up;  

    TIM_TimeBaseInitStruct.TIM_Period = 1799; //Frequency = 72000000/PSC/(ARR+1) = 40KHz 

    TIM_TimeBaseInitStruct.TIM_Prescaler = 0;       

      

    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);  

    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseInitStruct);  

    /**********************Initialize TIM3 OC structure****************************/  

    TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2;  

    TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_Low;  

    TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;  

    TIM_OCInitStruct.TIM_Pulse = 600; //TIM3 CH3 duty cycle: 600/(ARR+1)=33.3%

    TIM_OC3Init(TIM3,&TIM_OCInitStruct);  

    //TIM1 CH1pwm output configuration         

    TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2;  

    TIM_OCInitStruct.TIM_Pulse=600; //TIM1 CH1 duty cycle: 600/(ARR+1)=33.3%

    TIM_OCInitStruct.TIM_OCPolarity=TIM_OCPolarity_Low; //Set output polarity              

    TIM_OCInitStruct.TIM_OutputNState=TIM_OutputNState_Disable; //Disable complementary terminal output. Output polarity cannot be adjusted without this statement.                         

    TIM_OC1Init(TIM1,&TIM_OCInitStruct);            

  

    /**************************Configure phase shift trigger**************************/

    TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM2; //PWM2 and Toggle modes can trigger phase shift, other modes cannot

    TIM_OCInitStruct.TIM_Pulse = 600-2; //It takes about 2 timer cycles to trigger, phase shift: 360*600/(ARR+2)= 120 degrees

    TIM_OC2Init(TIM1,&TIM_OCInitStruct); //Need CH2 rising edge trigger. If the phase shift cannot be triggered, please change the output polarity of CH2.

  

    /**************************Configure master-slave mode****************************/

    TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_OC2Ref); //TIM1 OC2 triggers slave timer

    TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable); 

 

    TIM_SelectInputTrigger(TIM3, TIM_TS_ITR0); //ITRO trigger

    TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger);   

    TIM_Cmd(TIM1, ENABLE);

    TIM_CtrlPWMOutputs(TIM1, ENABLE); //Advanced timer pwm output enable, be sure to turn it on


The oscilloscope displays the following waveform:

CH1: TIM1 CH1

CH2: TIM3 CH3





Figure 1 shows that the frequency and duty cycle are correct, and combined with Figure 2, it can be seen that the phase shift degree is correct.


After observing the output of the two timers at different frequencies and duty cycles, it was found that the phase shift had an error of about 25ns, so two timer cycle corrections were performed during initialization.


The oscilloscope is zoomed in to 25ns, and it can be seen that after correction by 600-2,2 timer cycles (the timer does not divide the frequency), the phase shift has basically no offset.


Keywords:STM32 Reference address:STM32 timer --- master-slave mode triggers PWM phase shift

Previous article:STM32 advanced timer slave mode four
Next article:STM32 timer generates PWM--breathing light

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号