Skip to main content
yifan115
Associate II
July 22, 2014
Question

STM32F4 Discovery TIM1 PWM Generation problem

  • July 22, 2014
  • 7 replies
  • 2089 views
Posted on July 22, 2014 at 22:03

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();

}
    This topic has been closed for replies.

    7 replies

    Tesla DeLorean
    Guru
    July 22, 2014
    Posted on July 22, 2014 at 22:10

    You're on your own with the HAL stuff, but you might want to make sure you've associated the pins with the timer, and enabled the PWM output for TIM1

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    yifan115
    yifan115Author
    Associate II
    July 22, 2014
    Posted on July 22, 2014 at 22:18

    Actually I'm wondering why others are not using the HAL stuff, I'm using it because those HAL stuff are the only examples I find. So where can I find the commonly-used examples others are using? Thank you!

    Tesla DeLorean
    Guru
    July 22, 2014
    Posted on July 22, 2014 at 22:41

    Actually I'm wondering why others are not using the HAL stuff, I'm using it because those HAL stuff are the only examples I find. So where can I find the commonly-used examples others are using? Thank you!

    The firmware libraries are well tested, mature, and in heavy use. The HAL/CUBE stuff is immature and buggy, and when it's production ready and validated I might expend effort porting to it.

    Red Download button, bottom of the pages

    http://www.st.com/web/en/catalog/tools/PF257904

    http://www.st.com/web/en/catalog/tools/PF257901

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    yifan115
    yifan115Author
    Associate II
    July 22, 2014
    Posted on July 22, 2014 at 22:55

    Thank you! I'm just trying to use STM32F4 for some applications for which I have been using TI's 28335, and there are really a lot to learn :)

    Montassar BEN ROMDHANE_O
    Visitor II
    September 3, 2014
    Posted on September 03, 2014 at 18:40

    Hi yi.fan.001,

    After investigating you developped code based on STM32CubeF4 firmware, it turned out that yourGPIO Init structure for TIM1 output channel signals is missing the TIM1 alternate function configuration mode. That's why your project never come tofruition. Please find below the appropriateI/Os configuration:

    GPIO_InitStructure.Pin = GPIO_PIN_7;
    GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStructure.Alternate = GPIO_AF1_TIM1;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
    GPIO_InitStructure.Pin = GPIO_PIN_9;
    HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);

    We've tested your example with the suggested modification and it's well working. Hope that can help you.Keep us informed about your finding. Regards.
    raulhernandezfernandez
    Associate II
    November 17, 2015
    Posted on November 17, 2015 at 22:37

    Hi there,

    I have been trying to replicate the PWM script above in the stm32f429i disco board without any success. The program compiles with no errors and, in debugging mode, I check that the program reaches the infinite loop with no problems. However, I am not able to see the PWM signal from the PA7 pin. Could you help? please.

    Please find the full code below:

    int

    main(int argc, char* argv[])

    {

            GPIO_InitTypeDef GPIO_InitStructure;

            TIM_HandleTypeDef TimHandle;

            TIM_OC_InitTypeDef PWMConfig;

            TIM_BreakDeadTimeConfigTypeDef DeadConfig;

            __GPIOA_CLK_ENABLE();

            __GPIOE_CLK_ENABLE();

            GPIO_InitStructure.Pin = GPIO_PIN_7;

            GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;

            GPIO_InitStructure.Pull = GPIO_PULLUP;

            GPIO_InitStructure.Speed = GPIO_SPEED_HIGH;

            GPIO_InitStructure.Alternate = GPIO_AF1_TIM1;

            HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);

            GPIO_InitStructure.Pin = GPIO_PIN_9;

            HAL_GPIO_Init(GPIOE, &GPIO_InitStructure);

            __TIM1_CLK_ENABLE();

            uint16_t 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 = TIM_BREAKPOLARITY_LOW;

            DeadConfig.BreakState = TIM_BREAK_DISABLE;

            DeadConfig.DeadTime = 0xFF;

            DeadConfig.LockLevel = TIM_LOCKLEVEL_OFF;

            DeadConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;

            DeadConfig.OffStateRunMode = TIM_OSSR_DISABLE;

            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();

            }

           // Add your code here.

        while(1)

        {

            

        }

    }

    ftiti
    Visitor II
    November 19, 2015
    Posted on November 19, 2015 at 18:48

    Be careful for the systemClock configuration transition between two devices; STM32407 max frequency is 168MHz and STM32F429 is 180 MHz

    -> So you should review the prescaler and the period implemented according to TIMER counter clock. (not the same as the example for STM32F407)