【STM32】TIM2的PWM:脉冲宽度调制

PWM是一种周期固定,脉宽可调整的输出波形。

0.通用寄存器输出

1.捕获/比较通道1的主电路--中间部分

【STM32】TIM2的PWM:脉冲宽度调制_第1张图片

2.捕获/比较通道的输出部分--输出

【STM32】TIM2的PWM:脉冲宽度调制_第2张图片

3.通用定时器输出PWM原理

【STM32】TIM2的PWM:脉冲宽度调制_第3张图片

PWM波周期或者频率由ARR(就是要进递增/递减的值)决定,PWM波占空比由CRRx决定。

1.定时器的PWM输出功能介绍

【STM32】TIM2的PWM:脉冲宽度调制_第4张图片

1)通过定时器的中断,在isr中将一个GPIO引脚电平反转,可以实现PWM输出功能【麻烦,设置多】

2)定时器附带专用的PWM输出功能,定时器那边和某一个引脚绑定,然后定时器设置好了之后内部开始+1或者-1,然后时间到了之后不是产生中断,而是直接将绑定的引脚电平反转产生PWM输出。【CPU不参与,效率高】

1.占空比:脉宽(高电平)占总周期的比例

1)可以用来调制脉冲宽度--》脉冲宽度调制

2)占空比的调节,是通过比较值与计数器的大小差距,当两者的关系改变的时,会进行电平反转。

【STM32】TIM2的PWM:脉冲宽度调制_第5张图片

【STM32】TIM2的PWM:脉冲宽度调制_第6张图片

2.PWM频率

频率越大,切换速度越快,时间段越短

【STM32】TIM2的PWM:脉冲宽度调制_第7张图片

3.PWM占空比和周期

【STM32】TIM2的PWM:脉冲宽度调制_第8张图片

【STM32】TIM2的PWM:脉冲宽度调制_第9张图片

4.PWM1  VS  PWM2

【STM32】TIM2的PWM:脉冲宽度调制_第10张图片

2.专用PWM输出的实现原理

1.比较功能

1)所谓的比较原理,设计3个计数有关的寄存器:CMP(比较),CNT(计数器),ARR(存放计数原始值)

定时器有4个输出通道,每一个通道都有一个捕获/比较寄存器,将寄存器值(ARR)和计数器值(CNT)进行比较,通过比较结果输出高低电平,实现PWM信号输出。

高低电平的1和0可以进行设置

【STM32】TIM2的PWM:脉冲宽度调制_第11张图片

2)在输入捕获/输出比较功能中--都要使用同一个外部引脚

【STM32】TIM2的PWM:脉冲宽度调制_第12张图片

3)每一个定时器只有一个计数器,但是每一个通道都有自己的捕获/比较寄存器,因此对于一个定时器来说,4路输出的PWM频率(周期)都是相同的,而不同通道的占空比可以不同。

2.相关寄存器

1.TIMx_CNT(计数器),TIMx_ARR(自动重装载寄存器),TIMx_CCRn(捕获/比较寄存器)

TIMx_CCRn:是来选择哪一条通道

【STM32】TIM2的PWM:脉冲宽度调制_第13张图片

2.CCMR1,CCMR2,CCER:捕获/比较模式寄存器

CCMR1:处理了通道1和通道2

CCMR2:处理了通道3和通道4

CCER:配置要什么电平才是有效的

【STM32】TIM2的PWM:脉冲宽度调制_第14张图片

【STM32】TIM2的PWM:脉冲宽度调制_第15张图片

【STM32】TIM2的PWM:脉冲宽度调制_第16张图片

【STM32】TIM2的PWM:脉冲宽度调制_第17张图片

3.CR1,CR2,PSC

CR1,CR2:使能,开关

PSC:分频功能

3.标准库中相关的API

1.TIM_TimeBaseInit

