STM32F030K6 4路PWM只有1路正常,其它3路没PWM波形,一直是低电平。

2019-08-14 17:59发布

(一) 先贴出能工作的PWM初始化程序,如下
void TIM14_Init()
{        
        RCC->AHBENR|=1<<17;                   //使能PORTA时钟   
        GPIOA->MODER   = 0X68AAA6F5 ;     //PA4/TIM14-CH1复用功能输出
        GPIOA->OTYPER  = 0X00000000; 
        GPIOA->OSPEEDR = 0X00000000; 
        GPIOA->UPDR   = 0X25000000;  
        GPIOA->AFR[0] &=0XFFF0FFFF;;       //清除PA4原来的设置
        GPIOA->AFR[0] |=1<<18;                 //设置PA4为AF4
 
        RCC->APB1ENR|=1<<8;                  //TIM14时钟使能
        TIM14->SC=0;                               //预分频器1分频
        TIM14->ARR=240;                          //设置频率(200K)
        TIM14->CCR1 =72;                         //设置占空比  
        TIM14->EGR |= 1;                           //产生一次更新事件 
        TIM14->CCMR1|=6<<4;                 //CH1 PWM1模式  
        TIM14->CCMR1|=1<<3;                 //CH1预装载使能   
        TIM14->CCER|=1<<0;                   //OC1输出使?高电平有效有效 
        TIM14->CR1=0x0080;                    //ARPE使能
       TIM14->CR1|=0x01;                        //使能定时器14
}


(二) 不能工作的第1路PWM(TIMI-CH1/PA8)初始化程序,如下
void TIM1_Init()
{   
       RCC->AHBENR|=1<<17;                //使能PORTA时钟 
    
      GPIOA->MODER   = 0X68AAA6F5 ;
      GPIOA->OTYPER  = 0X00000000; 
      GPIOA->OSPEEDR = 0X00000000; 
      GPIOA->UPDR   = 0X25000000;  
      GPIOA->AFR[0] &=0XFFFFF0FF;;      //清除PA8原来的设置
      GPIOA->AFR[0] |=1<<9;                  //设置PA8为AF2
 
      RCC->APB2ENR|=1<<11;               //TIM1时钟使能
     TIM1->SC=47;                               //预分频器48分频
     TIM1->ARR=1000;                           //设置频率
     TIM1->CCR1 =500;                          //设置占空比   
     TIM1->CCMR1|=7<<4;                    //CH1 PWM2模式  
     TIM1->CCMR1|=1<<3;                     //CH1预装载使能   
     TIM1->CCER|=1<<0;                       //OC1输出使能   
     TIM1->CR1|=1<<7;                         //ARPE使能
     TIM1->EGR |= 1;                              //产生一次更新事件
     TIM1->CR1|=0x01;                          //使能定时器1
     TIM1->BDTR |=1<<15;
 }


 (三) 不能工作的第2路PWM(TIM3-CH1/PB4)初始化程序,如下
void TIM3_Init()
{   
        RCC->APB1ENR|=1<<1;                   //TIM3时钟使能
        RCC->AHBENR|=1<<18;                   //使能PORTB时钟
        GPIOB->MODER   = 0X55551655;     //PB4复用功能输出   
        GPIOB->OTYPER  = 0X00000000;     //推挽输出
        GPIOB->OSPEEDR = 0X00000000;    //低速
        GPIOB->UPDR   = 0X00004000;      //除了PB7带上拉,其它脚则无上拉/下拉
 
         GPIOB->AFR[0] &=0XFFFFFF0F;        //清除PB4原来的设置
         GPIOB->AFR[0] |=1<<4;                  //设置PB4为AF1
 
         TIM3->SC=47;                               //预分频器48分频
         TIM3->ARR=1000;                          //设置频率
         TIM3->EGR |= 1;                             //产生一次更新事件
         TIM3->CCR1 =500;                         //设置占空比
 
         TIM3->CCMR1|=7<<4;                   //CH1 PWM2模式  
         TIM3->CCMR1|=1<<3;                   //CH1预装载使能   
         TIM3->CCER|=1<<0;                     //OC1输出使能,高电平有效   
      
          TIM3->CR1|=1<<7;                     //ARPE使能
          TIM3->CR1|=0x01;                      //使能定时器3
   }

(三) 不能工作的第3路PWM(TIM16-CH1/PA6)初始化程序,如下
void TIM16_Init()
{             
        RCC->AHBENR|=1<<17;                   //使能PORTA时钟 
        GPIOA->MODER   = 0X68AAA6F5 ;     //PA6复用功能输出
        GPIOA->OTYPER  = 0X00000000; 
        GPIOA->OSPEEDR = 0X00000000; 
        GPIOA->UPDR   = 0X25000000;
     
        GPIOA->AFR[0] &=0XFF0FFFFF;;       //清除PA6原来的设置
        GPIOA->AFR[0] |=1<<20;                //设置PA6为AF5
        GPIOA->AFR[0] |=1<<22;
 
        RCC->APB2ENR|=1<<17;               //TIM16时钟使能
        TIM16->SC=47;                             //预分频器48分频
        TIM16->ARR=1000;                        //设置频率
        TIM16->CCR1 =170;                       //设置占空比  
        TIM16->EGR |= 1;                           //产生一次更新事件 
        TIM16->CCMR1|=6<<4;                 //CH1 PWM1模式  
         TIM16->CCMR1|=1<<3;                //CH1预装载使能   
         TIM16->CCER|=1<<0;                   //OC1输出使能   
         TIM16->CR1=0x0080;                   //ARPE使能
         TIM16->CR1|=0x01;                      //使能定时器16

}


 
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。