2017-03-19 01:51 PM
Hello Everyone,
Does every one know on how to create a simple 1 uS delay with timer ?
Thanks
#stm32f1xx #timer #delay-1us2017-03-19 01:56 PM
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 ?
2017-03-19 02:43 PM
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);
2017-03-19 03:55 PM
and wrap that code as a function delay_us(start) ?
I'll give a try...
2017-03-19 04:06 PM
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.