void TIM_TimeBaseInit(TIM_TypeDef* TIMx, TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct)
{
  uint16_t tmpcr1 = 0;

  /* Check the parameters */
  assert_param(IS_TIM_ALL_PERIPH(TIMx)); 
  assert_param(IS_TIM_COUNTER_MODE(TIM_TimeBaseInitStruct->TIM_CounterMode));
  assert_param(IS_TIM_CKD_DIV(TIM_TimeBaseInitStruct->TIM_ClockDivision));

  tmpcr1 = TIMx->CR1;  

  if((TIMx == TIM1) || (TIMx == TIM8)|| (TIMx == TIM2) || (TIMx == TIM3)||
     (TIMx == TIM4) || (TIMx == TIM5)) 
  {
    /* Select the Counter Mode */
    tmpcr1 &= (uint16_t)(~((uint16_t)(TIM_CR1_DIR | TIM_CR1_CMS)));
    tmpcr1 |= (uint32_t)TIM_TimeBaseInitStruct->TIM_CounterMode;
  }
 
  if((TIMx != TIM6) && (TIMx != TIM7))
  {
    /* Set the clock division */
    tmpcr1 &= (uint16_t)(~((uint16_t)TIM_CR1_CKD));
    tmpcr1 |= (uint32_t)TIM_TimeBaseInitStruct->TIM_ClockDivision;
  }

  TIMx->CR1 = tmpcr1;

  /* Set the Autoreload value */
  //要计数的值
  TIMx->ARR = TIM_TimeBaseInitStruct->TIM_Period ;
 
  /* Set the Prescaler value */
  //预分频参数
  TIMx->PSC = TIM_TimeBaseInitStruct->TIM_Prescaler;
    
  if ((TIMx == TIM1) || (TIMx == TIM8)|| (TIMx == TIM15)|| (TIMx == TIM16) || (TIMx == TIM17))  
  {
    /* Set the Repetition Counter value */
    TIMx->RCR = TIM_TimeBaseInitStruct->TIM_RepetitionCounter;
  }

  /* Generate an update event to reload the Prescaler and the Repetition counter
     values immediately */
     //预分频器参数的改变
  TIMx->EGR = TIM_PSCReloadMode_Immediate;           
}

2.TIM_OC1Init(TIM_OCnInit)

TIM_OCn--->指的使用了哪一个通道

void TIM_OC1Init(TIM_TypeDef* TIMx, TIM_OCInitTypeDef* TIM_OCInitStruct)
{
  uint16_t tmpccmrx = 0, tmpccer = 0, tmpcr2 = 0;
   
  /* Check the parameters */
  assert_param(IS_TIM_LIST8_PERIPH(TIMx));
  assert_param(IS_TIM_OC_MODE(TIM_OCInitStruct->TIM_OCMode));
  assert_param(IS_TIM_OUTPUT_STATE(TIM_OCInitStruct->TIM_OutputState));
  assert_param(IS_TIM_OC_POLARITY(TIM_OCInitStruct->TIM_OCPolarity));   
 /* Disable the Channel 1: Reset the CC1E Bit */
  TIMx->CCER &= (uint16_t)(~(uint16_t)TIM_CCER_CC1E);
  /* Get the TIMx CCER register value */
  tmpccer = TIMx->CCER;
  /* Get the TIMx CR2 register value */
  tmpcr2 =  TIMx->CR2;
  
  /* Get the TIMx CCMR1 register value */
  tmpccmrx = TIMx->CCMR1;
    
  /* Reset the Output Compare Mode Bits */
  tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_OC1M));
  tmpccmrx &= (uint16_t)(~((uint16_t)TIM_CCMR1_CC1S));

  /* Select the Output Compare Mode */
  tmpccmrx |= TIM_OCInitStruct->TIM_OCMode;
  
  /* Reset the Output Polarity level */
  tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC1P));
  /* Set the Output Compare Polarity */
  tmpccer |= TIM_OCInitStruct->TIM_OCPolarity;
  
  /* Set the Output State */
  tmpccer |= TIM_OCInitStruct->TIM_OutputState;
    
  if((TIMx == TIM1) || (TIMx == TIM8)|| (TIMx == TIM15)||
     (TIMx == TIM16)|| (TIMx == TIM17))
  {
    assert_param(IS_TIM_OUTPUTN_STATE(TIM_OCInitStruct->TIM_OutputNState));
    assert_param(IS_TIM_OCN_POLARITY(TIM_OCInitStruct->TIM_OCNPolarity));
    assert_param(IS_TIM_OCNIDLE_STATE(TIM_OCInitStruct->TIM_OCNIdleState));
    assert_param(IS_TIM_OCIDLE_STATE(TIM_OCInitStruct->TIM_OCIdleState));
    
    /* Reset the Output N Polarity level */
    tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC1NP));
    /* Set the Output N Polarity */
    tmpccer |= TIM_OCInitStruct->TIM_OCNPolarity;
    
    /* Reset the Output N State */
    tmpccer &= (uint16_t)(~((uint16_t)TIM_CCER_CC1NE));    
    /* Set the Output N State */
    tmpccer |= TIM_OCInitStruct->TIM_OutputNState;
    
    /* Reset the Output Compare and Output Compare N IDLE State */
    tmpcr2 &= (uint16_t)(~((uint16_t)TIM_CR2_OIS1));
    tmpcr2 &= (uint16_t)(~((uint16_t)TIM_CR2_OIS1N));
    
    /* Set the Output Idle state */
    tmpcr2 |= TIM_OCInitStruct->TIM_OCIdleState;
    /* Set the Output N Idle state */
    tmpcr2 |= TIM_OCInitStruct->TIM_OCNIdleState;
  }
  /* Write to TIMx CR2 */
  TIMx->CR2 = tmpcr2;
  
  /* Write to TIMx CCMR1 */
  TIMx->CCMR1 = tmpccmrx;

  /* Set the Capture Compare Register value */
  TIMx->CCR1 = TIM_OCInitStruct->TIM_Pulse; 
 
  /* Write to TIMx CCER */
  TIMx->CCER = tmpccer;
}

