The principle and use of STM32 PWM

Publisher:BlissfulJoyLatest update time:2019-01-15 Source: eefocusKeywords:stm32  pwm Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

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);


Keywords:stm32  pwm Reference address:The principle and use of STM32 PWM

Previous article:STM32 Study Notes - External Interrupts
Next article:STM32 timer principle and use

Recommended ReadingLatest update time:2024-11-16 07:55

STMicroelectronics launches long-range wireless microcontroller to improve connectivity energy efficiency in smart metering, smart buildings and industrial monitoring
The new STM32 system chip has low power consumption and supports multiple wireless communication protocols, simplifying the design of wireless systems for various purposes. China, November 24, 2023 - STMicroelectronics (ST;), a world-leading semiconductor company serving multiple electronic applications, has releas
[Embedded]
STMicroelectronics launches long-range wireless microcontroller to improve connectivity energy efficiency in smart metering, smart buildings and industrial monitoring
[MCU framework][bsp layer][AT32F415][bsp_pwm] PWM configuration and use
9.1.3.6 PWM Input Mode This mode is a special case of input capture mode and operates the same as input capture mode except for the following differences: Both ICx signals are mapped to the same TIx input. These two ICx signals are edge-active but have opposite polarities. One of the TIxFP signals is used as a trigg
[Microcontroller]
[MCU framework][bsp layer][AT32F415][bsp_pwm] PWM configuration and use
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号