改变占空比,如果只改变CCR4,就不出波形

2019-08-14 20:25发布

本帖最后由 Anthony_brave 于 2016-5-5 11:38 编辑

这是TIM3的初始化
void TIM3_Int_Init(u16 arr,u16 psc)
{
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        NVIC_InitTypeDef NVIC_InitStructure;
        GPIO_InitTypeDef         GPIO_InitStructure;
        TIM_OCInitTypeDef        TIM_OCInitStructure;
        
        RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //ê±Öóê1Ä  

        TIM_TimeBaseStructure.TIM_Period = arr; //éèÖÃÔúÏÂò»¸ö¸üDÂê¼t×°èë»î¶ˉμÄ×Ô¶ˉÖØ×°ÔؼÄ′æÆ÷ÖüÆúμÄÖμ         
        TIM_TimeBaseStructure.TIM_Prescaler =psc; //éèÖÃóÃà′×÷ÎaTIMxê±ÖóÆμÂê3yêyμÄÔ¤·ÖÆμÖμ  
        TIM_TimeBaseStructure.TIM_ClockDivision = 0; //éèÖÃê±Öó·Ö¸î:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòéϼÆêyÄ£ê½
        TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾YTIM_TimeBaseInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉTIMxμÄê±¼ä»ùêyμ¥λ

        TIM_ITConfig  //ê1Äü»òÕßê§ÄüÖ¸¶¨μÄTIMÖD¶Ï
        (
                TIM3, //TIM2
                TIM_IT_Update ,
                ENABLE  //ê1Äü
        );
        NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;  //TIM3ÖD¶Ï
        NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //ÏèÕ¼óÅÏ輶0¼¶
        NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;  //′óóÅÏ輶3¼¶
        NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQí¨μà±»ê1Äü
        NVIC_Init(&NVIC_InitStructure);  //¸ù¾YNVIC_InitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèNVIC¼Ä′æÆ÷

  TIM_ARRPreloadConfig(TIM3, ENABLE);
        TIM_Cmd(TIM3, ENABLE);  //ê1ÄüTIMxíaéè
        //----------------------------------------
        RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOB,ENABLE);
        GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_1;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);

        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
        TIM_OCInitStructure.TIM_Pulse = 0;
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
        TIM_OC4Init(TIM3, &TIM_OCInitStructure);
        TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
        
        TIM_ARRPreloadConfig(TIM3, ENABLE);
  /* TIM3 enable counter */
    TIM_Cmd(TIM3, ENABLE);
        //-------------------------------------------                                                
}

改变占空比  我单独写了一个函数  因为需要经常调用
void TIM3_PWM1(u16 CCR1_Value)
{
   TIM3->CCR4 =CCR1_Value;
}          结果不出波形


进行更改如下:
void TIM3_PWM1(u16 CCR1_Value)
{
         TIM_OCInitTypeDef        TIM_OCInitStructure;  

TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
   TIM_OCInitStructure.TIM_Pulse = CCR1_Value;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
    TIM_OC4Init(TIM3, &TIM_OCInitStructure);
}        结果出波形,但是因为一进TIM中断就需要改变占空比,如果运行以上五句话(时间过长),还是会有不正常波形出现如下图

请问如果精简改变占空比的这个函数  能够发出正常波形???






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