3.TIM_OCInitTypeDef:OC的结构体

typedef struct
{
//选择TIM的模式
  uint16_t TIM_OCMode;        /*!< Specifies the TIM mode.
                                   This parameter can be a value of @ref TIM_Output_Compare_and_PWM_modes */
//选择TIM的输出状态
  uint16_t TIM_OutputState;   /*!< Specifies the TIM Output Compare state.
                                   This parameter can be a value of @ref TIM_Output_Compare_state */

  uint16_t TIM_OutputNState;  /*!< Specifies the TIM complementary Output Compare state.
                                   This parameter can be a value of @ref TIM_Output_Compare_N_state
                                   @note This parameter is valid only for TIM1 and TIM8. */
//要进行比较的值
  uint16_t TIM_Pulse;         /*!< Specifies the pulse value to be loaded into the Capture Compare Register. 
                                   This parameter can be a number between 0x0000 and 0xFFFF */
//输出的极性
  uint16_t TIM_OCPolarity;    /*!< Specifies the output polarity.
                                   This parameter can be a value of @ref TIM_Output_Compare_Polarity */

  uint16_t TIM_OCNPolarity;   /*!< Specifies the complementary output polarity.
                                   This parameter can be a value of @ref TIM_Output_Compare_N_Polarity
                                   @note This parameter is valid only for TIM1 and TIM8. */

  uint16_t TIM_OCIdleState;   /*!< Specifies the TIM Output Compare pin state during Idle state.
                                   This parameter can be a value of @ref TIM_Output_Compare_Idle_State
                                   @note This parameter is valid only for TIM1 and TIM8. */

  uint16_t TIM_OCNIdleState;  /*!< Specifies the TIM Output Compare pin state during Idle state.
                                   This parameter can be a value of @ref TIM_Output_Compare_N_Idle_State
                                   @note This parameter is valid only for TIM1 and TIM8. */
} TIM_OCInitTypeDef;

1.TIM_OCMode:选择TIM的模式

【STM32】TIM2的PWM:脉冲宽度调制_第18张图片

【STM32】TIM2的PWM:脉冲宽度调制_第19张图片

2.TIM_OutputState:选择输出类型

选择输出有效电平类型

【STM32】TIM2的PWM:脉冲宽度调制_第20张图片

【STM32】TIM2的PWM:脉冲宽度调制_第21张图片

3.TIM_Pulse:输入要进行比较的值

【STM32】TIM2的PWM:脉冲宽度调制_第22张图片

【STM32】TIM2的PWM:脉冲宽度调制_第23张图片

4.TIM_OCPolarity:设置输出极性

【STM32】TIM2的PWM:脉冲宽度调制_第24张图片

