STM32 PWM不能正常计数

2019-10-15 19:24发布

下面是我的代码:
u32 mubiao=65000;
u32 dangqian=0;
u16 num=500;
u16 step=0;

void MyTimer2_Init()
{
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    TIM_OCInitTypeDef TIM_OCInitStructure;
    TIM_TimeBaseStructure.TIM_Period = num;
    TIM_TimeBaseStructure.TIM_Prescaler =720;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);

    //TIM_PrescalerConfig(TIM2,71,TIM_PSCReloadMode_Immediate);

    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;     //使能TIM输出
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OCInitStructure.TIM_Pulse = TIM_TimeBaseStructure.TIM_Period/2;
    TIM_OC1Init(TIM2,&TIM_OCInitStructure);
        
    TIM_OC1PreloadConfig(TIM2,TIM_OCPreload_Enable);

    #ifdef    VECT_TAB_RAM
        NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);
    #else
        NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
    #endif

    NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
    NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
    NVIC_Init(&NVIC_InitStructure);

    TIM_ClearFlag(TIM1, TIM_FLAG_Update);
    TIM_ITConfig(TIM2,TIM_IT_CC1,ENABLE);
   
    TIM_Cmd(TIM2,ENABLE);

}

void TIM2_IRQHandler(void)
{
        
    if (TIM_GetITStatus(TIM2, TIM_IT_Update) == RESET)
    {           
        step++;
        if(step>65000)
         {
         TIM_Cmd(TIM2,DISABLE);      
            }      
            TIM_ClearITPendingBit(TIM2 , TIM_IT_Update);
        //    TIM_Cmd(TIM2,ENABLE);

        }      
    }

现在PWM终端可以使用,但是无论怎么调之多就在0.55秒左右中断,不能可控的输出PWM个数。

示波器如上,请大神们帮我看看究竟是因为什么,谢谢原子哥了。

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