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);
Previous article:Use of STM32 Advanced Control Timer 1
Next article:STM32 TIM1 output pulse example
- 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
- Huawei's Strategic Department Director Gai Gang: The cumulative installed base of open source Euler operating system exceeds 10 million sets
- Download from the Internet--ARM Getting Started Notes
- Learn ARM development(22)
- Learn ARM development(21)
- Learn ARM development(20)
- Learn ARM development(19)
- Learn ARM development(14)
- Learn ARM development(15)
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- At 10 am today, Keysight Technologies will broadcast a live broadcast on [Applications and Techniques of Oscilloscopes in the Electronics Industry]
- EEWORLD University ---- Live playback: Start your next-generation 4K display application design with TI DLP? technology
- On the design of second-order active low-pass filter
- Ahhhh, the last digit of the digital tube cannot be controlled!
- Has anyone done grating data collection?
- Verilog Learning Tips
- Share: How to design a suitable voltage acquisition circuit! -Daoheshun Big Data Infinigo
- How to find MSP430 program examples on TI's official website
- Disassembly of two different types of radiometers
- What is the relationship between modbus and 485?