求大神看看程序,用定时器2通道3输出PWM怎么没反应,定时器2通道3是PA2的复用功能

2019-07-21 07:29发布

void TIM2_PWM_Init(u16 arr,u16 psc)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);

TIM_TimeBaseInitStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStructure.TIM_Prescaler=psc;
TIM_TimeBaseInitStructure.TIM_Period= arr;
TIM_TimeBaseInitStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStructure);

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;  
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
  GPIO_Init(GPIOA, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5
  //GPIO_SetBits(GPIOA,GPIO_Pin_2);  

TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC3Init(TIM2,&TIM_OCInitStructure);

//GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3,ENABLE);
TIM_OC3PreloadConfig(TIM2,TIM_OCPreload_Enable);

TIM_Cmd(TIM2,ENABLE);

}
 int main(void)
 {
  u32 temp=0;
u16 led0pwmval;
u16 dir; 
delay_init();     //延时函数初始化  
NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级
uart_init(9600); //串口初始化为9600
  LED_Init();     //LED端口初始化
  TIM2_PWM_Init(899,0); //不分频。PWM频率=72000/(899+1)=80Khz
    while(1)
   {
       delay_ms(10);
    if(dir)led0pwmval++;
    else led0pwmval--;
   if(led0pwmval>390)dir=0;
    if(led0pwmval==0)dir=1;  
   TIM_SetCompare3(TIM2,led0pwmval);  
   }
}

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