PIC Math - the need for speed


Closed Thread
Results 1 to 19 of 19

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    I determined it was 48 cycles by using this procedure ...

    instruction execution time
    http://www.picbasic.co.uk/forum/showthread.php?t=365

    I've double checked, and it's definately 48. Although my hardware is different, 18F452 @ 20Mhz, it shouldn't make that much difference.

    For your test routine I get these results:
    Code:
    Value = 20000
    Start:                     ' Cycles   uS@20mhz
        PORTD.0 = 1            '    1         .2
        Delay = Value /*$004D  '   48        9.6
        PORTD.0 = 0            '    1         .2
        PauseUs 700            ' 3501      700.2   
    Goto Start                 '    2         .4
                                 -----     -----
                  ' Loop Total   3553      710.6
    And, this I didn't expect. Here's the results from Ingvar's first example
    Code:
                                ' Cycles   uS@20mhz
    Dummy1 = OldPulseTime >> 2  '  31        6.2
    Delay = Dummy1 >> 2         '  31        6.2
    Delay = Delay + Dummy1      '   4         .8
                                 -----     -----
                  '      Total     66       13.2
    But, his second one looks pretty quick. As long as .3125 is close enough.
    Code:
                                ' Cycles   uS@20mhz
    Dummy1 = OldPulseTime >> 1  '   5        1.0
    Dummy1 = Dummy1 >> 1        '   3         .6
    Delay = Dummy1 >> 1         '   5        1.0
    Delay = Delay >> 1          '   3         .6
    Delay = Delay + Dummy1      '   4         .8
                                 -----     -----
                  '      Total     20        4.0
    Last edited by Darrel Taylor; - 10th October 2005 at 21:46.
    DT

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


    Did you find this post helpful? Yes | No

    Default

    Darrel, Ingvar,
    Hmm, this is interesting.
    I tried Ingvars 1'st code and measured it to 82uS. Then I tried the second one and measured it to 22uS which is great! 31.25% works fine for me.

    I'm still wondering though why the numbers doesn't match. Ingvars first code should take 66uS but I measure it to ~80uS. His second one should take 20 and it does, close enough. Then we have Darrel's 48 cycle code that takes ~250uS on my hardware. I just verified that again to make sure.

    I would think my scope was out of calibration if it wasn't for the fact that 700uS pause I have measures correct on the scope.

    The application hardware is 12F629 but the testing above is on a 16F877 @ 4MHz.

    Thanks guys!
    /Henrik Olsson.

  3. #3
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Apparently, your Scope works Really Well.

    I ran the numbers again for a 16F877 this time, and the results are rather surprising.
    Code:
    Start:                     ' Cycles   uS@20mhz
        PORTD.0 = 1            '    1         .2
        Delay = Value /*$004D  '  243       48.6
        PORTD.0 = 0            '    1         .2
        PauseUs 700            ' 3502      700.4
    Goto Start                 '    4         .8
                                 -----     -----
               '    Loop Total   3751      750.2
               ' Without Pause    249
    Code:
                                ' Cycles   uS@20mhz
    Dummy1 = OldPulseTime >> 2  '  37        7.4
    Delay = Dummy1 >> 2         '  37        7.4
    Delay = Delay + Dummy1      '   6        1.2
                                 -----     -----
                  '      Total     80       16.0
    Code:
                                ' Cycles   uS@20mhz
    Dummy1 = OldPulseTime >> 1  '   5        1.0
    Dummy1 = Dummy1 >> 1        '   3         .6
    Delay = Dummy1 >> 1         '   5        1.0 
    Delay = Delay >> 1          '   3         .6
    Delay = Delay + Dummy1      '   6        1.2
                                 -----     -----
                  '      Total     22        4.2
    I really didn't expect that much difference between the same code compiled for the 2 different chips.

    Way to go Ingvar.
    Last edited by Darrel Taylor; - 10th October 2005 at 23:57.
    DT

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


    Did you find this post helpful? Yes | No

    Default

    Darrel,
    Wow, thanks!
    I agree, that's a bit strange isn't it? It's gonna be interesting to see how fast it will run on the 12F629 where it actually will reside and operate when it's finished. That timing routine of yours seems to come in handy from time to time!

    Thanks again!
    /Henrik Olsson.

  5. #5
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Thumbs up

    Darrel,
    You forget that your 18F uses it's hardware multiplier. The 16 series does not have one and needs to do a software multply, that's a HUGE difference. I'm a little surprised that the ">>2" takes that much longer than ">>1". I suspected around 10-20 cycles difference, not 60. It should take a little longer since it uses a looped code rather than the straight code ">>1" produces. Strange, perhaps i'll look into it sometime ............ or not.

    Henrik,
    You should make sure that the variables reside in the same memorybank. You do that by using the "BANK" statement. If you don't it will be slower since the pic needs to switch banks between each statement. Declare your variables like .....
    Code:
    Dummy1          VAR WORD BANK0
    Delay           VAR WORD BANK0
    OldPulseTime    VAR WORD BANK0
    /Ingvar

    PS. Henrik, vart skall jag skicka fakturan DS.

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Right Ingvar,

    That's what it is, the Hardware Multiplier.

    The funny thing is that a couple of years ago, I did a comparison between 16F and 18F multiplication, and found that PBP didn't use the hardware MUL for 18F's. So, I made my own MUL routine for 18F's and have been using it ever since (when I need the speed). I hadn't looked since then, but, meLabs must have snuck it in there when I wasn't paying attention. Nothing listed in the Version History.

    That's good to know!
    <br>

    P.S. Ingvar, Add some to that bill for me too.
    Last edited by Darrel Taylor; - 13th October 2005 at 00:40.
    DT

  7. #7
    Join Date
    Jul 2003
    Location
    Sweden
    Posts
    237


    Did you find this post helpful? Yes | No

    Question

    Sure, as soon as Henrik tells me where to send it

Similar Threads

  1. SMS via pic
    By kenandere in forum GSM
    Replies: 15
    Last Post: - 10th March 2010, 10:00
  2. HSERIN & Interupts (aka controlling PIC programs from a remote PC)
    By HankMcSpank in forum mel PIC BASIC Pro
    Replies: 16
    Last Post: - 17th June 2009, 14:46
  3. pic to pic ir link versus wired link : help please anyone
    By xnihilo in forum mel PIC BASIC Pro
    Replies: 13
    Last Post: - 30th May 2008, 21:01
  4. My PIC can't do the math! Can yours?
    By sayzer in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 12th May 2006, 07:28
  5. Serial Pic to Pic using HSER
    By Chadhammer in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 11th March 2005, 23:14

Members who have read this thread : 0

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