STM32 advanced timer output PWM wave

I'm just a noob, if there are any mistakes, please point them out.

Table of contents

1. Timer pin diagram

2.Overall framework

2.1 Time base module

2.2 Compare register

2.3 Dead zone generator

2.4 Output control

3. Structure

4. Programming

5.Description


1. Timer pin diagram

2.Overall framework

Note: In addition to the input capture and output comparison functions of the general timer, the advanced timer also adds programmable dead zone complementary output, repetition counter, and brake (circuit break) functions. This article only intercepts the output comparison. block diagram.

I divide the framework of the advanced timer into 4 parts: time base module, comparison register, dead zone generator, and output control.

2.1 Time base module

There are 4 advanced timer clock sources. Only the internal clock source is introduced here.

 The time base unit frame is as shown in the figure

Divide this into 4 parts: 1. Prescaler PSC, which can achieve frequency division of 1-65536. 2. Counter CNT: three counting modes, up counting mode, down counting mode and up/down (center aligned) counting mode. 3. Automatic reload register ARR and 4. Repeat counter RCR

What is different from the basic timer here are the three counting modes and the repetition counter.

In layman's terms, the loop starts from 0 (or the maximum value) and counts upwards (or downwards). When the maximum value (or 0) is reached, an interrupt or event can occur. When a repetition counter is used, a specific number of times is counted. The number will generate an interrupt 1 or event. Counting up or down depends on the counting mode.

This time to output the PWM wave, there is no need to use interrupts, only its counting function is enough.

2.2 Compare register

When the value of counter CNT is equal to the value of comparison register CCR, the polarity of the output reference signal OCxREF will change, where OCxREF=1 (high level) is called the effective level, OCxREF=0 (low level) flat) is called the invalid level.

The signal output here is OCxREF.

Before entering the dead zone generator, you can set the output comparison mode. For outputting PWM waves, there are two modes to choose from, namely PWM1 and PWM2.

2.3 Dead zone generator

After passing through the dead zone generator, two complementary signals with dead zones, OCx_DT and OCxN_DT, will be generated. If no dead zone control is added, the signal entering the output control circuit is directly OCxREF.

Simple PWM wave will not be introduced in detail here.

2.4 Output control

The signal output by the dead zone generator will be divided into two channels, one is the original signal and the other is the inverted signal, which is specifically controlled by the bits CCxP and CCxNP of the register CCER. Whether the polarity selected signal is output from the OCx pin to the external pin CHx/CHxN (that is, whether it is enabled) is configured by the bit CxE/CxNE of the register CCER.

3. Structure

typedef struct
{
  uint16_t TIM_Prescaler;        //时钟预分频,对应PSC
                                 
  uint16_t TIM_CounterMode;      //时钟计数模式,对应3中计数方法
                                 
  uint16_t TIM_Period;           //定时器周期,对应ARR寄存器
                                                             
  uint16_t TIM_ClockDivision;    //时钟分频,设置定时器时钟 CK_INT 频率与死区发生器以及数字滤 
                                 //波器,采样时钟频率分频比。可以选择 1、2、4 分频。

  uint8_t TIM_RepetitionCounter; //重复计数器,对应RAR
                                                                 
} TIM_TimeBaseInitTypeDef;       

typedef struct
{
  uint16_t TIM_OCMode;          //输出模式,PWM1及PWM2,对应寄存器CCMR1->OCxM
                             
  uint16_t TIM_OutputState;     //比较输出使能,对应CCER->CCxE
                             
  uint16_t TIM_OutputNState;    //比较互补输出使能,对应CCER->CCxNE
                              
  uint16_t TIM_Pulse;           //脉冲宽度,即比较寄存器的值,对应CCR1
                             
  uint16_t TIM_OCPolarity;      //输出极性,对应CCER->CCxP

  uint16_t TIM_OCNPolarity;     //互补输出极性,对应CCER->CCxP

  uint16_t TIM_OCIdleState;     //空闲状态比较输出状态,对应CR2->OIS1

  uint16_t TIM_OCNIdleState;    //空闲状态下比较互补输出状态,对应CR2->OIS1N
                               
} TIM_OCInitTypeDef;

 There are two main structures used to configure pwm, one is the time base initialization structure, the other is the output comparison structure, and the time base initialization structure

