PWM波形问题

2019-07-20 20:51发布

本帖最后由 寞明 于 2016-8-7 08:52 编辑

[mw_shl_code=c,true]void TIM1_PWM_Init(u16 arr2,u16 psc2)
{
       
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_BDTRInitTypeDef TIM_BDTRInitStructure;

        //NVIC_InitTypeDef NVIC_InitStructure;

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);       
        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB, ENABLE);  //ê1ÄüGPIOíaéèoíAFIO¸′óÃ1|ÄüÄ£¿éê±Öó
       
       
  GPIO_PinAFConfig(GPIOA,GPIO_PinSource8,GPIO_AF_TIM1);
        GPIO_PinAFConfig(GPIOB,GPIO_PinSource13,GPIO_AF_TIM1);

        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  //¸′óÃíÆíìêä3ö
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOB, &GPIO_InitStructure);//3õê¼»ˉGPIO

       
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;  //¸′óÃíÆíìêä3ö
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);//3õê¼»ˉGPIO

       

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

                 
        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //Ñ¡Ôñ¶¨ê±Æ÷Ä£ê½:TIMÂö3å¿í¶èμ÷ÖÆÄ£ê½2
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±è½Ïêä3öê1Äü
        TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //TIM1_OutputN
        TIM_OCInitStructure.TIM_Pulse = 1440; //éèÖÃ′y×°èë2¶»ñ±è½Ï¼Ä′æÆ÷μÄÂö3åÖμ
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;                      //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
        TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;               //êä3ö¼«DÔ:TIMêä3ö±è½Ï¼«DÔ¸ß
  TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
        TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
        TIM_OC1Init(TIM1, &TIM_OCInitStructure);  //¸ù¾YTIM_OCInitStructÖDÖ¸¶¨μÄ2Îêy3õê¼»ˉíaéèTIMx
               
        //siqu/////////////////////////////////////////////////////////////////////////////////////////////////
        TIM_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
        TIM_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
        TIM_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_OFF;
        //bit7~5 = 111,Ôòdeadtime = (32 + (bit4~bit0)* 16*1/fosc)ns = (32+31)*16*1/72000000 = 14us
        TIM_BDTRInitStructure.TIM_DeadTime = 40; //Õaàïμ÷ÕûËàÇø′óD¡0-0xff            ËàÇø×î′ó14us
        TIM_BDTRInitStructure.TIM_Break = TIM_Break_Disable;
        TIM_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
        TIM_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
        TIM_BDTRConfig(TIM1, &TIM_BDTRInitStructure);
       
        TIM_CtrlPWMOutputs(TIM1,ENABLE);   //MOE Ö÷êä3öê1Äü

        TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  //CH1ԤװÔØê1Äü       
        TIM_ARRPreloadConfig(TIM1, ENABLE); //ê1ÄüTIMxÔúARRéÏμÄԤװÔؼÄ′æÆ÷

        TIM_Cmd(TIM1, ENABLE);  //ê1ÄüTIM1  

}
[/mw_shl_code]从F103的程序移植F407后,波形就变成这个样子了
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。