【STM32】TIM2的PWM:脉冲宽度调制_第25张图片

4.TIM_OCnPreloadConfig

void TIM_ForcedOC1Config(TIM_TypeDef* TIMx, uint16_t TIM_ForcedAction)
{
  uint16_t tmpccmr1 = 0;
  /* Check the parameters */
  assert_param(IS_TIM_LIST8_PERIPH(TIMx));
  assert_param(IS_TIM_FORCED_ACTION(TIM_ForcedAction));
  tmpccmr1 = TIMx->CCMR1;
  /* Reset the OC1M Bits */
  tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC1M);
  /* Configure The Forced output Mode */
  tmpccmr1 |= TIM_ForcedAction;
  /* Write to TIMx CCMR1 register */
  TIMx->CCMR1 = tmpccmr1;
}

5.TIM_OC1PreloadConfig

【STM32】TIM2的PWM:脉冲宽度调制_第26张图片

【STM32】TIM2的PWM:脉冲宽度调制_第27张图片

void TIM_OC1PreloadConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPreload)
{
  uint16_t tmpccmr1 = 0;
  /* Check the parameters */
  assert_param(IS_TIM_LIST8_PERIPH(TIMx));
  assert_param(IS_TIM_OCPRELOAD_STATE(TIM_OCPreload));
  tmpccmr1 = TIMx->CCMR1;
  /* Reset the OC1PE Bit */
  tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC1PE);
  /* Enable or Disable the Output Compare Preload feature */
  tmpccmr1 |= TIM_OCPreload;
  /* Write to TIMx CCMR1 register */
  TIMx->CCMR1 = tmpccmr1;
}

6.TIM_ClearOC1Ref

【STM32】TIM2的PWM:脉冲宽度调制_第28张图片

void TIM_ClearOC1Ref(TIM_TypeDef* TIMx, uint16_t TIM_OCClear)
{
  uint16_t tmpccmr1 = 0;
  /* Check the parameters */
  assert_param(IS_TIM_LIST3_PERIPH(TIMx));
  assert_param(IS_TIM_OCCLEAR_STATE(TIM_OCClear));

  tmpccmr1 = TIMx->CCMR1;

  /* Reset the OC1CE Bit */
  tmpccmr1 &= (uint16_t)~((uint16_t)TIM_CCMR1_OC1CE);
  /* Enable or Disable the Output Compare Clear Bit */
  tmpccmr1 |= TIM_OCClear;
  /* Write to TIMx CCMR1 register */
  TIMx->CCMR1 = tmpccmr1;
}

7.TIM_OC1PolarityConfig

void TIM_OC1PolarityConfig(TIM_TypeDef* TIMx, uint16_t TIM_OCPolarity)
{
  uint16_t tmpccer = 0;
  /* Check the parameters */
  assert_param(IS_TIM_LIST8_PERIPH(TIMx));
  assert_param(IS_TIM_OC_POLARITY(TIM_OCPolarity));
  tmpccer = TIMx->CCER;
  /* Set or Reset the CC1P Bit */
  tmpccer &= (uint16_t)~((uint16_t)TIM_CCER_CC1P);
  tmpccer |= TIM_OCPolarity;
  /* Write to TIMx CCER register */
  TIMx->CCER = tmpccer;
}

4.GPIO引脚和PWM的对应关系

STM32F103中文教程及参考手册.pdf · 林何/STM32F103C8 - 码云 - 开源中国 (gitee.com)

在AFIO中进行查找

没有重映像:表示默认接入的io口

完全重映像:如果使用这个则要调用函数进行声明【GPIO_PinRemapConfig】

【STM32】TIM2的PWM:脉冲宽度调制_第29张图片

5.TIM2的专用PWM输出编程实践

1.官方示例代码

我们使用的是TIM3,因为我们复用了GPIOA,所以要去AFIO中去查找TIM3对应的关系

【STM32】TIM2的PWM:脉冲宽度调制_第30张图片

#include "pwm.h"
#include "led.h"

