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
Previous article:How to set STM32 PB3 PB4 to normal GPIO configuration
Next article:Stepper motor acceleration and deceleration program, suitable for GPIO settings
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
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
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- ASML predicts that its revenue in 2030 will exceed 457 billion yuan! Gross profit margin 56-60%
- Detailed explanation of intelligent car body perception system
- How to solve the problem that the servo drive is not enabled
- Why does the servo drive not power on?
- What point should I connect to when the servo is turned on?
- How to turn on the internal enable of Panasonic servo drive?
- What is the rigidity setting of Panasonic servo drive?
- How to change the inertia ratio of Panasonic servo drive
- STM32F103+STM32CUBE MX+SIP+DMA mode, how to correctly judge the completion of sending and receiving in the sending and receiving interruption?
- Can the simulated 3D component diagram, AD19, be drawn without drawing it in SolidWorks and then importing it?
- Application of MSP430F415 in LCD air conditioner infrared remote control
- EBAZ4205 ore plate resource analysis
- [Reading Month] Read a good book on RT-Thread technology and write down your reading notes
- General Ideas for Ethernet Protection Circuit Design
- What has changed in the controversial power battery war?
- The voltage regulation value of the Zener diode
- Operational amplifier circuit working principle
- AC high voltage linear scheme to control two groups of lamps