2-way complementary PWM of stm32 timer 1

Publisher:心灵律动Latest update time:2018-06-29 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

void TIM1_PWM_Ini(u16 arr,u16 psc)

{

    GPIO_InitTypeDef GPIO_InitStructure;

    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;

    TIM_OCInitTypeDef  TIM_OCInitStructure;

    TIM_BDTRInitTypeDef TIM_BDTRInitStructure;

//Start the clock

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE); //Enable timer 3 clock

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE); //Enable GPIO and function clock

//RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE); //Pin remapping requires turning on this clock

// Initialize GPIO

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9; //PWM output at PA8,9,10

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Multiplex push-pull output

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOA,&GPIO_InitStructure);                               //初始化GPIO

 

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14; //PWM output at PA7

    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Multiplex push-pull output

    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

    GPIO_Init(GPIOB,&GPIO_InitStructure); //Initialize GPIO

 

//Initialize timer TIM1

    TIM_TimeBaseStructure.TIM_Period = arr; //Set the automatic reload period value

    TIM_TimeBaseStructure.TIM_Prescaler =psc; //Set the prescaler value

    TIM_TimeBaseStructure.TIM_ClockDivision = 0; //Set clock division: TDTS = Tck_tim

    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM up counting mode

// TIM_TimeBaseStructure.TIM_RepetitionCounter=0; //Repetition register, used to automatically update the pwm duty cycle

    TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);                     

 

//pwm output configuration, TIM_OCInitStructure has seven configurations

    TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2; //Set to pwm2 output mode

      

TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;  

    //TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low; //Set the output polarity //The effective level of the output polarity and complementary polarity is low

    TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable; //Enable the channel output //Comparison output enable

//The following parameters (except pulse) are only used by advanced timers, and do not need to be configured for general timers

  //  TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCPolarity_Low;  

TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCPolarity_High; //Set the complementary end output polarity //The effective level of the output polarity and complementary polarity is low

    TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable; //Enable complementary output

    TIM_OCInitStructure.TIM_OCIdleState=TIM_OCIdleState_Reset; //Output status after dead zone

    TIM_OCInitStructure.TIM_OCNIdleState=TIM_OCIdleState_Reset; //Complementary output status after dead zone

//Set the pulse value of channel 1 capture compare register - duty cycle is 50%

    TIM_OCInitStructure.TIM_Pulse=CCR1_Val;

    TIM_OC1Init(TIM1,&TIM_OCInitStructure); //Set channel 1

//Set the pulse value of channel 2 capture compare register - duty cycle is 25%

    TIM_OCInitStructure.TIM_Pulse=CCR2_Val;

    TIM_OC2Init(TIM1,&TIM_OCInitStructure);

//Set the pulse value of channel 3 capture compare register - duty cycle is 12.5%

    TIM_OCInitStructure.TIM_Pulse=CCR3_Val;

    TIM_OC3Init(TIM1,&TIM_OCInitStructure);

//Dead zone and brake function configuration, only available for advanced timers, general timers do not need to configure TIM_BDTRInitStructure, a total of seven configurations

        TIM_BDTRInitStructure.TIM_OSSRState=TIM_OSSRState_Disable; //Output selection in running mode //Enable "off state" in running mode

        TIM_BDTRInitStructure.TIM_OSSIState=TIM_OSSIState_Disable; //Output selection in idle mode //Enable "off state" in off mode

        TIM_BDTRInitStructure.TIM_LOCKLevel=TIM_LOCKLevel_OFF; //Lock setting //Lock off

        TIM_BDTRInitStructure.TIM_DeadTime=0x90; //Dead time setting

        TIM_BDTRInitStructure.TIM_Break=TIM_Break_Disable; //Enable the brake function

        TIM_BDTRInitStructure.TIM_BreakPolarity=TIM_BreakPolarity_High; //Brake input polarity//Brake input high level is valid    

        TIM_BDTRInitStructure.TIM_AutomaticOutput=TIM_AutomaticOutput_Enable; //Automatic output enable

     

        TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);        

//Open the enable terminal

        TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable); //Enable TIMx preload register on CCR1?

        TIM_ARRPreloadConfig(TIM1,ENABLE); //Enable TIMx preload register on ARR?

        TIM_Cmd(TIM1,ENABLE); //Turn on TIM2?

//The following sentence is only for advanced timers, output pwm must be turned on?

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

}


Keywords:stm32 Reference address:2-way complementary PWM of stm32 timer 1

Previous article:STM32F103 outputs multiple dead zone complementary PWM waves
Next article:STM32 complementary PWM output enable control

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
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号