Skip to main content
oeliks
Associate III
December 21, 2019
Solved

Start stop clear timer?

  • December 21, 2019
  • 3 replies
  • 1560 views

Hi, do HAL_TIM_Base_Stop will clear couter? When I next Start the timer will it count from 0 or will count from random number that it stopped? There is no Base_Clear as I know.

Thanks​

This topic has been closed for replies.
Best answer by berendi

If you want to be in control, use the hardware registers as described in the reference manual.

The timer functional description chapters have some very helpful and detailed diagrams on the effects of setting various control bits.

Basically, issuing

TIMx->CR1 |= TIM_CR1_CEN;

starts the counter from the actual value of the CNT register, and

TIMx->CR1 &= ~TIM_CR1_CEN;

freezes the value in CNT.

The register description says that CNT is a read-write register, so you can set it to 0 or any other value when you want that.

Unfortunately the HAL documentation lacks that level of detail, so if you want to see exactly what they are doing, you have to reverse-engineer the HAL functions, which might be a lot more effort than following the step-by-step instructions in the reference manual.

3 replies

Tesla DeLorean
Guru
December 21, 2019

Don't know, you can inspect the source, and experiment

TIM2->CNT = 0

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
berendi
berendiBest answer
Principal
December 21, 2019

If you want to be in control, use the hardware registers as described in the reference manual.

The timer functional description chapters have some very helpful and detailed diagrams on the effects of setting various control bits.

Basically, issuing

TIMx->CR1 |= TIM_CR1_CEN;

starts the counter from the actual value of the CNT register, and

TIMx->CR1 &= ~TIM_CR1_CEN;

freezes the value in CNT.

The register description says that CNT is a read-write register, so you can set it to 0 or any other value when you want that.

Unfortunately the HAL documentation lacks that level of detail, so if you want to see exactly what they are doing, you have to reverse-engineer the HAL functions, which might be a lot more effort than following the step-by-step instructions in the reference manual.

oeliks
oeliksAuthor
Associate III
December 21, 2019

I d​id:

HAL_TIM_Base_Start(&htim3);

HAL_Delay(1); // random

​HAL_TIM_Base_Stop(&htim3);

uint16_t x= TIM3->CNT;​

if (x==0)

{

Send_usb_ok(); // just send info its ok

}

And if I didnt make mistake in code, it clears the CNT register after Base_Stop.

Thanks !​

Prescaler=0

Period=65536

Pulse=0​