Skip to main content
qzha
Associate
Sep 4, 2018 8:43 AM UTC
Question

No PWM output using TIM1_CH3 on STM32F769 with Keil5.25

Hi i want to enable pwm output on PA10, as in below code, will do necessary init by MX_TIM1_Init, and then call HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_3); to start.

but PA10 is kept low measured by Osi.

could you help review and any suggestion ? thanks.

void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim);

/* TIM1 init function */

void MX_TIM1_Init(void)

{

 TIM_MasterConfigTypeDef sMasterConfig;

 TIM_OC_InitTypeDef sConfigOC;

 htim1.Instance = TIM1;

 htim1.Init.Prescaler = 1080-1;

 htim1.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim1.Init.Period = 2000-1;

 htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 HAL_TIM_PWM_Init(&htim1);

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

 HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);

 sConfigOC.OCMode = TIM_OCMODE_PWM1;

 sConfigOC.Pulse = 0;

 sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;

 sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

 HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3);

 HAL_TIM_MspPostInit(&htim1);

}

void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_pwm)

{

 if(htim_pwm->Instance==TIM1)

 {

 /* USER CODE BEGIN TIM4_MspInit 0 */

 /* USER CODE END TIM4_MspInit 0 */

  /* Peripheral clock enable */

  __HAL_RCC_TIM1_CLK_ENABLE();

 /* USER CODE BEGIN TIM4_MspInit 1 */

 /* USER CODE END TIM4_MspInit 1 */

 }

}

void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)

{

 GPIO_InitTypeDef GPIO_InitStruct;

 if(htim->Instance==TIM1)

 {

 /* USER CODE BEGIN TIM4_MspPostInit 0 */

 /* USER CODE END TIM4_MspPostInit 0 */

  

  /**TIM4 GPIO Configuration   

  PB6   ------> TIM4_CH1 

  */

  GPIO_InitStruct.Pin = GPIO_PIN_10;

  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

  GPIO_InitStruct.Pull = GPIO_NOPULL;

  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;

  GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;

  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

 /* USER CODE BEGIN TIM4_MspPostInit 1 */

 /* USER CODE END TIM4_MspPostInit 1 */

 }

}

GPIO clock have been enabled before timer init: __HAL_RCC_GPIOA_CLK_ENABLE();

    This topic has been closed for replies.

    6 replies

    waclawek.jan
    Super User
    Sep 4, 2018 2:35 PM UTC

    Read back and check/post the relevant GPIO and TIM registers' content.

    JW

    qzha
    qzhaAuthor
    Associate
    Sep 4, 2018 3:23 PM UTC

    Hi JW

    thanks your feedback, are you confirming there seems no issue in these code i referenced from some demo project ?

    especially the gpio setting for PA10, GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;

    waclawek.jan
    Super User
    Sep 4, 2018 3:46 PM UTC

    > are you confirming there seems no issue in these code

    No.

    I don't Cube and won't review Cube-related code.

    I just recommend you to read out the registers settings and check against the expected ones. The chip does not understand Cube either, it works out of the registers.

    JW

    PS. A quick glance at the code with no attempt to understand:  sConfigOC.Pulse = 0; is suspicious, PWM with pulse length 0 is exactly what you observe, isn't it?

    qzha
    qzhaAuthor
    Associate
    Sep 5, 2018 1:45 AM UTC

    thx.

    any expert can see any missing or wrong config here ?

    Tesla DeLorean
    Guru
    Sep 5, 2018 2:35 AM UTC

    Make sure you clear the initialization structures, or max sure ALL fields are initialized.

    TIM1/TIM8 are Advanced Timers, there is a Repetition Count, and the PWM Input/Output must be explicitly Enabled.

       /* Enable the main output */

       __HAL_TIM_MOE_ENABLE(htim);

    For TIM1 PWM initialization is going to look more like this

     /* Select the Timer instance */

     TimHandle.Instance = TIM1;

     TimHandle.Init.Prescaler        = 0;

     TimHandle.Init.Period           = uwPeriodValue;

     TimHandle.Init.ClockDivision    = 0;

     TimHandle.Init.CounterMode      = TIM_COUNTERMODE_UP;

     TimHandle.Init.RepetitionCounter = 0;

     TimHandle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

     if(HAL_TIM_PWM_Init(&TimHandle) != HAL_OK)

     {

       /* Initialization Error */

       Error_Handler();

     }

     /*##-2- Configure the PWM channels #########################################*/

     /* Common configuration for all channels */

     sPWMConfig.OCMode     = TIM_OCMODE_PWM1;

     sPWMConfig.OCFastMode = TIM_OCFAST_DISABLE;

     sPWMConfig.OCPolarity = TIM_OCPOLARITY_LOW;

     sPWMConfig.OCNPolarity = TIM_OCNPOLARITY_HIGH;

     sPWMConfig.OCIdleState = TIM_OCIDLESTATE_SET;

     sPWMConfig.OCNIdleState= TIM_OCNIDLESTATE_RESET;

     /* Set the pulse value for channel 3 */

     sPWMConfig.Pulse = uwPulse3;

     if(HAL_TIM_PWM_ConfigChannel(&TimHandle, &sPWMConfig, TIM_CHANNEL_3) != HAL_OK)

     {

       /* Configuration Error */

       Error_Handler();

     }

     /* Start channel 3 */

     if(HAL_TIM_PWM_Start(&TimHandle, TIM_CHANNEL_3) != HAL_OK)

     {

       /* Starting Error */

       Error_Handler();

     }

    Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
    waclawek.jan
    Super User
    Sep 5, 2018 8:08 AM UTC

    >> PS. A quick glance at the code with no attempt to understand: sConfigOC.Pulse = 0; is suspicious, PWM with pulse length 0 is exactly what you observe, isn't it?

    > any expert can see any missing or wrong config here ?

    Did you try to change that pulse length?

    Have you already read out and checked the registers' content?

    JW