STM32 study notes - Introduction and general-purpose PWM timer configuration

Pulse width modulation (the PWM), is the English "Pulse Width Modulation" acronym, or pulse width modulation, the microprocessor uses the digital output of the analog circuit to control a very effective technique. Simply, it is to control the pulse width.
In addition to the STM32 timer TIM6 and 7. Other timers may be used to generate the PWM output. One senior timers TIM1 and TIM8 can produce up to seven PWM outputs simultaneously. And the general purpose timer can generate up to four simultaneously PWM output, so that, the STM32 can generate up to 30 PWM outputs at the same time! Here we use only generates a PWM output of CH2 TIM3.

Here Insert Picture Description

Here Insert Picture Description
Here Insert Picture Description
Configuration
1), and a multiplexing clock opening TIM3 outputs the multiplexed clock function, configured to PB5.
First open TIM3 clock and configured to multiplex the output PB5, since TIM3_CH2 channel to remap the PB5, at this time, the output multiplexing function belonging PB5. Library Function TIM3 clock is enabled:

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //使能定时器 3 时钟

AFIO library function sets the clock is:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); //复用时钟使能

Finally set to alternate function output PB5
listed GPIO initialization line of code:
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; // push-pull output multiplexing

2) Set to the remapping TIM3_CH2 PB5.
Because TIM3_CH2 default is connected to the PA7, so we need to set TIM3_REMAP as part remapping (via AFIO_MAPR configuration), let TIM3_CH2 remapped to PB5 above. Remapping function set in the function library function which is:

void GPIO_PinRemapConfig(uint32_t GPIO_Remap, FunctionalState NewState)

STM32 remapping can only be re-mapped to a specific port. The first parameter can be understood as a set inlet type remapping, such TIM3 inlet portion remapping parameters GPIO_PartialRemap_TIM3. Therefore TIM3 library function implementation section remapping is:

GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); 

3) Initialization TIM3, provided TIM3 ARR and the PSC.
After opening the TIM3 clock, we need to set the value of the two PSC and ARR registers to control the output of the PWM
period. When the PWM cycle is too slow (less than 50Hz), we will obviously feel a flicker. Thus, here too the PWM cycle should not be provided. This is achieved by TIM_TimeBaseInit function library function [Reference] format of the call is:

TIM_TimeBaseStructure.TIM_Period = arr; //设置自动重装载值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据指定的参数初始化 TIMx 的 

4) Set TIM3_CH2 PWM mode, so that the output can TIM3 of CH2.
In a library function, the PWM channel setting function is set via the TIM_OC1Init () ~ TIM_OC4Init (), a function of setting different channels is not the same, we use here is the channel 2, the function uses TIM_OC2Init ().

void TIM_OC2Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)

TIM_OCInitTypeDef definition:

typedef struct
{
 uint16_t TIM_OCMode;
uint16_t TIM_OutputState; 
 uint16_t TIM_OutputNState; 
 uint16_t TIM_Pulse; 
 uint16_t TIM_OCPolarity; 
 uint16_t TIM_OCNPolarity; 
 uint16_t TIM_OCIdleState; 
 uint16_t TIM_OCNIdleState; 
} TIM_OCInitTypeDef;

Here we explain the relevant member variables and we ask:
parameter setting mode is TIM_OCMode or PWM output compare, here we are PWM mode.
TIM_OutputState parameter used to set the comparator output is enabled, i.e. to enable the PWM output port.
TIM_OCPolarity parameter used to set the polarity is high or low.
Other parameters TIM_OutputNState, TIM_OCNPolarity, TIM_OCIdleState and TIM_OCNIdleState senior timers TIM1 and TIM8 was used.
To achieve the scene we mentioned above, it is:

TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择 PWM 模式 2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性高
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //初始化 TIM3 OC2

5) Enable TIM3.
After completion of the above set up, we need to enable TIM3. The previous method enabled TIM3 has been explained:

TIM_Cmd(TIM3, ENABLE); //使能 TIM3

6) modifying TIM3_CCR2 duty ratio is controlled.
Finally, after the above setting, the PWM output is already started, but its duty cycle and frequency are fixed
, and we can be controlled by modifying the output duty CH2 TIM3_CCR2. In the library functions, modify TIM3_CCR2 duty cycle function is:

void TIM_SetCompare2(TIM_TypeDef* TIMx, uint16_t Compare2)

Of course, for the other channels, respectively, a function name, the function format TIM_SetComparex (x = 1,2,3,4).

Through the above six steps, we can control the CH2 TIM3 the PWM output.

//TIM3 PWM 部分初始化
//PWM 输出初始化
//arr:自动重装值
//psc:时钟预分频数
void TIM3_PWM_Init(u16 arr,u16 psc)
{ 
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //①使能定时器 3 时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE); //①使能 GPIO 和 AFIO 复用功能时钟

GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //②重映射 TIM3_CH2->PB5 

//设置该引脚为复用输出功能,输出 TIM3 CH2 的 PWM 脉冲波形 GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure); //①初始化 GPIO

//初始化 TIM3
TIM_TimeBaseStructure.TIM_Period = arr; //设置在自动重装载周期值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM 向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //③初始化 TIMx

//初始化 TIM3 Channel2 PWM 模式
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择 PWM 模式 2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性高
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //④初始化外设 TIM3 OC2
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能预装载寄存器

TIM_Cmd(TIM3, ENABLE); //⑤使能 TIM3
}
Published 28 original articles · won praise 25 · views 20000 +

Guess you like

Origin blog.csdn.net/Summertrainxy/article/details/105317449