# loop time

Discussion in 'Embedded Systems and Microcontrollers' started by homemade, Aug 10, 2010.

May 6, 2008
5
0
if a loop take 65us. running a 4 mhz crystal
how long would it take using a 20 mhz
pic micro asm.
thanks

Apr 5, 2008
15,806
2,389
Hello,

When the processor is running 5 times faster, the looptime will 5 times shorter.
In your case 65 / 5 = 13 us.

Bertus

3. ### Vaughanabe13 Active Member

May 4, 2009
102
5
You didn't specify which PIC you're using, but the 18F chips and lower divide the clock by 4 to get the CPU instruction clock. So,

1 instruction = 4 clock cycles.

So if your clock is running at 4 MHz you calculate it like this:
1 instruction cycle: 4/4000000 = 1 microsecond
And if you are running at 20 MHz you calculate it like this:
1 instruction cycle: 4/20000000 = 0.2 microseconds

So then you take the time/instruction and multiply by the number of instructions executed in your loop to get the total execution time. Using the time you gave of 65 us, that would mean you are executing 65/1 = 65 instructions.

So at the new clock frequency of 20 MHz the total time of your loop will be 65 instructions * 0.2 uS/ins. = 13 uS

A way you can verify this is by loading the MPLAB SIM debugger in MPLAB and setting the clock frequency to the clock you are using. Then put breakpoints before the first instruction in your loop and after the last instruction in your loop. Compile and run the program, and it will stop at the break before the loop. Then open the stopwatch in MPLAB can click the zero button. Hit the simulate button again and it will run to the end of the loop, where you placed the second breakpoint. Now the stopwatch will tell you how many clock cycles you just executed and the time it took to execute (based on the value you entered for your clock cycle).

Last edited: Aug 11, 2010