Skip to main content
MKlei.1
Associate
Sep 2, 2020 1:09 PM UTC
Solved

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

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.
MKlei.1
MKlei.1Best answer
Associate
Sep 2, 2020 3:12 PM UTC

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
Sep 2, 2020 3:12 PM UTC

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
Sep 2, 2020 3:19 PM UTC

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
Sep 2, 2020 3:23 PM UTC

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
Sep 2, 2020 3:27 PM UTC

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.