As for what PWM is, just search on Baidu. In fact, the content is simple. First of all, the PWM function in STM32 is implemented using a counter, which is similar to 51, but there are also differences. 51 uses the interrupt of the counter to adjust the duty cycle, which takes up the time of the MCU. However, STM32 uses hardware to implement the PWM function, so PWM is a peripheral function. (The counter's up-counting, down-counting, and center-aligned modes are not introduced).
PWM mainly configures 2 registers. TIMx_ARR = T (set period T, frequency setting)
TIMx_CCRx = t (set duty cycle, this is a library function)
**P.S.: PWM still enters the interrupt operation of the connected TIMx. However, the 51 interrupt is interrupted as a small part and then counts to perform pwm modulation. However, STM32 enters when it reaches ti and has already been modulated.
#include "PWM.h"
void PWM_init()
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
//Hook up the clock RCC
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
/* Timer configuration */
TIM_TimeBaseStructure.TIM_Period = 900 ; //Set timer frequency
TIM_TimeBaseStructure.TIM_Prescaler = 0; //No frequency division
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //Count up
TIM_TimeBaseInit(TIM3, & TIM_TimeBaseStructure);
TIM_Cmd(TIM3, ENABLE);
/* PWM configuration*/
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
TIM_OC2Init(TIM3, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE); //Specify the mapping of the pin, this configured pwm is mapped to TIM3
/* Configure pwm to adjust the brightness of the led, so configure the GPIO of the led*/
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //Push-pull multiplexing function, pwm controls the led, if it is not an MCU, GPIO_Mode_Out_PP is not used
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
Functions used in interrupt files
void EXTI2_IRQHandler(void) //Here the external interrupt is used as the ti (duty cycle) of modulated pwm
{
if(EXTI_GetITStatus(EXTI_Line2)==SET) //Judge whether to enter external interrupt 2
{
EXTI_ClearITPendingBit(EXTI_Line2); // Clear the suspension of line EXIT
delay_ms(10); //
if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)==Bit_RESET) //Read whether key_up is pressed
{
if(ti<500)
ti=ti+100;
else
ti=0; //Press the button to increase the duty cycle
}
while(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_2)==0); //Yes, here is the release judgment
}
}
void TIM3_IRQHandler(void) //When ti is reached, enter the interrupt and give the led a high level with a certain duty cycle
{
static u8 i=0;
TIM_ClearITPendingBit(TIM3,TIM_IT_Update); //Clear the interrupt source
GPIO_Write(GPIOC,(u16)~(0x01<if(i==8) i=0;
}
main.c The main function is simple
#include "public.h"
#include "Systick.h"
#include "PWM.h"
#include "ExInter.h"
int main()
{
PWM_init();
ExInter_init();
while(1)
{
delay_ms(10);
TIM_SetCompare2(TIM3,ti); //Connect channel 2,
}
}
To be honest, it is a good habit to write down your understanding in comments.
Previous article:stm32 simulates output of multiple PWM channels through IO port
Next article:stm32f103 timer1 generates 400Hz PWM
Recommended ReadingLatest update time:2024-11-16 16:00
- 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
- RFID card reader help
- The application channels for multiple development boards have been reopened. Come and apply if you are interested!
- Learn about RTOS in ten minutes!
- Power communication - carrier communication
- PowerPC
- High salary recruitment of 5G filter design engineers located in Suzhou Industrial Park
- Bluetooth Low Energy Issues
- TI's TPS92610-Q1 is a single-channel LED driver
- Good video material sharing on UWB in detail
- Please tell me how to use low power resistors when connecting LEDs in series and parallel