Pulse Width Modulation, or PWM for short, is a method of using digital signals to output analog signals. It has multiple modes. The main principle is to use the periodic changes in the high and low levels of digital signals to change the average value of the output voltage, thereby achieving the output mode of analog signals. It is mainly used in motor speed regulation and dimming lamp circuit control.
There are multiple PWM signals in STM32. Each timer output pin can be set to the output of the corresponding PWM signal. There are two main parameters of PWM signal, the duty cycle of the cycle. The cycle is mainly determined by the corresponding timer. The duty cycle is the time when the PWM outputs a high level. Assuming that the current timing cycle is 2MS, in 2MS, by setting 1MS pin to output a high level. In this way, it can be said that the duty cycle of this PWM is 50%.
During program execution, you can use TIM_SetCompare1() to change the corresponding duty cycle, so that you can flexibly control the output of the PWM signal.
#include "Time.h"
#include "stm32f10x.h"
void TIM4_init(void)
{
GPIO_InitTypeDef GPIO_InitStruc;
TIM_TimeBaseInitTypeDef TIM_struct;
TIM_OCInitTypeDef TIM_ocinit;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //Turn on the clock of TIM4
GPIO_InitStruc.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStruc.GPIO_Pin=GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8| GPIO_Pin_9;
GPIO_InitStruc.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStruc); //Initialize the output pin of the PWM signal
GPIO_InitStruc.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStruc.GPIO_Pin=GPIO_Pin_0 | GPIO_Pin_1;
GPIO_InitStruc.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruc);
TIM_struct.TIM_Period=999; //Set the timer period 72/72*1000 to 1MS
TIM_struct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_struct.TIM_ClockDivision=TIM_CKD_DIV1 ;
TIM_struct.TIM_Prescaler=71; //Timer 72 division
TIM_TimeBaseInit(TIM4,&TIM_struct);
//TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
TIM_ocinit.TIM_OCMode=TIM_OCMode_PWM1; //Set PWM mode to 1
TIM_ocinit.TIM_Pulse=200; //Set the initial PWM duty cycle
TIM_ocinit.TIM_OutputState=TIM_OutputState_Enable; //Enable pWM output enable
TIM_ocinit.TIM_OCPolarity=TIM_OCPolarity_High; //Set PWM signal polarity
TIM_OC1Init(TIM4,&TIM_ocinit);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC2Init(TIM4,&TIM_ocinit);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC3Init(TIM4,&TIM_ocinit);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_OC4Init(TIM4,&TIM_ocinit);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_Cmd(TIM4,ENABLE);
}
void TIM_PWM_Con(u16 DR1,u16 DR2,u16 DR3,u16 DR4)
{
TIM_SetCompare1(TIM4,DR1); //Change the duty cycle of PWM
TIM_SetCompare2(TIM4,DR2);
TIM_SetCompare3(TIM4,DR3);
TIM_SetCompare4(TIM4,DR4);
}
Previous article:STM32-Serial port IAP upgrade
Next article:STM32-SPI OLED
Recommended ReadingLatest update time:2024-11-16 13:23
- Popular Resources
- Popular amplifiers
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
- Summary of Common/Uncommon IOT Protocols
- There are many "tools" that are not fully understood.
- Some people started preparing for the national competition in junior high school! What would happen if we started learning electronics in junior high school?
- Principles and methods for selecting voltage zener diodes
- Types of MOS tubes in Wien bridge
- Automatic charging of power banks is annoying!
- Please tell me the decompression password for the downloaded data!
- Due to the company's production needs, we need to purchase STM32F437VIT6 chips
- msp430f149 microcontroller serial port C program
- Pulse meter based on 51 single chip monitors pulse rate and draws electrocardiogram