PWM输出在STM32F10x OPEN107V开发板上不起作用

时间:2014-04-06 20:13:55

标签: c microcontroller stm32

你好我有#34; stm32f10x open107V开发板" ,我已经修改了制造商给出的PWM代码,但我没有在开发板上给出任何PWM输出,请为以下代码提供帮助。 端口B(GPIOB)上的GPIO_pins 0,1,1,1,15是开发板上给出的Led引脚。代码没有错误,并且在链接时没有错误。由于我是初学者,我不明白这是什么问题。

   /**
    -----------------------------------------------------------------*/
          #include "stm32f10x_gpio.h"
          #include "stm32f10x_rcc.h"
          #include "stm32f10x_tim.h"



     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
     TIM_OCInitTypeDef  TIM_OCInitStructure;
     uint16_t CCR1_Val = 333;
     uint16_t CCR2_Val = 249;
     uint16_t CCR3_Val = 166;
     uint16_t CCR4_Val = 83;
     uint16_t PrescalerValue ;

       /* Private function prototypes -----------------------------------------------*/
    void RCC_Configuration(void);
    void GPIO_Configuration(void);

      /* Private functions ---------------------------------------------------------*/

       /**

     int main(void)
     {

     RCC_Configuration();

    /*GPIO Configuration */
     GPIO_Configuration();


    PrescalerValue =(72000000 / 24000000) - 1;
    /* Time base configuration */
    TIM_TimeBaseStructure.TIM_Period = 665;
    TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
    TIM_TimeBaseStructure.TIM_ClockDivision = 4;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

    /* PWM1 Mode configuration: Channel1 */
    TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
    TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;

    TIM_OC1Init(TIM3, &TIM_OCInitStructure);

    TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);

    /* PWM1 Mode configuration: Channel2 */
    TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
    TIM_OCInitStructure.TIM_Pulse = CCR2_Val;

    TIM_OC2Init(TIM3, &TIM_OCInitStructure);

     TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);

     /* PWM1 Mode configuration: Channel3 */
     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
     TIM_OCInitStructure.TIM_Pulse = CCR3_Val;

     TIM_OC3Init(TIM3, &TIM_OCInitStructure);

      TIM_OC3PreloadConfig(TIM3, TIM_OCPreload_Enable);


      /* TIM3 enable counter */
     TIM_Cmd(TIM3, ENABLE);

   while (1)
    {}
   }


   void RCC_Configuration(void)
   {
        /* TIM3 clock enable */
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

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


    void GPIO_Configuration(void)
    {
     GPIO_InitTypeDef GPIO_InitStructure;
      /*GPIOB Configuration: TIM3 channel1, 2, 3 and 4 */
     GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_14|GPIO_Pin_15 ;
     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

     GPIO_Init(GPIOB, &GPIO_InitStructure);

     GPIO_PinRemapConfig(GPIO_FullRemap_TIM3, ENABLE);  

     }

1 个答案:

答案 0 :(得分:1)

首先,如果您查看STM32F107数据表(这是我认为您拥有的MCU),PB0和PB1引脚可以映射到TIM3_CH3和TIM3_CH4,但PB14和PB15不能映射到任何TIM3通道 - 仅限TIM1通道。有关我的数据表版本,请参见第3节表5。

其次,在调用GPIO_PinRemapConfig()时,您正在使用AFIO重映射功能。确实连接到LED(PB0和PB1)的两个引脚不需要重映射,而另外两个(PB14和PB15)无论你做什么都无法重新映射到TIM3通道。

具体而言,只需取消对GPIO_PinRemapConfig()的调用,就可以在两个引脚(PB0和PB1)上获得PWM输出,而代码更改最少。对于PB14和PB15,您需要配置需要大量额外代码的TIM1。不幸的是,您无法将这些引脚映射到TIM3。