The structure of the output comparison corresponds to the register in the figure as shown below

 But in addition to these two structures, we also need to enable a register 

Corresponding to the BDER->MOE register, the official introduction is as follows

 Just enable the output pin

4. Programming

To summarize, the goals of programming are:

1. Configure the GPIO pins of the channel

2. Configure the two structures

3. Enable the clock source and enable the output channel

The following program configures a PWM wave with 1Khz and a duty cycle of 40%.

void advance_tim1_gpio_config(void)
{
	//1.结构体
	GPIO_InitTypeDef GPIO_InitStructure;
	
	//2.开时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

	//3.配置
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
		
	//4.初始化
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	//5.使能(无)	
}

void advance_tim1_mode_config()
{
	//1.结构体,时基结构体及输出比较结构体
	TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
	TIM_OCInitTypeDef TIM_OCInitStructure;
	
	//2.开时钟,TIM1的时钟
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
	
	//3.配置及初始化
	/*--------------------时基结构体初始化-------------------------*/
	// PWM 信号的频率 F = TIM_CLK/{(ARR+1)*(PSC+1)}
	
	TIM_TimeBaseStructure.TIM_Period = 9;	                      //ARR寄存器的值
	TIM_TimeBaseStructure.TIM_Prescaler = 7199;	                  //PSC分频的值
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;   //向上计数
	TIM_TimeBaseStructure.TIM_RepetitionCounter=0;                //重复计数器为0
	TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
	
	/*--------------------输出比较结构体初始化-------------------*/
	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;             //输出模式:PWM1
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
	TIM_OCInitStructure.TIM_Pulse = 4;			                  //设置占空比
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;     //有效电平
	TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;  //空闲时比较输出状态
	TIM_OC1Init(TIM1, &TIM_OCInitStructure);

	//4.使能,此处使能为将内部时钟作为TIM1时钟源的使能
	TIM_Cmd(TIM1, ENABLE);									      //使能计数器
	TIM_CtrlPWMOutputs(TIM1, ENABLE);				              //使能输出通道
}

Note: The ARR register and PSC register start counting from 0, so the value must be reduced by one.

PWM wave frequency=TIM_CLK/{(ARR+1)*(PSC+1)}

Among them, TIM_CLK is generally the frequency of APB2, because TIM1 is mounted in APB2, and APB2 is generally 72Mhz.

Duty cycle=TIM_OCInitStructure.TIM_Pulse/(ARR+1)

Personally configure PWM wave, generally follow the following steps

1. Confirm the frequency of PWM wave

2. Confirm the accuracy of the PWM wave (corresponding to the ARR register)

3. Confirm the duty cycle, TIM_OCInitStructure.TIM_Pulse

In addition, there is a function that specifically modifies the duty cycle (that is, modifies the value of TIM_OCInitStructure.TIM_Pulse). I didn't know about it before, so I wrote one myself (please avoid this pitfall).

void TIM_SetCompare1(TIM_TypeDef* TIMx, uint16_t Compare1);

5.Description

In addition to the registers explained in this article, there are also some preload functions, such as TIM_OC1PreloadConfi();

Wait, this function corresponds to CCMR->OC1PE, but I don’t need this function for the time being, so I won’t introduce it in detail. 

The official description is as follows: In addition, for the time base unit, there are shadows under certain registers, and the corresponding shadow registers will not be introduced in detail.

An article on PWM wave control ESC will be published later.

References include "STM32F10x-Chinese Reference Manual" and "STM32 Library Development Practical Guide - Based on the Wildfire Guide Development Board"

Guess you like

Origin blog.csdn.net/qq_62573253/article/details/126335766