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
}
Previous article:STM32F103 outputs multiple dead zone complementary PWM waves
Next article:STM32 complementary PWM output enable control
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Factors to consider when selecting an electromagnetic flowmeter
- Migrating SP4 from MSP430F2xx and MSP430G2xx series to MSP430FR4xx and MSP430FR2xx series...
- [ESP32-S2-Kaluga-1 Review] 4. Add an interactive GUI?
- Baby growth "little partner"
- 【Practical】Eliminating EMI in Buck Converters (3)
- Looking for a DCDC chip
- Optocoupler device selection
- N32G430 lighting review
- The live broadcast entrance is now open! Award-winning live broadcast [TI C2000 signal chain real-time evaluation and usage tutorial]
- DMA serial port output