cancel
Showing results for 
Search instead for 
Did you mean: 

TIM1 PWM output on pin (cube HAL)

laski
Associate II
Posted on December 09, 2015 at 11:50

Hello,

I have configured TIM1 as PWM source and enabled it. Its working becouse TIM1->CNT is incrementing and I have blinking LED based on value of TIM1->CNT. But now I need output it on PIN and its not workig. My configuration (generated mostly by CubeMX):

[code]TIM_HandleTypeDef htim1;

/* TIM1 init function */

void MX_TIM1_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct;

/* USER CODE BEGIN TIM1_MspInit 0 */

/* USER CODE END TIM1_MspInit 0 */

/* Peripheral clock enable */

__TIM1_CLK_ENABLE();

/**TIM1 GPIO Configuration

PE12     ------> TIM1_CH3N

PE13     ------> TIM1_CH3

*/

GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;

GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_LOW;

GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;

HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

  TIM_MasterConfigTypeDef sMasterConfig;

  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig;

  TIM_OC_InitTypeDef sConfigOC;

  __HAL_RCC_TIM1_CLK_ENABLE();

  htim1.Instance = TIM1;

  htim1.Init.Prescaler = 1000;

  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;

  htim1.Init.Period = 8399;

  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

  htim1.Init.RepetitionCounter = 0;

  HAL_TIM_OC_Init(&htim1);

//  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

//  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

//  HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);

//

//  sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;

//  sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;

//  sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;

//  sBreakDeadTimeConfig.DeadTime = 0;

//  sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;

//  sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;

//  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;

//  HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig);

  sConfigOC.OCMode = TIM_OCMODE_PWM2;

  sConfigOC.Pulse = 4000;

  sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;

  sConfigOC.OCNPolarity = TIM_OCNPOLARITY_LOW;

  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;

  sConfigOC.OCIdleState = TIM_OCIDLESTATE_SET;

  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_SET;

  HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3);

}

void HAL_TIM_OC_MspInit(TIM_HandleTypeDef* htim_oc)

{

  GPIO_InitTypeDef GPIO_InitStruct;

  if(htim_oc->Instance==TIM1)

  {

  /* USER CODE BEGIN TIM1_MspInit 0 */

  /* USER CODE END TIM1_MspInit 0 */

    /* Peripheral clock enable */

    __TIM1_CLK_ENABLE();

  

    /**TIM1 GPIO Configuration    

    PE12     ------> TIM1_CH3N

    PE13     ------> TIM1_CH3 

    */

    GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;

    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH ;

    GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;

    HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);

  /* USER CODE BEGIN TIM1_MspInit 1 */

  /* USER CODE END TIM1_MspInit 1 */

  }

}[/code]

and main function:

[code]int main(void)

{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

  HAL_Init();

  /* Configure the system clock */

  SystemClock_Config();

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_TIM1_Init();

  HAL_TIM_OC_MspInit(&htim1);

  __HAL_RCC_GPIOE_CLK_ENABLE();

  __HAL_TIM_ENABLE(&htim1);

  /* USER CODE BEGIN 2 */

  /* USER CODE END 2 */

  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1)

  {

  /* USER CODE END WHILE */

 if(TIM1->CNT > 4000)

 {

 HAL_GPIO_WritePin(GPIOE, LEDG_Pin, 0);

 }

 else

 {

 HAL_GPIO_WritePin(GPIOE, LEDG_Pin, 1);

 }

  /* USER CODE BEGIN 3 */

  }

  /* USER CODE END 3 */

}[code]

What am I missing?

Thanks for any help.

#timer-pwm-gpio-cube
8 REPLIES 8
Posted on December 09, 2015 at 12:26

I don't speak the Cube gobbledygook, but the common gotcha with Advanced Timers (TIM1, TIM8, TIM15+) is that MOE in TIMx_BDTR needs to be set.

JW
laski
Associate II
Posted on December 09, 2015 at 13:10

Thank you for interest.

setting this bit havn't help. I have uncomented:

[code]  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

  HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig);

  sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;

  sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;

  sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;

  sBreakDeadTimeConfig.DeadTime = 0;

  sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;

  sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;

  sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_ENABLE;

  HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig);[code]

and set  sBreakDeadTimeConfig.AutomaticOutput to enable so AOE bit is set and MOE is auto setting. Value of BDTR register is 0xE000 so MOE bit is set.

Still no output on pin:/

Currently I'm checking register by register what is set as Timer configuration;

Thanks (Dziękuję)

laski
Associate II
Posted on December 09, 2015 at 16:15

I think I'm missing something more basic. I have initialized TIMER4 with same configuration and I don't have output there also.

What I have:

- Timer configuration (working because counting)

- Timer clock enabled: __TIM1_CLK_ENABLE();

- Timer enabled: __HAL_TIM_ENABLE(&htim1);

- GPIO clock enabled: __GPIOE_CLK_ENABLE();

- GPIO configured with AF: GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;

- System clock is generated from HSI

What else should I enable to make it working?

It's my first contact with st uC so I'm little confused

Amel NASRI
ST Employee
Posted on December 09, 2015 at 17:07

Hi bamboosso,

To have the PWM output, you should configure your timer channel in ''PWM generation CH1'' mode.

Then you will have the function ''HAL_TIM_PWM_ConfigChannel'' called in the generated code.

To start the PWM output, you have to use the function ''HAL_TIM_PWM_Start'' in your main.

I recommend you have a look to the example ''TIM_PWMOutput'' that you may find under ''STM32Cube_FW_F4_V1.9.0\Projects\STM324xG_EVAL\Examples\TIM\TIM_PWMOutput'' if you are using STM32F4 parts (you should find similar example for other packages).

-Mayla-

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

rbenesh
Associate II
Posted on December 09, 2015 at 22:11

Once the Timer is configured can the output be directed to any pin? I have tried CubeMX and find that if I enable all the intended peripherals and GPIO's I cannot direct PWM to the desired pin.

Posted on December 10, 2015 at 00:55

bamboosso,

How do you observe the outcome? What is your hardware? Can you blinkey, i.e. produce pulses by putting the same pin as GP output and toggle it in a loop (together with some loop-delay, perhaps)?

Post the timer's registers' content, and also the relevant GPIO registers'.

(It's a risky business to try to guess one's nationality from his/her name - I understand Polish of course but I am from Bratislava, Slovakia, and my mother tongue is Hungarian, actually... welcome to Central Europe ;) )

NStuff,

please don't hijack threads - start your own, stating clearly and precisely what is your problem.

JW

Posted on December 10, 2015 at 01:25

Once the Timer is configured can the output be directed to any pin?

Well there a limited subset of choices. The Data Manual for the part has pin/function listings that associate TIMx_CHx functionality.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
laski
Associate II
Posted on December 10, 2015 at 09:09

Thank you all for help!

HAL_TIM_PWM_Start this is what I was missing!!

Thanks again!

bamboosso