STM32 general-purpose timer (TIM2-5) PWM output

Publisher:李国永Latest update time:2015-09-23 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Pulse Width Modulation (PWM), short for "Pulse Width Modulation", is a very effective technology that uses the digital output of a microprocessor to control analog circuits. To put it simply, it is the control of pulse width. It is generally used to control the speed of a stepper motor, etc. Except for TIM6 and TIM7, all other timers of STM32 can be used to generate PWM outputs. Among them, the advanced timers TIM1 and TIM8 can generate 7-way PWM outputs at the same time, and the general timer can also generate 4-way PWM outputs at the same time.

 

PWM Output Mode

The STM32 PWM output has two modes, Mode 1 and Mode 2, which are determined by the OCxM bit in the TIMx_CCMRx register ("110" is Mode 1, "111" is Mode 2). The differences between Mode 1 and Mode 2 are as follows:

110: PWM mode 1 - When counting up, once TIMx_CNTTIMx_CCR1, channel 1 is at an invalid level (OC1REF=0), otherwise it is at a valid level (OC1REF=1).

111: PWM mode 2 - When counting up, once TIMx_CNT < TIMx_CCR1, channel 1 is at an invalid level, otherwise it is at a valid level; when counting down, once TIMx_CNT > TIMx_CCR1, channel 1 is at a valid level, otherwise it is at an invalid level.

From this point of view, Mode 1 and Mode 2 complement each other and are opposite to each other, so there is not much difference in their application.

From the counting mode point of view, PWM also has up counting mode, down counting mode and center alignment mode just like TIMx when it is used as a timer. For specific information about the three modes, please refer to the "14.3.9 PWM Mode" section of the "STM32 Reference Manual". I will not go into details here.

 

 PWM output pin

The output pins of PWM are determined. For specific pin functions, please refer to the "8.3.7 Timer Multiplexing Function Remapping" section of the "STM32 Reference Manual". It should be emphasized here that different TIMx have different pins assigned, but considering the pin multiplexing function, STM32 proposes a concept of remapping, that is, by setting some related registers, PWM can be output on other non-originally designated pins. However, these remapped pins are also given by the reference manual. For example, the second channel of TIM3, when there is no remapping, the designated pin is PA.7. If partial remapping is set, the output of TIM3_CH2 is mapped to PB.5. If full remapping is set, the output of TIM3_CH2 is mapped to PC.7.

 

 PWM output signal

PWM outputs a square wave signal, the frequency of which is determined by the TIMx clock frequency and the TIMx_ARR prescaler. The specific setting method is explained in detail in the previous study notes. The duty cycle of the output signal is determined by the TIMx_CRRx register. The formula is "duty cycle = (TIMx_CRRx/TIMx_ARR)*100%", so you can output a square wave signal with the frequency and duty cycle you need by filling in the appropriate number in CRR.

 

Follow these steps to configure:

1.  Set the RCC clock;

2.  Set the GPIO clock;

3.  Set the relevant registers of TIMx timer;

4.  Set the PWM related registers of the TIMx timer;

 

    Step 1 Setting the RCC clock has been given in detail in the previous article, so I will not repeat it here. It should be noted that the general timer TIMx is clocked by APB1, while the GPIO is clocked by APB2. Note that if you need to reimage the PWM output, you also need to enable the pin multiplexing clock AFIO.

   When setting the GPIO clock in step 2, the GPIO mode should be set to multiplexed push-pull output GPIO_Mode_AF_PP. If pin remapping is required, it needs to be set using the GPIO_PinRemapConfig() function.

   In step 3, when setting the relevant registers of the TIMx timer, set the relevant TIMx clock and technology mode, etc., just like the previous study notes.

   Step 4 sets the PWM related registers. First, set the PWM mode (PWM is frozen by default), then set the duty cycle (calculated according to the formula described above), and then set the output comparison polarity: when set to High, the output signal is not inverted, and when set to Low, the output signal is inverted before output. The most important thing is to enable the output state of TIMx and enable the PWM output of TIMx. After the relevant settings are completed, you can use TIM_Cmd() to turn on the TIMx timer to get PWM output.

 

Here is the reference program I wrote:

 void GPIO_PA_Init()
{//PA13 pin configuration
 GPIO_InitTypeDef GPIO_InitStructure;
 GPIO_DeInit(GPIOA);
 GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed
 =GPIO_Speed_50MHz;
 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP ;//Multiplexed push-pull output
 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);//Enable port clock A
 GPIO_Init(GPIOA, &GPIO_InitStructure);
}

 

void TIMER3_Init()
{
 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
 TIM_DeInit(TIM3);
 TIM_InternalClockConfig(TIM3);
 TIM_TimeBaseStructure.TIM_Period=10000-1;//ARR value period 10K
 TIM_TimeBaseStructure.TIM_Prescaler=0;
 TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; //Up counting mode
 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
}

 

void TIMER3_PWM_Init()
 
 TIM_OCInitTypeDef TIMOCInitStructure;
 TIMOCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //PWM mode 1 output
 TIMOCInitStructure.TIM_Pulse = 0; //Duty cycle = (CCRx/ARR)*100%
 TIMOCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //TIM output comparison polarity high
 TIMOCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //Enable output state
 TIM_OC2Init(TIM3, &TIMOCInitStructure); //TIM3 CH2 output
 TIM_CtrlPWMOutputs(TIM3,ENABLE); //Set TIM3 PWM output to enable

 TIM_Cmd(TIM3, ENABLE); //Turn on the clock
}

Keywords:STM32 Reference address:STM32 general-purpose timer (TIM2-5) PWM output

Previous article:STM32 PWM input mode
Next article:10. ARM9(2440) IIC-theoretical knowledge and program examples

Recommended ReadingLatest update time:2024-11-16 08:52

Iron Man creates robotic arm design based on STM32 and ESP32
Zhihui has always been very interested in this field, and he thinks that the most practical one is the robot arm. Coincidentally, half a year ago, Zhihui accidentally found a second-hand robot arm, which made him a little excited. However, the joy did not last long, and Zhihui was a little unhappy: after he spent so
[robot]
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号