Skip to main content
Associate III
April 3, 2025
Question

small and fast: µs-delay with TIMER4

  • April 3, 2025
  • 2 replies
  • 294 views

First suggestion after internet inquiry:

// Initialization procedure:

RCC->APB1ENR = 0b00000000000000000000000000000010;

TIM4->PSC = 15; 
TIM4->ARR = 65534;
TIM4->CR1 = (1<<0);

// Function:

void delay_US(uint32_t us)
{
uint32_t i;
for( i = 0; i <= us; i++ )
{
/* Clear the count */
TIM4->CNT = 0;
/* Wait UIF to be set */
while((TIM4->SR & TIM_SR_UIF) == 0); /* ?!?!? */
/* Reset UIF */
TIM4->SR &= ~TIM_SR_UIF;
}
}

 

I need a short common µs-delay - any ideas for the marked µc?

 

 

2 replies

Andrew Neil
Super User
April 3, 2025

Why would you want a delay to be Fast ?

Surely, a delay is a deliberate waste of time.

A microsecond is a microsecond - it can't be "slow" or "fast" ?

Did you mean, accurate ?

A complex system that works is invariably found to have evolved from a simple system that worked.A complex system designed from scratch never works and cannot be patched up to make it work.
timbo2023Author
Associate III
April 3, 2025

correction accepted - accurate to ~20ns ...

 

Is there a possibility with DWT (Data Watchpoint Trigger) ?

Could you explain it shortly?