1. homemade

    Thread Starter New Member

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


    Apr 5, 2008

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

  3. Vaughanabe13

    Active Member

    May 4, 2009
    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
  4. homemade

    Thread Starter New Member

    May 6, 2008
    thanks to you both
    that's what I thought but need to double check myself
    I'm trying to learn asm. and One of the ways I do that is look @ written code and try to figure out what each part does(and ask questions). The code I'm looking @ now did not list the clock speed. I did know what the Timing need to be (finding the start bit,etc.) and 4mhz is a lot closer than 20 LOL

    good tip in using MPLAB SIM debugger that could come in handy as I advice
    with my learning
    thanks again:)