cancel
Showing results for 
Search instead for 
Did you mean: 

TIM Output compare on STM32F407

MG1
Associate II

Good morning community,
I am trying to capture output compare unit.
Board: STM32F407G Discovery.
I configure TIM1 which is 16bit timer on bus1.
Reference clock is 12.5Mhz in our case.
I am seeing when I run the code only one time interrupt is generated and after that it will not toggle the LED Gpio.
So, my concern is that it needs to toggle every time when interrupt is generated by comparing that.
The code is here,
In main function code is here:

 

 

 

 

while (1)
  {
    /* USER CODE END WHILE */

		/* USER CODE BEGIN 3 */
		  if(HAL_TIM_OC_Start(&htim1, TIM_CHANNEL_1) == HAL_OK)
		  {
			  HAL_GPIO_TogglePin(LD3_GPIO_Port, LD3_Pin);
			  HAL_Delay(1000);
		  }
		  if(HAL_TIM_OC_Start(&htim1, TIM_CHANNEL_2) == HAL_OK)
			  {
				  HAL_GPIO_TogglePin(LD4_GPIO_Port, LD4_Pin);
				  HAL_Delay(2000);
			  }
		  if(HAL_TIM_OC_Start(&htim1, TIM_CHANNEL_3) == HAL_OK)
			  {
				  HAL_GPIO_TogglePin(LD5_GPIO_Port, LD5_Pin);
				  HAL_Delay(3000);
			  }
		  if(HAL_TIM_OC_Start(&htim1, TIM_CHANNEL_4) == HAL_OK)
			  {
				  HAL_GPIO_TogglePin(LD6_GPIO_Port, LD6_Pin);
				  HAL_Delay(4000);
			  }
  }

 

 

 

 

And timer configuration is as below:

 

 

 

 

static void MX_TIM1_Init(void)
{

  /* USER CODE BEGIN TIM1_Init 0 */

  /* USER CODE END TIM1_Init 0 */

  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};
  TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};

  /* USER CODE BEGIN TIM1_Init 1 */

  /* USER CODE END TIM1_Init 1 */
  htim1.Instance = TIM1;
  htim1.Init.Prescaler = 0;
  htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim1.Init.Period = 1000;
  htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim1.Init.RepetitionCounter = 0;
  htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_OC_Init(&htim1) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
  sConfigOC.Pulse = 1000;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
  sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.Pulse = 1500;
  if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.Pulse = 2000;
  if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.Pulse = 2500;
  if (HAL_TIM_OC_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
  {
    Error_Handler();
  }
  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;
  if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM1_Init 2 */

  /* USER CODE END TIM1_Init 2 */
  HAL_TIM_MspPostInit(&htim1);

}

 

 

 

 


I am configuring four channels of timer.
Those will generate different frequency.
TIM_CH1: 12.5Khz.
TIM_CH2: 8.33Khz.
TIM_CH3: 6.250Khz.
TIM_CH4: 5.000Khz.
So, TIM_CH1 is working properly, but other channels are not working as per desire.
So, please suggest me configuration of four channel with different frequency is possible or not?
And is there any need of add any callback function or not?
And for vary with duty cycle which configuration need to change?
Please suggest me,
Thank you.

3 REPLIES 3

> configuration of four channel with different frequency is possible or not?

Not.

There's only one single counter, so all PWM channels have the same frequency.

JW

TDK
Guru

> htim1.Init.Period = 1000;

> sConfigOC.Pulse = 1500;

Pulse cannot be greater than period. The counter counts up to the Period value, then resets to 0. So it will never get to a pulse value above there to trigger anything.

If you feel a post has answered your question, please click "Accept as Solution".
Sarra.S
ST Employee

Hello @MG1

>> And for vary with duty cycle which configuration need to change?

I suppose what you tried to do with the different channel frequencies is to change duty cycles, is that it? 

Please refer to this example in STM32F3CubeFW which generates four different signals at four different frequencies.

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.