cancel
Showing results for 
Search instead for 
Did you mean: 

Start stop clear timer?

oeliks
Senior

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​

1 ACCEPTED SOLUTION

Accepted Solutions
berendi
Principal

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.

View solution in original post

3 REPLIES 3

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

TIM2->CNT = 0

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
berendi
Principal

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
Senior

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​