Except for TIM6 and TIM7, all other timers of STM32 can be used to generate PWM signals. Advanced timers TIM1 and TIM8 can generate 7 PWM outputs at the same time, and general timers can generate 4 PWM outputs at the same time.
The steps for setting the STM32 timer PWM output are as follows:
1) Turn on the STM32 clock and configure the output I/O as multiplexed output
2) Set ARR (period) and PSC (prescaler)
3) Set PWM mode (edge-aligned or center-aligned)
4) Enable the channel output of the timer and enable the timer
5) Modify CCR2 to control the duty cycle
Example: 72MHz main frequency, PB7 (TIM4_CH2) output PWM
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; // for PWM
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
void TIM4_Configuration(void) // for PWM
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
TIM4->ARR = 900; // Set counter auto reload value 72MHz/900 = 8kHz
TIM4->PSC = 0; // Prescaler does not divide
TIM4->CCMR1 |= 7<<12; // PWM2
TIM4->CCMR1 |= 1<<11; // CH2 preload enable
TIM4->CCER |= 1<<4; // OC2 output enable
TIM4->CR1 |= 0x8000; // ARPE enable
TIM4->CR1 |= 0x01; // Enable timer
TIM4->CCR2 = 550; // Adjust duty cycle
}
Because each version of the IAR firmware library is different, this routine directly controls the registers for easy understanding.
Add a routine based on the firmware library (TIM3 is used as an example, IO settings are omitted):
void TIM3_Configuration(void)
{
// Output two PWM
/*----------------------------------------------------------
RCC_APB2PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM3->ARR = 900; // Maximum 900
TIM3->PSC = 0;
TIM3->CCMR1 |= 7<<12; // PWM2 center alignment
TIM3->CCMR1 |= 1<<11; // CH2 pre-load enable
TIM3->CCER |= 1<<4; // OC2 output enable
TIM3->CCMR1 |= 7<<4; // PWM2 center alignment
TIM3->CCMR1 |= 1<<3; // CH1 pre-load enable
TIM3->CCER |= 1; // OC1 output enable
TIM3->CR1 |= 0x8000; // ARPE enable
TIM3->CR1 |= 0x01; // Enable timer
TIM3->CCR1 = 100; // Adjust duty cycle TIM3_CH1
TIM3->CCR2 = 300; // Adjust duty cycle TIM3_CH2
-----------------------------------------------------------*/
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
TIM_TimeBaseStructure.TIM_Period = 999;
TIM_TimeBaseStructure.TIM_Prescaler = 0; TIM_TimeBaseStructure.TIM_ClockDivision
= 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 500;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OC Polarity_High;
TIM_OC1Init(TIM3, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel2 */
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 300;
TIM_OC2Init (TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM3, ENABLE);
TIM_Cmd(TIM3, ENABLE);
}
Previous article:Several notes on STM32
Next article:Setting the pull-up resistor when the GPIO of S3C2440 is used as input
- 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
- 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
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- SHT31 Review - In-depth Review
- Four major reasons for power module heating
- 【Home smart dashboard】esp32 s2 LED, button driver
- Does the backlight 14-leds in LCD refer to the number of LEDs in the backlight?
- [GD32L233C-START Review] III. Comparison of various image refresh drive methods for 1.5-inch monochrome 16-level grayscale OLED
- 【McQueen Trial】+ Unboxing and Assembly
- This must be a liar.
- How to find MSP430 program examples on TI's official website
- Industrial grade 485 serial port server
- C2000 Software Serial Interface (SCI) Implementation Method