STM32 TIM1 PWM output

Publisher:csw520Latest update time:2017-11-02 Source: eefocusKeywords:STM32  TIM1  PWM  output Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

 GPIO_InitTypeDef GPIO_InitStructure2;         

 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;        

 TIM_OCInitTypeDef TIM_OCInitStructure;        

 TIM_BDTRInitTypeDef TIM_BDTRInitStructure;

 //Step one: Configure the clock                 

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_TIM1,ENABLE);

 //The second step is to configure the goio port          

    GPIO_InitStructure2.GPIO_Pin=GPIO_Pin_8;         

 GPIO_InitStructure2.GPIO_Speed=GPIO_Speed_50MHz;         

 GPIO_InitStructure2.GPIO_Mode=GPIO_Mode_AF_PP; //Set to multiplex floating output         

 GPIO_Init(GPIOA,&GPIO_InitStructure2);        

 

         

 GPIO_InitStructure2.GPIO_Pin=GPIO_Pin_13;         

 GPIO_InitStructure2.GPIO_Speed=GPIO_Speed_50MHz;         

 GPIO_InitStructure2.GPIO_Mode=GPIO_Mode_AF_PP; //Set to multiplex floating output         

 GPIO_Init(GPIOB,&GPIO_InitStructure2);

 //The third step, basic configuration of timer         

 TIM_TimeBaseStructure.TIM_Period=1000-1; // The value of the automatic reload register        

 TIM_TimeBaseStructure.TIM_Prescaler=72-1; // Clock prescaler number        

 TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; // Sampling frequency division        

 TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //Count up        

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

 TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);

 //The fourth step pwm output configuration         

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

 TIM_OCInitStructure.TIM_Pulse=500; //Set the duty cycle time         

 TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low; //Set the output polarity         

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

 

 //The following parameters are only used by advanced timers. General timers do not need to be configured.         

 TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_High; //Set the complementary terminal output polarity         

 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_OCNIdleState_Reset; //Complementary terminal output state after dead zone         

 TIM_OC1Init(TIM1,&TIM_OCInitStructure); //Initialize according to specified parameters           

 

 //The fifth step is to configure the dead zone and braking functions. This is only available for advanced timers. General timers do not need to be configured.         

 TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Disable;//Output in running mode

 TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Disable;//Output selection in idle mode          

 TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF; //Lock settings        

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

 TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable; //Brake function enabled         

 TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;//Brake input polarity        

 TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;//Automatic output enable         

 TIM_BDTRConfig(TIM1,&TIM_BDTRInitStructure);

 //The sixth step is to 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); //Open TIM2         

 

 //The following sentence is only available for advanced timers. The output pwm must be turned on.         

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

 // If you want to change the frequency and duty cycle of pwm during operation, call:

 TIM_SetAutoreload(TIM1,1000); 

 TIM_SetCompare1(TIM1,500);


Keywords:STM32  TIM1  PWM  output Reference address:STM32 TIM1 PWM output

Previous article:Use of STM32 Advanced Control Timer 1
Next article:STM32 TIM1 output pulse example

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号