Faster DIG algorithm


Closed Thread
Results 1 to 9 of 9

Hybrid View

  1. #1
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Faster DIG algorithm

    I confirmed you observations when I reran the routines @ 16MHz and PBPL.

    I did notice if you use PBP's "DIG" command, the number of instruction cycles used are not consistent for various 4 digit numbers.

    Here is the data I collected.

    Code:
    Algorithm 1:
    Pressure = 1234
    'Start Stopwatch in MPLAB Simulator
    tmpDig0 = Pressure dig 0
    tmpDig1 = Pressure dig 1
    tmpDig2 = Pressure dig 2
    tmpDig3 = Pressure dig 3
    'Stop Stopwatch in MPLAB Simulator
    
    Algorithm 2:
    Pressure = 1234
    'Start Stopwatch in MPLAB Simulator
    tmpDig0 = Pressure // 10
    tmpDig1 = Pressure / 10
    tmpDig2 = Pressure / 100
    tmpDig3 = Pressure / 1000
    
    tmpDig1 = tmpDig1 // 10 
    tmpDig2 = tmpDig2 // 10
    tmpDig3 = tmpDig3 // 10
    'Stop Stopwatch in MPLAB Simulator
    
    Algorithm 3:
    Pressure = 1234
    tmpDig0  = Pressure // 10 
    pressure = Pressure / 10 
    tmpDig1  = Pressure // 10
    pressure = Pressure / 10
    tmpDig2  = Pressure // 10
    tmpDig3  = Pressure / 10
    'Stop Stopwatch in MPLAB Simulator
    
    
    PBPW @ 16MHz
    Algorithm   Inst Cycles     Time
    1           2958            739.50 us
    2           2086            521.50 us
    3           1783            445.75 us
    
    PBPL @ 16MHz
    Algorithm   Inst Cycles     Time
    1           9626            2.406500 ms
    2           6830            1.707500 ms
    3           5839            1.459750 ms
    Obviously PBPL slows down the routines considerably.

    Good luck.
    Regards,
    TABSoft

  2. #2
    Join Date
    Sep 2009
    Posts
    755


    Did you find this post helpful? Yes | No

    Default Re: Faster DIG algorithm

    Thanks.
    I'll post current consumption when I get PCB.

  3. #3


    Did you find this post helpful? Yes | No

    Default Re: Faster DIG algorithm

    Hi guys,

    This is an interesting topic.

    I was wondering if the following line ( i dont know how fast it runs ) can also run faster with the technic shown above:

    Code:
    LCDOUT $FE,LINE2,"FREQ ",DEC DISPFREQ DIG 2,DEC DISPFREQ DIG 1,",",DEC DISPFREQ DIG 0, " KHz    "
    Best regards
    Rui

  4. #4
    Join Date
    Jan 2013
    Location
    Texas USA
    Posts
    229


    Did you find this post helpful? Yes | No

    Default Re: Faster DIG algorithm

    Yes, it will reduce the number of instruction cycles in your example.
    Given that you are only extracting 3 digits of your DISPFREQ variable, Algorithm #2 will be faster.

    Assuming you are using PBPW not PBPL here are the instruction cycles results.

    Using the DIG method: 7606 Inst Cycles
    Using Algorithm #2: 7293 Inst Cycles

    That is a savings of 313 instruction cycles. If you are running a 4Mhz Fosc/4 MCU then that would be a 313us savings.
    Though it is a small savings on cycles and time, it does come at a cost of 3 extra byte variables to hold the values.

    Here are the two code options tested.

    Code:
    tmpDig0 var byte
    tmpDig1 var byte
    tmpDig2 var byte
    DISPFREQ var byte
    LINE2 con $c0
    
    
    DISPFREQ = 1234
    
    'Start Stopwatch in MPLAB Simulator
    LCDOUT $FE,LINE2,"FREQ ",DEC DISPFREQ DIG 2,DEC DISPFREQ DIG 1,",",DEC DISPFREQ DIG 0, " KHz    "
    'Stop Stopwatch in MPLAB Simulator
    'Output: FREQ 23,4 KHz
    
    'Start Stopwatch in MPLAB Simulator
    tmpDig0 = DISPFREQ // 10
    tmpDig1 =DISPFREQ / 10
    tmpDig1 = tmpDig1 // 10
    tmpDig2 = DISPFREQ / 100
    tmpDig2 = tmpDig2 // 10
    
    LCDOUT $FE,LINE2,"FREQ ",DEC tmpDig2,DEC tmpDig1,",",DEC tmpDig0, " KHz    "
    'Stop Stopwatch in MPLAB Simulator 
    'Output: FREQ 23,4 KHz
    Regards,
    TABSoft

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: Faster DIG algorithm

    Greetings Tabsoft,

    Thank you for the information and tip.
    Very helpfull !

    Regards
    Rui

Similar Threads

  1. help about ds1804 (dig. pot.)
    By chailuck in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 7th August 2010, 11:33
  2. A Checksum Algorithm
    By amindzo in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 8th December 2008, 23:33
  3. DIG DEC Madness
    By earltyso in forum mel PIC BASIC Pro
    Replies: 1
    Last Post: - 14th June 2008, 00:05
  4. Use of DIG, DCD, NCD
    By websmith in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 6th November 2006, 07:14
  5. 'DIG' equivalent in other Languages?
    By Art in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 27th November 2005, 23:43

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