Skip to main content
jgauthier
Senior
December 10, 2021
Question

One shot pulse on different channels - same timer

  • December 10, 2021
  • 2 replies
  • 855 views

I am trying to get both PWM CH1 and PWM CH2 to fire independently with TIM15.

However, both are enabled at the same time.

Using CubeMX to generate initialization function:

void MX_TIM15_Init(void)
{
 
 /* USER CODE BEGIN TIM15_Init 0 */
 
 /* USER CODE END TIM15_Init 0 */
 
 TIM_MasterConfigTypeDef sMasterConfig = {0};
 TIM_OC_InitTypeDef sConfigOC = {0};
 TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
 
 /* USER CODE BEGIN TIM15_Init 1 */
 
 /* USER CODE END TIM15_Init 1 */
 htim15.Instance = TIM15;
 htim15.Init.Prescaler = 3200-1;
 htim15.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim15.Init.Period = 1500-1;
 htim15.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htim15.Init.RepetitionCounter = 0;
 htim15.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
 if (HAL_TIM_PWM_Init(&htim15) != HAL_OK)
 {
 Error_Handler();
 }
 if (HAL_TIM_OnePulse_Init(&htim15, TIM_OPMODE_SINGLE) != HAL_OK)
 {
 Error_Handler();
 }
 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
 if (HAL_TIMEx_MasterConfigSynchronization(&htim15, &sMasterConfig) != HAL_OK)
 {
 Error_Handler();
 }
 sConfigOC.OCMode = TIM_OCMODE_PWM2;
 sConfigOC.Pulse = 1;
 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_PWM_ConfigChannel(&htim15, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
 {
 Error_Handler();
 }
 __HAL_TIM_DISABLE_OCxPRELOAD(&htim15, TIM_CHANNEL_1);
 if (HAL_TIM_PWM_ConfigChannel(&htim15, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
 {
 Error_Handler();
 }
 __HAL_TIM_DISABLE_OCxPRELOAD(&htim15, TIM_CHANNEL_2);
 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(&htim15, &sBreakDeadTimeConfig) != HAL_OK)
 {
 Error_Handler();
 }
 /* USER CODE BEGIN TIM15_Init 2 */
 
 /* USER CODE END TIM15_Init 2 */
 HAL_TIM_MspPostInit(&htim15);
 
}

Then just doing this to start and test things:

	// OutputChannel isn't even used in the code //
	if (HAL_TIM_OnePulse_Start(&htim15, TIM_CHANNEL_1) != HAL_OK) {
		serprintf("Issue with interrupt on TIM51 channel");
		Error_Handler();
	}

Finally, enablement of the timer:

This actually enables both CH1 and CH2 at the same time.

TIM15->CR1 = TIM_CR1_CEN|TIM_CR1_OPM;

Can I accomplish this with both channels independently?

This topic has been closed for replies.

2 replies

TDK
December 10, 2021

There is only one timer (CNT register) per timer.

If you want independent pulses, you will need independent timers.

"If you feel a post has answered your question, please click ""Accept as Solution""."
waclawek.jan
Super User
December 10, 2021

There is only one counter in the timer, and the channels are comparators, generating output by comparing value in CCRx register against that one single counter

Read the TIM chapter in RM.

JW