1.What is PWM?
It is pulse width modulation, or PWM for short. A very effective technique for controlling analog circuits using the digital output of a microprocessor is the control of pulse width.
The pulse mentioned here is the square wave we generate. A square wave is a continuous generation of N such cycles.
The duration of the high level in one cycle is the pulse width (pulse width), and PWM (pulse width modulation) is to control the duration of the high level in one cycle.
----------------------------------------------------------------------------------------------------------------------------------------
2. Schematic diagram of simple PWM principle
CNT: is the current value register, count register.
ARR: is the automatic reload register (initialization setting).
CCRx: Comparison value register (TIM_SetCompare1() sets and modifies the duty cycle).
Assume the timer operates in up-counting PWM mode:
When CNT
When the value of CNT is less than CCRx, IO outputs low level (0).
When the CNT value is greater than or equal to CCRx, IO outputs a high level (1).
When the value of CNT reaches ARR, it will reset to zero and then count up again, repeating the cycle.
Changing the value of CCRx can change the duty cycle of the PWM output. Changing the value of ARR can change the frequency of the PWM output. This is the output principle of PWM.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
3. Register workflow:
PWM Mode
The pulse width modulation mode can generate a signal whose frequency is determined by the TIMx_ARR register value and whose duty cycle is determined by the
Determined by the TIMx_CCRx register value.
By writing 110 (PWM mode 1) or 111 (PWM mode 2) to the OCxM bit in the TIMx_CCMRx register,
The PWM mode can be selected independently for each channel (one PWM per OCx output).
The OCxPE bit in the TIMx_CCMRx register is set to 1 to enable the corresponding preload register. Finally,
Setting the ARPE bit in the register enables auto-reload of the preload register (in up-counting or center-aligned mode).
Because the preload registers are transferred to the shadow registers only when an update event occurs, you must
All registers must be initialized by setting the UG bit in the TIMx_EGR register.
The OCx polarity can be programmed using the CCxP bit in the TIMx_CCER register. It can be set to either active high or
Set to active low. OCx output is enabled by setting the CCxE bit in the TIMx_CCER register to 1.
For more information, see the TIMx_CCERx register description.
In PWM mode (1 or 2), TIMx_CNT is always compared with TIMx_CCRx to determine whether
TIMx_CNT =< TIMx_CCRx。
Because the counter counts up, the timer can generate PWM in edge-aligned mode.
----------------------------------------------------------------------------------------------------------------------------------------------------------------
4. PWM edge-aligned mode
The following takes PWM mode 1 as an example. As long as TIMx_CNT < TIMx_CCRx, the PWM reference signal OCxREF is
If the comparison value in TIMx_CCRx is greater than the auto-reload value (in TIMx_ARR),
If the comparison value is 0, OCxRef remains at "0". Figure 183 shows an example of edge
Some PWM waveforms in edge-aligned mode (TIMx_ARR=8).
----------------------------------------------------------------------------------------------------------------------------------------------------------------
5. PWM steps - light brightness control:
Check out the schematic for the LED:
//①Find 4 pins according to the schematic diagram:
PF9 can use TIM14_CH1, which means that channel 1 of timer 14 can be used to generate PWM output.
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct; // Defined TIM attribute structure variable
GPIO_InitTypeDef GPIO_InitStruct; //Define GPIO type variable
TIM_OCInitTypeDef TIM_OCInitStruct; //Define variables for reuse functions
②// 1. Initialize clock: TIM14 and PF9
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE);
/* TIM3 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
// 2. Configure GPIO pins for multiplexing functions
/* GPIOC Configuration: TIM14 CH1 (PF9) */
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; // Select pin PF9
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; // Set to multiplexing function
GPIO_InitStruct.GPIO_Speed = GPIO_High_Speed; // Set the output speed to 100MHz
GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; // Set to push-pull output
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // Set to pull-up output
GPIO_Init(GPIOC, &GPIO_InitStruct); // Installation parameters
// 3. Connect the TIM and pin multiplexing functions: TIM14 and PF9
GPIO_PinAFConfig(GPIOF, GPIO_PinSource9, GPIO_AF_TIM14);
// 4. Configure the parameters of the TIM timer
TIM_TimeBaseInitStruct.TIM_Period = 100-1; // Set the reload value ARR (control frequency)
TIM_TimeBaseInitStruct.TIM_Prescaler = 8400-1; // Set the pre-scaling factor: period (times) 100Hz == 100us
TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; // Set the re-division value: TIM_CKD_DIV1 means no frequency division
TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; // Set the counting mode
TIM_TimeBaseInit(TIM14, &TIM_TimeBaseInitStruct);
// 5. Configure multiplexing function: PWM
TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1; // Configure as PWM mode 1
TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; // Enable output
//TIM_OCInitStruct.TIM_Pulse = CCR1_Val; // Initialize the configuration comparison value register
TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High; //Configure as high level valid
// 6.TIM14 channel 1 initialization
TIM_OC1Init(TIM14, &TIM_OCInitStruct); // TIM14 channel 1 initialization
// 7. Set the initial value of the automatic reload comparison value CCR1 to continuously generate PWM pulses
TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable);
// 8. Set the auto-reload value (ARR) to continuously generate PWM pulses
TIM_ARRPreloadConfig(TIM14, ENABLE);
/* 9. Enable timer 14 */
TIM_Cmd(TIM14, ENABLE);
// 10. Enable TIM1PWM output (advanced timer)
//TIM_CtrlPWMOutputs(TIM1, ENABLE)
Set the comparison value function
void TIM_SetCompareX(TIM_TypeDef* TIMx, uint16_t Comparex);
Previous article:STM32 Study Notes - External Interrupts
Next article:STM32 timer principle and use
Recommended ReadingLatest update time:2024-11-16 07:55
- Popular Resources
- Popular amplifiers
- 西门子S7-12001500 PLC SCL语言编程从入门到精通 (北岛李工)
- Small AC Servo Motor Control Circuit Design (by Masaru Ishijima; translated by Xue Liang and Zhu Jianjun, by Masaru Ishijima, Xue Liang, and Zhu Jianjun)
- Intelligent Control Technology of Permanent Magnet Synchronous Motor (Written by Wang Jun)
- usb_host_device_code
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- 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
- IMP34DT05 data sheet, package and other information
- About the triggering problem of monostable chip CD4098
- Byte/word alignment issues in DSP
- Pre-registration for the live broadcast with prizes: Cytech & ADI discuss with you Gigabit digital isolators for video, converters, and communications
- GPIO Operation of TMS320C6748
- Gigabit Ethernet Courseware.zip
- 1. “Wanli” Raspberry Pi car - Establishing a project warehouse
- ST Live: Introduction to MEMS sensor development kit, learn about sensors with embedded "finite state machine and machine learning core"
- EEWORLD University ---- Basics of Brushless DC Motors - Drive Control
- [Project source code] Basic logic design of OV5640 data stream reception and application based on FPGA