Skip to main content
Sergii Volchenko
Associate III
March 14, 2024
Solved

Again Microsecond timer on STM32C011F6U6

  • March 14, 2024
  • 4 replies
  • 2024 views

Hi,

I tried to reach 1us delay. To do that I use a timer on 1MHz and try to reach more or less precision delay.

 

void delayMicro(uint16_t delayMc) {
 __HAL_TIM_SET_COUNTER(&htim16,0); // set the counter value a 0
 while (__HAL_TIM_GET_COUNTER(&htim16) < delayMc);
}
 HAL_TIM_Base_Start(&htim16);
 while () {
 delayMicro(100);
 HAL_GPIO_TogglePin(OUT1_GPIO_Port, OUT1_Pin);
 }

 

SergiiVolchenko_2-1710415105639.png

 

SergiiVolchenko_0-1710414975564.png

And the result is always desired delay+6-8us

SergiiVolchenko_1-1710415059317.png

Even if I set delay = 1us, I get 7-8us.

I did something wrong or is this GPIO or microcontroller limitation?

Best answer by TDK

> And the result is always desired delay+6-8us

Overhead. Your timer has a precision of 1us, so expect some jitter due to that.

7us on a 12 MHz chip is 84 ticks. That's not a lot. You're jumping/returning to a function, dereferencing pointers, accessing registers. Likely compiling under Release mode would do much better. Using direct register access will help a bit.

4 replies

Tesla DeLorean
Guru
March 14, 2024

You don't show any of the TIM initialization code.. check prescaler, check clocking via MCO pin.

You don't want to have interrupts.

Perhaps easier to delta the TIM16->CNT than keep resetting

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Sergii Volchenko
Associate III
March 14, 2024
 htim16.Instance = TIM16;
 htim16.Init.Prescaler = 11;
 htim16.Init.CounterMode = TIM_COUNTERMODE_UP;
 htim16.Init.Period = 65534;
 htim16.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
 htim16.Init.RepetitionCounter = 0;
 htim16.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;

And I don't use interrupt for this timer.

SergiiVolchenko_0-1710416189625.png

Implementing delta for CNT didn't help.

AScha.3
Super User
March 14, 2024

>I did something wrong or is this GPIO or microcontroller limitation?

You did more than something wrong.

"If you feel a post has answered your question, please click ""Accept as Solution""."
TDK
TDKAnswer
Super User
March 14, 2024

> And the result is always desired delay+6-8us

Overhead. Your timer has a precision of 1us, so expect some jitter due to that.

7us on a 12 MHz chip is 84 ticks. That's not a lot. You're jumping/returning to a function, dereferencing pointers, accessing registers. Likely compiling under Release mode would do much better. Using direct register access will help a bit.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Sergii Volchenko
Associate III
March 14, 2024

Yeah, I think the same but hope that I'm wrong. :)

gregstm
Senior II
March 16, 2024

My belated thoughts -

Agree with Tesla, don't bother resetting the counter, just use it as a reference and compare it with a register/variable with a delta added to it.

If these things are critical, I would be doing something in assembler to minimise overheads (including loop overheads).