cancel
Showing results for 
Search instead for 
Did you mean: 

1 uS delay with timer ?

antonius
Senior
Posted on March 19, 2017 at 21:51

Hello Everyone,

Does every one know on how to create a simple 1 uS delay with timer ?

Thanks

#stm32f1xx #timer #delay-1us
4 REPLIES 4
antonius
Senior
Posted on March 19, 2017 at 21:56

I've made :

void Delay(int nTime)
{
unsigned int i;
unsigned long j;
for(i = nTime;i > 0;i--)
for(j = 1000;j > 0;j--);
}�?�?�?�?�?�?�?

but not sure how much delay I got on every loop ?

Posted on March 19, 2017 at 22:43

Configure a TIM at 1 MHz, so TIMx->CNT ticks each 1us

Perhaps better just to let the TIM clock at maximal speed, at 72 MHz, 1us would be 72 ticks, at 36 MHz, 36 ticks

uint16_t start = TIMx->CNT;

while((TIMx->CNT - start) < 36);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on March 19, 2017 at 22:55

and wrap that code as a function delay_us(start) ?

I'll give a try...

Posted on March 19, 2017 at 23:06

SW delays are easy traps in bare metal coding unless doing gpio direct manipulation. To be more flexible, use the output compare channels of a timer to set a flag when the programmed time has elapsed. This way, the sw can do other thing in the background while waiting. As a bonus, this compare can flip a gpio at the exactly programmed time.