ARM Cortex M0 LP1114 12MHz delay timing calculation

I found NXP's example program uses   for (i = 0; i < 0x000FFFFF; i++)  { } to do time delays.  I wonder if they have more user friendly functions like Arduino's delayMilliSecodns().  Anyway, I decided to DIY my own delay function.  I first wrote the following, which seems to delay for about one second.

void delay()
  {
int i;
  for (i = 0; i < 0x000FFFFF; i++)
    {
 }
  }

I checked that the Somy board used a 12MHz crystal.  So perhaps I can calculate the timing.  I guess each for loop takes 3 machine instructions.

1. increment counter.
2. check if counter reaches limit
3. branch if counter overflows.

That means time for each loop should be roughly the following.

(1 second / 12,000,000 instruction) * 3 instruction

= (1/ 4,000,000) second/loop

Now the counter is initialized to FFFFF which a web counter converts to 1,048,575 which is about 1 million.  So the time the for loop takes should be roughly the following.

1,048,575 loops * (time/loop)

= 1,048,575 loops *  (1/4,000,000 second/loop)

=  1,048,575 / 4,000,000 second

= 0.25 second.

But it appears the actual delay is longer than the calculation.  So my first assumption is over optimistic. Each loop might take more than 3 machine instructions, perhaps 12 instructions.

Perhaps I could experiment to get more exact timing.

.END


No comments:

Post a Comment