STM32TIM module generates PWM

Publisher:糖三角Latest update time:2016-10-08 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
 This is the PWM output mode of STM32. The TIM1 module of STM32 is an enhanced timer module, which is born for motor control. It can generate 3 groups of 6 PWMs. At the same time, each group of 2 PWMs is complementary and can have a dead zone, which can be used to drive the H-bridge.

  The following code is an example of using channels 1 and 2 of the TIM1 module to generate a total of 4 PWM channels. Similar code can also refer to the corresponding example in the ST firmware library.


C language:  TIM1 module generates PWM with dead zone
     
//Step1. Turn on TIM and corresponding port clock
//Start GPIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | \
                       RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD,\
                       ENABLE);
//Start AFIO
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
//Start TIM1
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
//Step2. Set GPIO accordingly for AF output //
Set PA.8/9 port as OC1 output port of TIM1
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//PB.13/14 port is set as TIM1_CH1N and TIM1_CH2N output port
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 3 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//Step3. TIM module initialization
void TIM_Configuration(void)
{
    TIM_TimeBaseInitTypeDef TIM_BaseInitStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
    TIM_BDTRInitTypeDef TIM_BDTRInitStructure;
    //TIM1 basic counter setup (set PWM frequency)
    //Frequency = TIM1_CLK/(ARR+1)
    TIM_BaseInitStructure.TIM_Period = 1000-1;
    TIM_BaseInitStructure.TIM_Prescaler = 72-1;
    TIM_BaseInitStructure.TIM_ClockDivision = 0;
    TIM_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_BaseInitStructure.TIM_RepetitionCounter = 0;
    TIM_TimeBaseInit(TIM1, &TIM_BaseInitStructure);
    //Enable ARR shadow register (until an update event occurs)
    TIM_ARRPreloadConfig(TIM1, ENABLE);
    //TIM1_OC1 module settings (set 1 channel duty cycle)
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
    TIM_OCInitStructure.TIM_OCPolarity
    = TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
    TIM_OCInitStructure.TIM_Pulse = 120;
    TIM_OC1Init(TIM1, &TIM_OCInitStructure);
    //Enable the shadow register of the CCR1 register (until an update event occurs, the setting will not be changed)
    TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);
    //TIM2_OC2 module settings (set 2 channel duty cycle)
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
    TIM_OCInitStructure.TIM_Pulse = 680;
    TIM_OC2Init(TIM1, &TIM_OCInitStructure);
    //Enable the shadow register of the CCR2 register (until an update event is generated)
    TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);
   
    //Dead zone settings
    TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
    TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
    TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
    TIM_BDTRInitStructure. TIM_DeadTime = 0x90; //Adjust the dead zone size here 0-0xff
    TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
    TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
    TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
    TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
   
    //TIM1 is turned on
    TIM_Cmd(TIM1, ENABLE);
    //TIM1_OC channel outputs PWM (must be added)
    TIM_CtrlPWMOutputs(TIM1, ENABLE);
}
  
  In fact, the PWM module can also have many tricks to play, such as in abnormal situations ( If there is a problem with the CPU clock), the output can be shut down urgently to avoid serious accidents such as circuit burnout
Keywords:STM32 Reference address:STM32TIM module generates PWM

Previous article:How to set STM32 PB3 PB4 to normal GPIO configuration
Next article:Stepper motor acceleration and deceleration program, suitable for GPIO settings

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号