Skip to main content
MKlei.1
Associate
September 2, 2020
Solved

G4 MCU in OnePulseMode on TIM3, how to set default (idle) value to low

  • September 2, 2020
  • 4 replies
  • 1544 views

I have successfully set up this timer to output one high pulse with following low state.

However after finishing the period the output toggles to high until next activation.

I need it to be low inbetween activations. How can this be achieved?

I tried all combinations of PWM1 and PWM2 with high and low polarity.

Delay->Pulse is no problem but Pulse->Pause always sets the Idle value wrong.

This topic has been closed for replies.
Best answer by MKlei.1

Found one solution myself, please keep me posted if there is a more elegant way:

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
	TIM3->CCER &= ~TIM_CCER_CC2P;
	TIM3->CR1 |= 0x1;
}
 
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if (htim->Instance == TIM3) {
		TIM3->CCER |= TIM_CCER_CC2P;
	}
}

I do capture on channel 4 therefore I cannot trigger directly and have to go by interrupts.

Line 4 starts the pulse.

4 replies

MKlei.1
MKlei.1AuthorBest answer
Associate
September 2, 2020

Found one solution myself, please keep me posted if there is a more elegant way:

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
	TIM3->CCER &= ~TIM_CCER_CC2P;
	TIM3->CR1 |= 0x1;
}
 
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
	if (htim->Instance == TIM3) {
		TIM3->CCER |= TIM_CCER_CC2P;
	}
}

I do capture on channel 4 therefore I cannot trigger directly and have to go by interrupts.

Line 4 starts the pulse.

waclawek.jan
Super User
September 2, 2020

One way might be to use some of the combined modes, to output high only in the CNT range of 1..CCRx instead of 0..CCRx, if you can tolerate the 1-clock delay.

There might be other solutions; it depends on the particularities of your application (e.g. how exactly do you start the pulse).

JW

MKlei.1
MKlei.1Author
Associate
September 2, 2020

I do an edge triggered capture on TIM3_CH4 and start the pulse via timer enable on IC_CaptureCallback as seen above.

1 cycle surely poses no problem (at 168 Mhz ;) ) so I will look into combined modes tomorrow, thank you for your answer!

MKlei.1
MKlei.1Author
Associate
September 2, 2020

Ah, I have to add.

The Pause after the Pulse is needed. During that time no new pulse can be allowed, regardless of new trigger pulses on channel 4.

This is why I went for OnePulse + PWM in the first place.