野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 14001|回复: 3

stm32 timer3与timer8 PWM输出设置冲突

[复制链接]
发表于 2015-3-26 17:43:05 | 显示全部楼层 |阅读模式
如题,本人目前需要配置stm32的timer3与timer8输出PWM,现在单独配置timer8时候有输出,但是加入timer3后,timer8就没有输出了。这时候timer3是有输出的。比较困惑。timer3用的pb0和pb1,timer8用的是PC6-9,查看手册这几个timer都是独立的,没有交集。请高手指点。谢谢!

附上代码:
/********************************TIMER 3**********************************/

static void TIM3_GPIO_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;

        /* éèÖÃTIM3CLK Îa 72MHZ */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  /* GPIOA and GPIOB clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);

  /*GPIOA Configuration: TIM3 channel 1 to 4 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0 | GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;                    // ¸′óÃíÆíìêä3ö
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOB, &GPIO_InitStructure);

}

static void TIM3_Mode_Config(void)
{
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;

        /* PWMDÅoÅμçƽìø±äÖμ */
        u16 CCR3_Val = 250;
        u16 CCR4_Val = 125;

  /* Time base configuration */                 
  TIM_TimeBaseStructure.TIM_Period = 999;       //챶¨ê±Æ÷′ó0¼Æêyμ½999£¬¼′Îa1000′Σ¬Îaò»¸ö¶¨ê±ÖüÆú
  TIM_TimeBaseStructure.TIM_Prescaler = 0;            //éèÖÃÔ¤·ÖÆμ£o2»Ô¤·ÖÆ죬¼′Îa72MHz
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;        //éèÖÃê±Öó·ÖÆμÏμêy£o2»·ÖÆμ(ÕaàïóÃ2»μ½)
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //ÏòéϼÆêyÄ£ê½
  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;            //ÅäÖÃÎaPWMÄ£ê½1
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;       
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  //챶¨ê±Æ÷¼ÆêyÖμD¡óúCCR1_Valê±Îa¸ßμçƽ
  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;        //éèÖÃí¨μà3μÄμçƽìø±äÖ죬êä3öáííaò»¸öÕ¼¿Õ±èμ&#196WM
  TIM_OC3Init(TIM3, &TIM_OCInitStructure);         //ê1Äüí¨μà3
  TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);

  /* PWM1 Mode configuration: Channel4 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;        //éèÖÃí¨μà4μÄμçƽìø±äÖ죬êä3öáííaò»¸öÕ¼¿Õ±èμ&#196WM
  TIM_OC4Init(TIM3, &TIM_OCInitStructure);        //ê1Äüí¨μà4
  TIM_OC4PreloadConfig(TIM3, TIM_OCPreload_Enable);
  TIM_ARRPreloadConfig(TIM3, ENABLE);                         // ê1ÄüTIM3ÖØÔؼÄ′æÆ÷ARR

  /* TIM3 enable counter */
  TIM_Cmd(TIM3, ENABLE);                   //ê1Äü¶¨ê±Æ÷3       
}

void TIM3_PWM_Init(void)
{
        TIM3_GPIO_Config();
        TIM3_Mode_Config();       
}

/********************************TIMER 8**********************************/

static void TIM8_GPIO_Config(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
       
  /* GPIOA and GPIOB clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_TIM8, ENABLE);

  /*GPIOA Configuration: TIM8 channel 1 to 4 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;                    // ¸′óÃíÆíìêä3ö
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  GPIO_Init(GPIOC, &GPIO_InitStructure);
}

static void TIM8_Mode_Config(void)
{
        TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
        TIM_OCInitTypeDef  TIM_OCInitStructure;

        /* PWMDÅoÅμçƽìø±äÖμ */
        u16 CCR1_Val = 2;        
        u16 CCR2_Val = 4;
        u16 CCR3_Val = 6;
        u16 CCR4_Val = 8;

  /* Time base configuration */                 
  TIM_TimeBaseStructure.TIM_Period = 9;       //챶¨ê±Æ÷′ó0¼Æêyμ½999£¬¼′Îa1000′Σ¬Îaò»¸ö¶¨ê±ÖüÆú
  TIM_TimeBaseStructure.TIM_Prescaler = 72;            //éèÖÃÔ¤·ÖÆμ£o2»Ô¤·ÖÆ죬¼′Îa72MHz
  TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ;        //éèÖÃê±Öó·ÖÆμÏμêy£o2»·ÖÆμ(ÕaàïóÃ2»μ½)
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //ÏòéϼÆêyÄ£ê½
  TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);

  /* PWM1 Mode configuration: Channel1 */
  TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;            //ÅäÖÃÎaPWMÄ£ê½1
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;       
  TIM_OCInitStructure.TIM_Pulse = CCR1_Val;           //éèÖÃìø±äÖμ£¬μ±¼ÆêyÆ÷¼Æêyμ½Õa¸öÖμê±£¬μçƽ·¢éúìø±ä
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;  //챶¨ê±Æ÷¼ÆêyÖμD¡óúCCR1_Valê±Îa¸ßμçƽ
  TIM_OC1Init(TIM8, &TIM_OCInitStructure);         //ê1Äüí¨μà1
  TIM_OC1PreloadConfig(TIM8, TIM_OCPreload_Enable);

  /* PWM1 Mode configuration: Channel2 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR2_Val;          //éèÖÃí¨μà2μÄμçƽìø±äÖ죬êä3öáííaò»¸öÕ¼¿Õ±èμ&#196WM
  TIM_OC2Init(TIM8, &TIM_OCInitStructure);          //ê1Äüí¨μà2
  TIM_OC2PreloadConfig(TIM8, TIM_OCPreload_Enable);

  /* PWM1 Mode configuration: Channel3 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR3_Val;        //éèÖÃí¨μà3μÄμçƽìø±äÖ죬êä3öáííaò»¸öÕ¼¿Õ±èμ&#196WM
  TIM_OC3Init(TIM8, &TIM_OCInitStructure);         //ê1Äüí¨μà3
  TIM_OC3PreloadConfig(TIM8, TIM_OCPreload_Enable);

  /* PWM1 Mode configuration: Channel4 */
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = CCR4_Val;        //éèÖÃí¨μà4μÄμçƽìø±äÖ죬êä3öáííaò»¸öÕ¼¿Õ±èμ&#196WM
  TIM_OC4Init(TIM8, &TIM_OCInitStructure);        //ê1Äüí¨μà4
  TIM_OC4PreloadConfig(TIM8, TIM_OCPreload_Enable);
  TIM_ARRPreloadConfig(TIM8, ENABLE);                         // ê1ÄüTIM8ÖØÔؼÄ′æÆ÷ARR

  /* TIM8 enable counter */
  TIM_Cmd(TIM8, ENABLE);                   //ê1Äü¶¨ê±Æ÷8       
        TIM_CtrlPWMOutputs(TIM8, ENABLE);
}

void TIM8_PWM_Init(void)
{
        TIM8_GPIO_Config();
        TIM8_Mode_Config();       
}

回复

使用道具 举报

发表于 2015-3-27 09:09:15 | 显示全部楼层
看看板子是不是其它地方用到了这些引脚
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-3-31 09:23:34 | 显示全部楼层
现在好了,谢谢!
回复 支持 反对

使用道具 举报

发表于 2015-3-31 09:50:50 | 显示全部楼层

是什么问题
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-11-28 21:33 , Processed in 0.039446 second(s), 24 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表