Skip to main content
SGold.3
Associate II
May 4, 2021
Question

How to set my timers to 0V when off?

I am having this problem with a variety of signals but let's take for example a 10 kHz signal. I am using the repetition counter to count 9500 times (950 ms) and then be off for 50 ms before turning back on. During the repetition counter part my signal works great but then my signal goes to 3.3V for the 50 ms I want it to be off. Is there a setting somewhere to ensure my signal is at 0 during this off time? For timing reasons, I do not want to stop and start my signal.

This topic has been closed for replies.

9 replies

waclawek.jan
Super User
May 4, 2021

There are several ways to achieve this, including in PWM mode setting CCRx to 0 or maximum depending on the particular PWM mode and output polarity; using some of the Forced output compare mode; or simply switching the given pin to GPIO Output for the required time.

JW

SGold.3
SGold.3Author
Associate II
May 4, 2021

How do you set CCR for a given timer? I know that ARR is Counter Period, PSC is Prescaler, and RCR is Repetition Counter but I am not sure how to control CCR.

Thanks,

Sam

TDK
May 4, 2021

If you just want to invert the polarity of what you're currently getting, just change the channel polarity.

To modify CCR for timer 1 channel 2:

TIM1->CCR2 = 0;

"If you feel a post has answered your question, please click ""Accept as Solution""."
SGold.3
SGold.3Author
Associate II
May 4, 2021

So can I do something like:

if (TIM1->RCR1=9499)

{

TIM1->CCR2 = 0;

}

So it sets the signal to 0 when it hits the limit of the repetition counter?

Thanks

SGold.3
SGold.3Author
Associate II
May 4, 2021

Where in the code would I input this? I tried to put it in the Timer configuration and it shut the whole signal off but then when I put it after the start code below, nothing happened

 if (HAL_TIM_PWM_Start_IT(&htim1, TIM_CHANNEL_1) != HAL_OK)

 {

 Error_Handler();

 }

nothi

waclawek.jan
Super User
May 5, 2021

Methods I suggested are intended to be used runtime, when the pulsetrain stops, e.g. employing interrupts.

If you want to stay with Cube, do what TDK suggested above and invert the output polarity.

JW

TDK
May 5, 2021

If you want to stick with HAL methods, I would start both timers, then set the master counter's CNT register to the appropriate value right after they are started.

"If you feel a post has answered your question, please click ""Accept as Solution""."
SGold.3
SGold.3Author
Associate II
May 5, 2021

if (HAL_TIM_PWM_Start_IT(&htim1, TIM_CHANNEL_1) != HAL_OK)

{

Error_Handler();

}

if (HAL_TIM_PWM_Start(&htim1, TIM_CHANNEL_2) != HAL_OK)

{

Error_Handler();

}

if (HAL_TIM_PWM_Start(&htim8, TIM_CHANNEL_1) != HAL_OK)

{

Error_Handler();

}

if (HAL_TIM_Base_Start_IT(&htim15) != HAL_OK)

{

Error_Handler();

}

while (TIM15->CNT == 9500)

{

TIM1->CCR1 = 0;

TIM1->CCR2 = 0;

}

Would this kind of thing make sense? And then would I need to set CCRx to not be 0 at 10,000?

SGold.3
SGold.3Author
Associate II
May 6, 2021

I've tried a couple variations of the above code and none have gotten the signals to flip. Is there anything I may be missing?