//PWM输出初始化
//arr:自动重装值
//psc:时钟预分频数
void TIM1_PWM_Init(u16 arr,u16 psc)
{  
	GPIO_InitTypeDef GPIO_InitStructure;
	TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
	TIM_OCInitTypeDef  TIM_OCInitStructure;
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);			// 
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);  		//使能GPIO外设时钟使能
	                                                                     	
	//设置该引脚为复用输出功能,输出TIM1 CH1的PWM脉冲波形
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; 						//TIM_CH1
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  				//复用推挽输出
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);

	TIM_TimeBaseStructure.TIM_Period = arr; 						//设置在下一个更新事件装入活动的自动重装载寄存器周期的值	 80K
	TIM_TimeBaseStructure.TIM_Prescaler =psc; 						//设置用来作为TIMx时钟频率除数的预分频值  不分频
	TIM_TimeBaseStructure.TIM_ClockDivision = 0; 					//设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;		//TIM向上计数模式
	TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); 				//根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位

	TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; 				//选择定时器模式:TIM脉冲宽度调制模式2
	TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;	//比较输出使能
	TIM_OCInitStructure.TIM_Pulse = 0;								//设置待装入捕获比较寄存器的脉冲值
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; 		//输出极性:TIM输出比较极性高
	TIM_OC1Init(TIM1, &TIM_OCInitStructure);  						//根据TIM_OCInitStruct中指定的参数初始化外设TIMx

	TIM_CtrlPWMOutputs(TIM1,ENABLE);								//MOE 主输出使能	

	TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  				//CH1预装载使能	 
	
	TIM_ARRPreloadConfig(TIM1, ENABLE); 							//使能TIMx在ARR上的预装载寄存器
	
	TIM_Cmd(TIM1, ENABLE);  										//使能TIM1
}

2.代码移植

我们先去查看我们进行操作的TIM2对应应该复用哪一个AFIO引脚

【STM32】TIM2的PWM:脉冲宽度调制_第31张图片

可知TIM2的通道1对于的没有重映像是PA0

【STM32】TIM2的PWM:脉冲宽度调制_第32张图片

#include "stm32f10x.h"                  // Device header
/**
	使用TIM2的Channel1,无重映射时对应PA0引脚,在原理图上对应P1.0
*/

void pwm_init(void);


int main(){
	
	pwm_init(); //频率是2Kh
	return 0;
}
 
void pwm_init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;   //声明一个结构体变量,用来初始化GPIO

	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;//声明一个结构体变量,用来初始化定时器

	TIM_OCInitTypeDef TIM_OCInitStructure;//根据TIM_OCInitStruct中指定的参数初始化外设TIMx

	/* 开启时钟 */
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

	/*  配置GPIO的模式和IO口 */
	GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;//复用推挽输出
	GPIO_Init(GPIOA,&GPIO_InitStructure);		// GPA15,



	// time = CNT/fHz = 9000/72000000s
	// Fpwm = 1/T = 72000000/9000Hz = 8000Hz = 8KHz
	//TIM3定时器初始化
	TIM_TimeBaseInitStructure.TIM_Period = 9000 - 1;	   //不分频,PWM 频率=72000/900=8Khz//设置自动重装载寄存器周期的值
	TIM_TimeBaseInitStructure.TIM_Prescaler = 0;//设置用来作为TIMx时钟频率预分频值,100Khz计数频率
	TIM_TimeBaseInitStructure.TIM_ClockDivision = 0;//设置时钟分割:TDTS = Tck_tim
	TIM_TimeBaseInitStructure.TIM_CounterMode = TIM_CounterMode_Up;	//TIM向上计数模式
	TIM_TimeBaseInit(TIM2, & TIM_TimeBaseInitStructure);

	// 将TIM2的输出引脚进行fll remap到PA15,也就是P3.7
	//GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE);


	//PWM初始化	  //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
	TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;
	TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;//PWM输出使能
	//TIM_OCInitStructure.TIM_Pulse = 4500 - 1;
	//TIM_Pulse:设置占空比【占了1/3==3000/9000】
	TIM_OCInitStructure.TIM_Pulse = 3000 - 1;
	TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

	TIM_OC1Init(TIM2,&TIM_OCInitStructure);

	TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);
	
	TIM_Cmd(TIM2,ENABLE);//使能或者失能TIMx外设
}







【STM32】TIM2的PWM:脉冲宽度调制_第33张图片

你可能感兴趣的:(STM32,stm32,嵌入式硬件,单片机)