STM32——PWM

0.

  1)一般STM32有8个16位定时器

  2)TIM1、TIM8是高级定时器;TIM2、TIM3、TIM4、TIM5是通用定时器;TIM6、TIM7是基本定时器(不具备PWM功能)

  3)如何产生PWM脉冲?

    方法:通用定时器可以利用GPIO引脚进行脉冲输出

    配置:

      捕获/比较寄存器TIMx_CCR = A    (TIM_Pulse = A)

      脉冲计数器TIMx_CNT = 向上计数    (TIM_CounterMode = TIM_CounterMode_Up)

      重载寄存器TIMx_ARR = N    (TIM_Period = N)

    工作原理:

      TIMx_CNT在TIMxCLK时钟源的驱动下不断累加,当TIMx_CNT的值X大于N时,会重置TIMx_CNT数值为0重新计数

      TIMx_CNT计数的同时,TIMx_CNT的值X会与比较寄存器TIMx_CCR预先储存的值A进行比较。①TIMx_CNT的值X < 比较寄存器TIMx_CCR的值A时,输出高电平(或低电平);②TIMx_CNT的值X >= 比较寄存器TIMx_CCR的值A时,输出低电平(或高电平);

      输出脉冲PWM的占空比 = A / (N + 1)


 1.TIM_CH对应引脚(STM32F10x)

**
  TIM1_CH1  -  PA8
  TIM1_CH2  -  PA9
  TIM1_CH3  -  PA10
  TIM1_CH4  -  PA11
**
  TIM2_CH1  -  PA0
  TIM2_CH2  -  PA1
  TIM2_CH3  -  PA2
  TIM2_CH4  -  PA3
**
  TIM3_CH1  -  PA6
  TIM3_CH2  -  PA7
  TIM3_CH3  -  PB0
  TIM3_CH4  -  PB1
**
  TIM4_CH1  -  PB6
  TIM4_CH2  -  PB7
  TIM4_CH3  -  PB8
  TIM4_CH4  -  PB9
**
  TIM5_CH1  -  PA0
  TIM5_CH2  -  PA1
  TIM5_CH3  -  PA2
  TIM5_CH4  -  PA3
**
  TIM8_CH1  -  PC6
  TIM8_CH2  -  PC7
  TIM8_CH3  -  PC8
  TIM8_CH4  -  PC9


2.代码:PA6口输出20 / (999 + 1) * 板载电压

#include 

void TIM3_GPIO_Config(void);
void PWM_Config(u8 CCR_Value);

int main()
{
    TIM3_GPIO_Config();
    PWM_Config(20);
    
    while(1);
}

void TIM3_GPIO_Config()
{
    //配置TIM3复位输出PWM的IO
    GPIO_InitTypeDef GPIO_InitStructure;
    
    //打开TIM3的时钟
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
    //打开GPIOA的时钟
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    //配置PA6的工作模式
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void PWM_Config(u8 CCR_Value)
{
    //初始化TIM3的时间基数单位
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    //初始化TIM3的外设
    TIM_OCInitTypeDef TIM_OCInitStructure;
    
    //TIM3的时间基数单位设置(如计数终止值:999,从0开始;技术方式:向上计数)
    TIM_TimeBaseStructure.TIM_Period = 999;
    TIM_TimeBaseStructure.TIM_Prescaler = 0;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
    
    //TIM3的外设的设置
    //设置输出模式:TIM脉冲宽度调制模式1
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    //输出状态使能
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    //设置待装入捕获比较寄存器的脉冲值
    TIM_OCInitStructure.TIM_Pulse = CCR_Value;
    //TIM输出比较极性高
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    
    /******************************************************************************/
    /**************************
    **
    **由复用引脚决定
    **
    ***************************/
    TIM_OC1Init(TIM3, &TIM_OCInitStructure);
    //使能或失能TIMx在CCR上预装载寄存器
    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
    /******************************************************************************/
    
    //使能TIM3 重载寄存器ARR
    TIM_ARRPreloadConfig(TIM3, ENABLE);
    
    //使能TIM3
    TIM_Cmd(TIM3, ENABLE);
}

 

转载于:https://www.cnblogs.com/maplesnow/p/5388901.html

你可能感兴趣的:(STM32——PWM)