How long does this take to execute


Closed Thread
Results 1 to 24 of 24

Hybrid View

  1. #1
    Join Date
    Nov 2003
    Posts
    98

    Default How long does this take to execute

    Hello all
    'how many clock cycles for the two lines inside the LOOP?

    DEFINE OSC 32

    ' chip is 16F1939, 8MHz xtal 4X PLL = 32MHz clock

    OutputPin1 var PORTD.0
    InputPin1 var PORTD.1
    Sw1 var byte

    'here is executable section
    do
    OutputPin1 = 1
    Sw1 = InputPin1
    loop
    END

    ' is it as simple as n per line or is there compiler overhead ?
    ' and everything else being equal would an 18F be the same?

  2. #2
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: How long does this take to execute

    It compiles to this:
    Code:
    0001                      M L00001
    0001   140F               M         bsf     PORTD,  000h
    0002   3000               M         movlw   0
    0003   188F               M         btfsc   PORTD, 001h
    0004   3001               M         movlw   1
    0005   00BA               M         movwf   _Sw1
    0006   33FA               M         bra     L00001
    So as far as I can see the part IN the actual loop takes either 5 or 6 cycles, then the jump back to the start of the loop is another 2 cycles.
    At 32MHz each instruction cycle is 125ns.
    It doesn't seem to make any difference if it's for a 16F or 18F but be aware that if you have the WDT enabled it's possible that the compiler "injects" an instruction to clear it depending on where, in a larger program, this snippet ends up.

    /Henrik.

  3. #3
    Join Date
    Nov 2003
    Posts
    98


    Did you find this post helpful? Yes | No

    Default Re: How long does this take to execute

    just what i needed to know, thanks and hello again Henrik

    so if i had 4 pairs of lines like above inside the loop (different pins) then i would have
    6 * 4 = 24 cycles
    24 + 2 = 26 cycles
    26 * .125 = 3.25uS
    sound about right ?
    i am not doing anything that requires high precision regarding number, if it was 2x slower i would be ok, if it was 3 - 4x i would need to know to make context adjustments

    not incredibly speedy is it ?
    that's only 300kHz for 6 pins

    just to be clear
    so 1/32MHz = 31nS
    one instruction cycle = 4 clock periods ?
    (this is starting to come back to me, it fades so quickly if you don't do this every day)

    this is probably fast enough though and i assume reading a whole port would be about the same as reading a pin ?
    in some areas that should speed things up quite a bit

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: How long does this take to execute

    Hi,
    Yes, one instruction cycle is 4 Clock cycles. That's where the Fosc/4 you see all around comes from. At 32MHz each instruction cycle (1/32M)*4 or 125ns.

    Reading a whole port is obviously faster than Reading the 8 pins indvidually if that's what you're asking.

    Four "sets" of those two line compiles to
    Code:
    000004                    M L00001
    000004 8082               M         bsf     PORTC,  000h
    000006 0E00               M         movlw   0
    000008 B282               M         btfsc   PORTC, 001h
    00000A 0E01               M         movlw   1
    00000C 6E1A               M         movwf   _Sw1
    00000E 8482               M         bsf     PORTC,  002h
    000010 0E00               M         movlw   0
    000012 B682               M         btfsc   PORTC, 003h
    000014 0E01               M         movlw   1
    000016 6E1B               M         movwf   _Sw2
    000018 8882               M         bsf     PORTC,  004h
    00001A 0E00               M         movlw   0
    00001C BA82               M         btfsc   PORTC, 005h
    00001E 0E01               M         movlw   1
    000020 6E1C               M         movwf   _Sw3
    000022 8082               M         bsf     PORTC,  000h
    000024 0E00               M         movlw   0
    000026 B282               M         btfsc   PORTC, 001h
    000028 0E01               M         movlw   1
    00002A 6E1D               M         movwf   _Sw4
    00002C D7EB               M         bra     L00001
    I count 20 instructions. Some of them (the btfsc) may take two cycles to execute but when they do the instruction AFTER the btfsc is skipped so in the end it'll still take 20 cycles, something I didn't take into account in the previous post. 20 * 125ns = 2.5us.

    With that said I'm far from an expert in assembly programming so I truly hope someone that is corrects me if I'm wrong.

    If you can explain what you're trying to do it may be easier to help you choose the best/fastest option.

    /Henrik.

  5. #5
    Join Date
    Nov 2003
    Posts
    98


    Did you find this post helpful? Yes | No

    Default Re: How long does this take to execute

    at the moment this is good, i am just roughing in some rough estimates for various parts of the overall program loop

    at this stage whether it is 5 vs 6 shouldn't matter, 20 vs 5 or 6 might

    where i am going to need some real help i think is when i get to the RS485 output which needs to run continuously 512 bytes in the overall loop at 250k baud,
    the program will need to respond to switches and things and is reading about 30 analogs which produce the serial stream

  6. #6
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: How long does this take to execute

    DMX512 by any chance?
    If you really need a continous stream at 250k baud then I think you need to some careful planning (and I guess that IS what you're doing with these test so thumbs up!).
    At 250k baud you've only got 40us to do stuff Before you need to feed the next byte the UART (well you CAN wait up to 80us since there'a basically a single byte buffer) between each byte.

Similar Threads

  1. Unable to execute mpasmwin
    By oldmainframer in forum General
    Replies: 3
    Last Post: - 17th December 2016, 23:01
  2. Interrupt won't execute.
    By bison_bloke in forum mel PIC BASIC Pro
    Replies: 23
    Last Post: - 26th March 2010, 15:46
  3. Replies: 5
    Last Post: - 24th February 2009, 18:55
  4. Proteus Execute Error
    By pramarn in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 6th October 2006, 08:51
  5. SEROUT2 takes 1400ms to execute?
    By droptail in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 16th March 2006, 17:08

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts