f103zet6高级定时器和通用定时器 PWM输出问题

2019-07-21 03:01发布

本帖最后由 牧马人人 于 2019-4-11 08:08 编辑

本人参照定时器3的配置方法,将定时器2,4,5配置,定时器3,4能够正常输出4路PWM控制舵机,但是定时器5无PWM输出,其中定时器2为了避免与定时器5引脚冲突,定时器2采用完全重映射将引脚映射到其他空闲引脚,映射后的四个引脚只有PB10,PB11正常输出PWM;另外两个引脚不输出。两个高级定时器time1和time8配置后也无PWM输出。求大佬指点   定时器1,2,3,4,5,8配置程序如下:


void TIM1_PWM_Init(u16 arr,u16 psc)
{        
        GPIO_InitTypeDef GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);//
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE|RCC_APB2Periph_GPIOA , ENABLE);  //??GPIO??????
   //????????????,??TIM1 CH1 CH2 CH3 CH4?PWM????
   
        GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE); //Timer1ÍêÈ«ÖØÓ³Éä   
   
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_11|GPIO_Pin_13|GPIO_Pin_14; //TIM1 CH1 CH2 CH3
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //??????
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOE, &GPIO_InitStructure);
   
//        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11; //TIM1 CH1 CH2 CH3
//        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //??????
//        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//        GPIO_Init(GPIOA, &GPIO_InitStructure);

        TIM_TimeBaseStructure.TIM_Period = arr; //???????????????????????????         
        TIM_TimeBaseStructure.TIM_Prescaler =psc; //??????TIMx???????????  ???
        TIM_TimeBaseStructure.TIM_ClockDivision = 0; //??????:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM??????
        TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); //??TIM_TimeBaseInitStruct?????????TIMx???????

        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //???????:TIM????????1
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //??????
       // TIM_OCInitStructure.TIM_Pulse = 0;                            //????????????????
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;     //????:TIM???????
        TIM_OC1Init(TIM1, &TIM_OCInitStructure);  //??TIM_OCInitStruct???????????TIMx
        TIM_OC2Init(TIM1, &TIM_OCInitStructure);  //??TIM_OCInitStruct???????????TIMx
        TIM_OC3Init(TIM1, &TIM_OCInitStructure);  //??TIM_OCInitStruct???????????TIMx
        TIM_OC4Init(TIM1, &TIM_OCInitStructure);  //??TIM_OCInitStruct???????????TIMx
      
        TIM_CtrlPWMOutputs(TIM1,ENABLE);        //MOE ?????      
      
        TIM_OC1PreloadConfig(TIM1, TIM_OCPreload_Enable);  //CH1?????         
        TIM_OC2PreloadConfig(TIM1, TIM_OCPreload_Enable);  //CH2?????         
        TIM_OC3PreloadConfig(TIM1, TIM_OCPreload_Enable);  //CH3?????         
        TIM_OC4PreloadConfig(TIM1, TIM_OCPreload_Enable);  //CH3?????        
      
      
        TIM_ARRPreloadConfig(TIM1, ENABLE); //??TIMx?ARR????????
        TIM_Cmd(TIM1, ENABLE);  //??TIM1
   
    // ËĸöͨµÀÈ«²¿Ê¹ÄÜ  CH1--PA8  CH2--PA9  CH3--PA10   CH4--PA11  
    //  ÍêÈ«ÖØÓ³Éäºó     CH1--PE9  CH2--PE11  CH3--PE13    CH4--PE14   
}

void TIM2_PWM_Init(u16 arr,u16 psc)
{

     GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
   
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);    //ʹÄܶ¨Ê±Æ÷2ʱÖÓ
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB| RCC_APB2Periph_AFIO, ENABLE);  //ʹÄÜGPIOÍâÉèºÍAFIO¸´Óù¦ÄÜÄ£¿éʱÖÓ
   
    GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE); //Timer3²¿·ÖÖØÓ³Éä  TIM3_CH2->B5   

   //ÉèÖøÃÒý½ÅΪ¸´ÓÃÊä³ö¹¦ÄÜ,Êä³öTIM2 CH1--4 µ&#196WMÂö³å²¨ÐΠ  
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //TIM_CH2
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //¸´ÓÃÍÆÍìÊä³ö
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIO
   
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_10|GPIO_Pin_11; //TIM_CH2
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //¸´ÓÃÍÆÍìÊä³ö
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯GPIO
   

   //³õʼ»¯TIM2
    TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ
    TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ
    TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòÉϼÆÊýģʽ
    TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
   
   
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐԵ͠   ###//¸Ä¶¯¼«ÐÔ //###
   
    TIM_OC1Init(TIM2, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC1
    TIM_OC2Init(TIM2, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC2
    TIM_OC3Init(TIM2, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC3
    TIM_OC4Init(TIM2, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC4

    TIM_OC1PreloadConfig(TIM2, TIM_OCPreload_Enable);  //ʹÄÜTIM3ÔÚCCR1ÉϵÄԤװÔؼĴæÆ÷
     TIM_OC2PreloadConfig(TIM2, TIM_OCPreload_Enable);  //ʹÄÜTIM3ÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷
     TIM_OC3PreloadConfig(TIM2, TIM_OCPreload_Enable);  //ʹÄÜTIM3ÔÚCCR3ÉϵÄԤװÔؼĴæÆ÷
     TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);  //ʹÄÜTIM3ÔÚCCR4ÉϵÄԤװÔؼĴæÆ÷
   
    TIM_Cmd(TIM2, ENABLE);  //ʹÄÜTIM2
   
// ËĸöͨµÀÈ«²¿Ê¹ÄÜ  CH1--PA0  CH2--PA1  CH3--PA2   CH4--PA3
      
}

void TIM3_PWM_Init(u16 arr,u16 psc)
{  
    GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
   

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);    //ʹÄܶ¨Ê±Æ÷3ʱÖÓ
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA| RCC_APB2Periph_AFIO, ENABLE);  //ʹÄÜGPIOÍâÉèºÍAFIO¸´Óù¦ÄÜÄ£¿éʱÖÓ
   
    //GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3²¿·ÖÖØÓ³Éä  TIM3_CH2->B5   

   //ÉèÖøÃÒý½ÅΪ¸´ÓÃÊä³ö¹¦ÄÜ,Êä³öTIM3 CH2µ&#196WMÂö³å²¨ÐΠ   GPIOB.5
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //TIM_CH2
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //¸´ÓÃÍÆÍìÊä³ö
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIO
   
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //TIM_CH2
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //¸´ÓÃÍÆÍìÊä³ö
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);//³õʼ»¯GPIO
   

   //³õʼ»¯TIM3
    TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ
    TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ
    TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòÉϼÆÊýģʽ
    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
   
    //³õʼ»¯TIM3 Channel2 PWMģʽ     
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐԵ͠   ###//¸Ä¶¯¼«ÐÔ //###
   
    TIM_OC1Init(TIM3, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC1
    TIM_OC2Init(TIM3, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC2
    TIM_OC3Init(TIM3, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC3
    TIM_OC4Init(TIM3, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC4

    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);  //ʹÄÜTIM3ÔÚCCR1ÉϵÄԤװÔؼĴæÆ÷
     TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);  //ʹÄÜTIM3ÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷
     TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);  //ʹÄÜTIM3ÔÚCCR3ÉϵÄԤװÔؼĴæÆ÷
     TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);  //ʹÄÜTIM3ÔÚCCR4ÉϵÄԤװÔؼĴæÆ÷
   
    TIM_Cmd(TIM3, ENABLE);  //ʹÄÜTIM3
   
// ËĸöͨµÀÈ«²¿Ê¹ÄÜ  CH1--PA6  CH2--PA7  CH3--PB0   CH4--PB1


}
   
void TIM4_PWM_Init(u16 arr,u16 psc)
{        
     GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
   
      

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);    //ʹÄܶ¨Ê±Æ÷4ʱÖÓ
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE);  //ʹÄÜGPIOÍâÉèºÍAFIO¸´Óù¦ÄÜÄ£¿éʱÖÓ
   
    GPIO_PinRemapConfig(GPIO_Remap_TIM4, ENABLE); //Timer1ÍêÈ«ÖØÓ³Éä  TIM3_CH2->B5
   
    //GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //Timer3²¿·ÖÖØÓ³Éä  TIM3_CH2->B5   

   //ÉèÖøÃÒý½ÅΪ¸´ÓÃÊä³ö¹¦ÄÜ,Êä³öTIM4 CH2µ&#196WMÂö³å²¨ÐΠ  
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; //TIM_CH2
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //¸´ÓÃÍÆÍìÊä³ö
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOD, &GPIO_InitStructure);//³õʼ»¯GPIO
   
   
   //³õʼ»¯TIM3
    TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ
    TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ
    TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòÉϼÆÊýģʽ
    TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
   
    //³õʼ»¯TIM4 Channel2 PWMģʽ     
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐԵ͠   ###//¸Ä¶¯¼«ÐÔ //###
   
    TIM_OC1Init(TIM4, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC1
    TIM_OC2Init(TIM4, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC2
    TIM_OC3Init(TIM4, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC3
    TIM_OC4Init(TIM4, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC4

    TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);  //ʹÄÜTIM3ÔÚCCR1ÉϵÄԤװÔؼĴæÆ÷
     TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);  //ʹÄÜTIM3ÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷
     TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);  //ʹÄÜTIM3ÔÚCCR3ÉϵÄԤװÔؼĴæÆ÷
     TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);  //ʹÄÜTIM3ÔÚCCR4ÉϵÄԤװÔؼĴæÆ÷
   
    TIM_Cmd(TIM4, ENABLE);  //ʹÄÜTIM4
   
// ËĸöͨµÀÈ«²¿Ê¹ÄÜ  CH1--PB6  CH2--PB7  CH3--PB8   CH4--PB9
   
}

