AnsweredAssumed Answered

STM32F4 Discovery TIM1 PWM Generation problem

Question asked by yi.fan.001 on Jul 22, 2014
Latest reply on Nov 19, 2015 by tn.metalfun
Hi, I've been trying to generate complementary PWM output on TIM1 Channel 1, but there is no example of this for the STM32F4 Discovery, so I wrote the code based on several examples for other chips/boards, but the code doesn't work at all. The output pin is always low. I would really appreciate it if anyone can find the problem, thanks!

     __GPIOA_CLK_ENABLE();
     __GPIOE_CLK_ENABLE();
     
     GPIO_InitStructure.Pin = GPIO_PIN_7;
     GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
     GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
     HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);

     GPIO_InitStructure.Pin = GPIO_PIN_9;
     HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);
     
     
     __TIM1_CLK_ENABLE();
  
     uwPrescalerValue =0;
     TimHandle.Instance = TIM1;
     TimHandle.Init.Period = 1680 - 1;
      TimHandle.Init.Prescaler = uwPrescalerValue;
     TimHandle.Init.ClockDivision = 0;
     TimHandle.Init.CounterMode = TIM_COUNTERMODE_UP;
     
     if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)
  {
    Error_Handler();
  }
     
     PWMConfig.Pulse = 840;
     PWMConfig.OCMode = TIM_OCMODE_PWM1;
     PWMConfig.OCPolarity = TIM_OCPOLARITY_HIGH;
     PWMConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;
     PWMConfig.OCIdleState = TIM_OCIDLESTATE_SET;
     PWMConfig.OCNIdleState= TIM_OCNIDLESTATE_RESET;
     PWMConfig.OCFastMode = TIM_OCFAST_DISABLE;
     
     if(HAL_TIM_PWM_ConfigChannel(&TimHandle,&PWMConfig,TIM_CHANNEL_1) != HAL_OK)
     {
          Error_Handler();
     }
     
     DeadConfig.AutomaticOutput=TIM_AUTOMATICOUTPUT_ENABLE;
     DeadConfig.BreakPolarity=0;
     DeadConfig.BreakState=0;
     DeadConfig.DeadTime=100;
     DeadConfig.LockLevel=0;
     DeadConfig.OffStateIDLEMode=1;
     DeadConfig.OffStateRunMode=1;


     if(HAL_TIMEx_ConfigBreakDeadTime(&TimHandle,&DeadConfig) != HAL_OK)
     {
          Error_Handler();
     }
     
     if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
     if(HAL_TIMEx_PWMN_Start(&TimHandle,TIM_CHANNEL_1) != HAL_OK)
     {
          Error_Handler();
     }

Outcomes