How to determine machine cycles?

Discussion in 'Embedded Systems and Microcontrollers' started by wannaBinventor, Dec 27, 2010.

  1. wannaBinventor

    Thread Starter Member

    Apr 8, 2010
    179
    4
    Is there a way that I can determine how many clock ticks it takes to perform one loop of my program without just single stepping through MPLAB SIM and counting the instruction executed?

    I have plenty of delays and what not that would take forever to single step through. I thought about starting a timer at the top of the loop, recording the value at the end of the loop, displaying it on an LCD, and computing the cycles but I figured there had to be a more direct way.
     
  2. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Use the stopwatch function in the simulator.
     
  3. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Stopwatch or profiler, I don't think profiling is an option without plugins, most C compilers come with one that shows a graph of processor time spent in each function. This can be used to speed up the whole program or tweak it in other ways.

    You could read the timer value at the start and end, multiply by prescaler and you'd have a constant number.
     
  4. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    The usual way to determine machine cycles for a delay loop is to count them. The data sheet gives you a list of assembler instructions and the number of machine cycles each one take to execute. Simply add up the cycles for all the instructions in the loop and multiple by the number of times the loop is executed to get the total delay. Much simpler and faster than single-stepping and much more accurate than the stopwatch.
     
  5. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    The stopwatch is fine, gives the exact number of cycles. Counting is tricky for me especially when some instructions take one or two cycles depending if there is a skip (like btfss).
     
  6. MMcLaren

    Well-Known Member

    Feb 14, 2010
    759
    116
    What are you talking about? Stopwatch is accurate.

    Just turn on the simulator, view Stopwatch, set clock speed, set Program Counter location, insert breakpoint, and go...

    Here's a good general purpose fixed delay subsystem to play with (much more flexible than those fixed delay code generators);

    Cheerful regards, Mike, K8LH

    Code ( (Unknown Language)):
    1. ;******************************************************************
    2. ;  K8LH DelayCy() subsystem macro generates four instructions     *
    3. ;******************************************************************
    4.         radix   dec
    5. clock   equ     8               ; 4, 8, 12, 16, 20 (MHz), etc.
    6. usecs   equ     clock/4         ; cycles/microsecond multiplier
    7. msecs   equ     clock/4*1000    ; cycles/millisecond multiplier
    8.  
    9. DelayCy macro   delay           ; 11..327690 cycle range
    10.         movlw   high((delay-11)/5)+1
    11.         movwf   delayhi
    12.         movlw   low ((delay-11)/5)
    13.         call    uDelay-((delay-11)%5)
    14.         endm
    15.  
    16. ;******************************************************************
    17. ;  example code for simulation testing                            *
    18. ;******************************************************************
    19.         org     0x000
    20. SimTest
    21.         DelayCy(200*usecs)      ; <- put simulator PC here
    22.         goto    $               ; <- put simulator break point here
    23.  
    24. ;******************************************************************
    25. ;  K8LH DelayCy() subsystem 16-bit uDelay subroutine              *
    26. ;******************************************************************
    27.         nop                     ; entry for (delay-11)%5 == 4     |B0
    28.         nop                     ; entry for (delay-11)%5 == 3     |B0
    29.         nop                     ; entry for (delay-11)%5 == 2     |B0
    30.         nop                     ; entry for (delay-11)%5 == 1     |B0
    31. uDelay  addlw   -1              ; subtract 5 cycle loop time      |B0
    32.         skpc                    ; borrow? no, skip, else          |B0
    33.         decfsz  delayhi,F       ; done?  yes, skip, else          |B0
    34.         goto    uDelay          ; do another loop                 |B0
    35.         return                  ;                                 |B0
    36. ;******************************************************************
    37.         end
    38.  
     
    Last edited: Dec 28, 2010
Loading...