void TIM5_PWM_Init(u16 arr,u16 psc)
{
         GPIO_InitTypeDef GPIO_InitStructure;
    TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
    TIM_OCInitTypeDef  TIM_OCInitStructure;
   

    RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE);    //ʹÄܶ¨Ê±Æ÷2ʱÖÓ
     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);  //ʹÄÜGPIOÍâÉèºÍAFIO¸´Óù¦ÄÜÄ£¿éʱÖÓ
   
    //GPIO_PinRemapConfig(GPIO_FullRemap_TIM2, ENABLE); //Timer3²¿·ÖÖØÓ³Éä  TIM3_CH2->B5   

   //ÉèÖøÃÒý½ÅΪ¸´ÓÃÊä³ö¹¦ÄÜ,Êä³öTIM2 CH1--4 µ&#196WMÂö³å²¨ÐΠ  
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3; //TIM_CH2
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //¸´ÓÃÍÆÍìÊä³ö
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIO
   
   
   

   //³õʼ»¯TIM2
    TIM_TimeBaseStructure.TIM_Period = arr; //ÉèÖÃÔÚÏÂÒ»¸ö¸üÐÂʼþ×°Èë»î¶¯µÄ×Ô¶¯ÖØ×°ÔؼĴæÆ÷ÖÜÆÚµÄÖµ
    TIM_TimeBaseStructure.TIM_Prescaler =psc; //ÉèÖÃÓÃÀ´×÷ΪTIMxʱÖÓƵÂʳýÊýµÄÔ¤·ÖƵֵ
    TIM_TimeBaseStructure.TIM_ClockDivision = 0; //ÉèÖÃʱÖÓ·Ö¸î:TDTS = Tck_tim
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIMÏòÉϼÆÊýģʽ
    TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //¸ù¾ÝTIM_TimeBaseInitStructÖÐÖ¸¶¨µÄ²ÎÊý³õʼ»¯TIMxµÄʱ¼ä»ùÊýµ¥Î»
   
    //³õʼ»¯TIM2 Channel2 PWMģʽ     
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //Ñ¡Ôñ¶¨Ê±Æ÷ģʽ:TIMÂö³å¿í¶Èµ÷ÖÆģʽ2
     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //±È½ÏÊä³öʹÄÜ
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //Êä³ö¼«ÐÔ:TIMÊä³ö±È½Ï¼«ÐԵ͠   ###//¸Ä¶¯¼«ÐÔ //###
   
    TIM_OC1Init(TIM5, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC1
    TIM_OC2Init(TIM5, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC2
    TIM_OC3Init(TIM5, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC3
    TIM_OC4Init(TIM5, &TIM_OCInitStructure);  //¸ù¾ÝTÖ¸¶¨µÄ²ÎÊý³õʼ»¯ÍâÉèTIM3 OC4

    TIM_OC1PreloadConfig(TIM5, TIM_OCPreload_Enable);  //ʹÄÜTIM3ÔÚCCR1ÉϵÄԤװÔؼĴæÆ÷
     TIM_OC2PreloadConfig(TIM5, TIM_OCPreload_Enable);  //ʹÄÜTIM3ÔÚCCR2ÉϵÄԤװÔؼĴæÆ÷
     TIM_OC3PreloadConfig(TIM5, TIM_OCPreload_Enable);  //ʹÄÜTIM3ÔÚCCR3ÉϵÄԤװÔؼĴæÆ÷
     TIM_OC4PreloadConfig(TIM5, TIM_OCPreload_Enable);  //ʹÄÜTIM3ÔÚCCR4ÉϵÄԤװÔؼĴæÆ÷
   
    TIM_Cmd(TIM5, ENABLE);  //ʹÄÜTIM2
   
// ËĸöͨµÀÈ«²¿Ê¹ÄÜ  CH1--PA0  CH2--PA1  CH3--PA2   CH4--PA3
      
   
}
void TIM8_PWM_Init(u16 arr,u16 psc)
{  

       GPIO_InitTypeDef GPIO_InitStructure;
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM8, ENABLE);//
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);  //??GPIO??????
   
    //GPIO_PinRemapConfig(GPIO_FullRemap_TIM1, ENABLE); //Timer1ÍêÈ«ÖØÓ³Éä  TIM3_CH2->B5   
   
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9; //TIM1 CH1 CH2 CH3
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //??????
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOC, &GPIO_InitStructure);

        TIM_TimeBaseStructure.TIM_Period = arr; //???????????????????????????         
        TIM_TimeBaseStructure.TIM_Prescaler =psc; //??????TIMx???????????  ???
        TIM_TimeBaseStructure.TIM_ClockDivision = 0; //??????:TDTS = Tck_tim
        TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM??????
        TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure); //??TIM_TimeBaseInitStruct?????????TIMx???????

        TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; //???????:TIM????????1
        TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //??????
        TIM_OCInitStructure.TIM_Pulse = 0;                            //????????????????
        TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;     //????:TIM???????
        TIM_OC1Init(TIM8, &TIM_OCInitStructure);  //??TIM_OCInitStruct???????????TIMx
        TIM_OC2Init(TIM8, &TIM_OCInitStructure);  //??TIM_OCInitStruct???????????TIMx
        TIM_OC3Init(TIM8, &TIM_OCInitStructure);  //??TIM_OCInitStruct???????????TIMx
        TIM_OC4Init(TIM8, &TIM_OCInitStructure);  //??TIM_OCInitStruct???????????TIMx
      
        TIM_CtrlPWMOutputs(TIM8,ENABLE);        //MOE ?????      
      
        TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);  //CH1?????         
        TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable);  //CH2?????         
        TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable);  //CH3?????         
        TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable);  //CH3?????        
      
      
        TIM_ARRPreloadConfig(TIM8, ENABLE); //??TIMx?ARR????????
        TIM_Cmd(TIM8, ENABLE);  //??TIM1
   
    // ËĸöͨµÀÈ«²¿Ê¹ÄÜ  CH1--PA8  CH2--PA9  CH3--PA10   CH4--PA11  
    //  ÍêÈ«ÖØÓ³Éäºó     CH1--PE9  CH2--PE11  CH3--PE13    CH4--PE14   

}



友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
15条回答
牧马人人
1楼-- · 2019-07-21 21:54
本帖最后由 牧马人人 于 2019-4-10 20:12 编辑
peng1554 发表于 2019-4-10 16:59
先尝试工程同时2个定时器输出PWM没问题,定时器之间PWM输出不互相影响的

定时器3,4能够同时输出,定时器2的ch3,ch4通道也能同时输出PWM。
定时器5还是不能输出。定时器1,定时器8和通用定时器不能同时输出。
牧马人人
2楼-- · 2019-07-22 01:55
原程序工程文件@peng1554 @ nashui_sx @翼行园子 @天涯海QxkJX     

ty110
3楼-- · 2019-07-22 05:24

谢谢分享,稍后实践下
天涯海QxkJX
4楼-- · 2019-07-22 06:44
 精彩回答 2  元偷偷看……
ty110
5楼-- · 2019-07-22 09:07

谢谢分享,稍后实践下
牧马人人
6楼-- · 2019-07-22 10:20
 精彩回答 2  元偷偷看……

一周热门 更多>