Multiple if then optimization


Closed Thread
Results 1 to 36 of 36

Hybrid View

  1. #1
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Default

    A Different Approach (317 Words ... without most of Darrel's amazing methods, which means there is room for more codesavings)

    Code:
    ;@__config _XT_OSC & _WDT_ON & _MCLRE_ON & _LVP_OFF & _CP_OFF
    CMCON=%00000111
    DEFINE DEBUG_BAUD 2400   'Debug baud rate 
    DEFINE DEBUGIN_REG PORTB 'Debugin pin port 
    DEFINE DEBUGIN_BIT 0     'Debugin pin bit 
    DEFINE DEBUGIN_MODE 1    'Debugin mode: 0 = True, 1 = Inverted 
    
    ;DEFINE OSC 4
    
    xB0 var byte     ; SERIAL IN IS A BYTE SO MAKE IT A BYTE
    ;inputData var word ' variable to receive data into
    
    ' START OF MAIN PROGRAM
    '
    CMCON = 7 ' RA0-RA3 are digital I/O
    TRISA = 0 ' PORT A is output
    TRISB = 1 ' RB0 is Input others output
    
    MAIN:
    
    DEBUGin [xB0]
    
    ;B0 = 100 * B0  ; why do these two lines?  B0 is already a Byte so work with Bytes
    ;Uin = B0 / 207  ;
    
    LCDOUT $FE, 1
    
    If xB0 < 21 then
        LCDOUT "TOO LOW"
        GOTO OUT
    ENDIF
    
    IF xB0 < 34 then BARGRAPH1
    IF xB0 < 42 then BARGRAPH2
    IF xB0 < 52 then BARGRAPH3
    IF xB0 < 63 then BARGRAPH4
    IF xB0 < 73 then BARGRAPH5
    IF xB0 < 92 then BARGRAPH6
    IF xB0 < 112 then BARGRAPH7
    IF xB0 < 131 then BARGRAPH8
    
    LCDOUt $FE, $C0
    
    IF xB0 < 152 then BARGRAPH9
    IF xB0 < 170 then BARGRAPH10
    IF xB0 < 181 then BARGRAPH11
    IF xB0 < 191 then BARGRAPH12
    IF xB0 < 201 then BARGRAPH13
    IF xB0 < 210 then BARGRAPH14
    IF xB0 < 220 then BARGRAPH15
    IF xB0 < 230 then BARGRAPH16
    
    LCDOUT $FE,$C0,"TOO RICH"
    
    goto OUT
    
    BARGRAPH16:
    gosub p255
    BARGRAPH15:
    gosub p255
    BARGRAPH14:
    gosub p255
    BARGRAPH13:
    gosub p255
    BARGRAPH12:
    gosub p255
    BARGRAPH11:
    gosub p255
    BARGRAPH10:
    gosub p255
    BARGRAPH9:
    gosub p255
    
    LCDOUT $FE, $80   ; line 1
    
    BARGRAPH8:
    gosub p255
    BARGRAPH7:
    gosub p255
    BARGRAPH6:
    gosub p255
    BARGRAPH5:
    gosub p255
    BARGRAPH4:
    gosub p255
    BARGRAPH3:
    gosub p255
    BARGRAPH2:
    gosub p255
    BARGRAPH1:
    gosub p255
    
    OUT: 
    Pause 5
    goto MAIN
    
    p255:
    lcdout 255
    return
    
    
    END ' End of program
    [disclaimer] untested [/disclaimer]
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

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


    Did you find this post helpful? Yes | No

    Arrow

    Good one Paul.

    Completely different way, and saved 18 words.

    And you're right. The multiplication uses a lot of code space.
    So I thought I'd remove it from my last version too and see what happened.

    268 words
    Code:
    <font color="#000000"><b>CMCON </b>= <font color="#800000"><b>7                </b></font><font color="#0000FF"><b><i>' Disable Comparators
    
    </i></b></font><font color="#008000"><b>DEFINE </b></font><b>DEBUG_BAUD </b><font color="#800000"><b>2400   </b></font><font color="#0000FF"><b><i>'Debug baud rate 
    </i></b></font><font color="#008000"><b>DEFINE </b></font><b>DEBUGIN_REG PORTB </b><font color="#0000FF"><b><i>'Debugin pin port 
    </i></b></font><font color="#008000"><b>DEFINE </b></font><b>DEBUGIN_BIT </b><font color="#800000"><b>0     </b></font><font color="#0000FF"><b><i>'Debugin pin bit 
    </i></b></font><font color="#008000"><b>DEFINE </b></font><b>DEBUGIN_MODE </b><font color="#800000"><b>1    </b></font><font color="#0000FF"><b><i>'Debugin mode: 0 = True, 1 = Inverted 
    
    </i></b></font><b>Uin       </b><font color="#008000"><b>VAR BYTE
    </b></font><b>B0        </b><font color="#008000"><b>VAR BYTE 
    </b></font><b>BarCount  </b><font color="#008000"><b>VAR BYTE
    </b></font><b>BarValue  </b><font color="#008000"><b>VAR WORD
    </b></font><b>MsgAddr   </b><font color="#008000"><b>VAR WORD
    </b></font><b>Carry     </b><font color="#008000"><b>VAR </b></font><b>STATUS</b>.<font color="#800000"><b>0        </b></font><font color="#0000FF"><b><i>; Hardware carry flag
    
    </i></b></font><font color="#008000"><b>GOTO </b></font><b>Main                     </b><font color="#0000FF"><b><i>; Jump over data
    ;=============================================================================
     </i></b></font><b>BarSteps</b>:  <b>POKECODE   </b><font color="#800000"><b>22</b></font>,<font color="#800000"><b>33</b></font>,<font color="#800000"><b>41</b></font>,<font color="#800000"><b>51</b></font>,<font color="#800000"><b>62</b></font>,<font color="#800000"><b>72</b></font>,<font color="#800000"><b>91</b></font>,<font color="#800000"><b>111</b></font>,<font color="#800000"><b>130</b></font>,<font color="#800000"><b>151</b></font>,<font color="#800000"><b>169</b></font>,<font color="#800000"><b>180</b></font>,<font color="#800000"><b>190</b></font>,<font color="#800000"><b>200</b></font>,<font color="#800000"><b>209</b></font>,<font color="#800000"><b>219
    </b></font><b>_BarSteps   </b><font color="#008000"><b>CON </b></font><b>EXT           </b><font color="#0000FF"><b><i>; create a PBP constant that points to the Table
    
     </i></b></font><b>TooLow</b>:  <b>POKECODE </b><font color="#800000"><b>$FE</b></font>,<font color="#800000"><b>1</b></font>,<font color="#FF0000">&quot;TOO LOW &quot;</font>,<font color="#800000"><b>0
    </b></font><b>_TooLow   </b><font color="#008000"><b>CON </b></font><b>EXT             </b><font color="#0000FF"><b><i>; PBP constant pointing to the &quot;Too Low &quot; string
      
     </i></b></font><b>TooRich</b>: <b>POKECODE </b><font color="#800000"><b>$FE</b></font>,<font color="#800000"><b>$C0</b></font>,<font color="#FF0000">&quot;TOO RICH&quot;</font>,<font color="#800000"><b>0
    </b></font><b>_TooRich  </b><font color="#008000"><b>CON </b></font><b>EXT             </b><font color="#0000FF"><b><i>; PBP constant pointing to the &quot;Too Rich&quot; string
      
    </i></b></font><b>FlashSize </b><font color="#008000"><b>CON </b></font><b>EXT
    </b><font color="#008000"><b>ASM
       </b></font><font color="#000080">ifdef BSR
    FlashSize = 2                 </font><font color="#0000FF"><b><i>; 18F has byte addressing (2)
       </i></b></font><font color="#000080">else
    FlashSize = 1                 </font><font color="#0000FF"><b><i>; 12F/16F has words (1)
       </i></b></font><font color="#000080">endif
    </font><font color="#008000"><b>ENDASM
    
    </b></font><b>MidChar   </b><font color="#008000"><b>CON </b></font><b>EXT             </b><font color="#0000FF"><b><i>; MidChar is the border between
    </i></b></font><font color="#000080">@MidChar = 8 * FlashSize      </font><font color="#0000FF"><b><i>; 8 char segments of the 1x16 LCD being used
    
    </i></b></font><b>CountLength  </b><font color="#008000"><b>CON </b></font><b>EXT
    </b><font color="#000080">@CountLength = 15 * FlashSize
    
    </font><font color="#0000FF"><b><i>;=============================================================================
    </i></b></font><b>Main</b>:
        <font color="#008000"><b>DEBUGIN </b></font>[<b>Uin</b>]             <font color="#0000FF"><b><i>; Serial Input
        </i></b></font><font color="#008000"><b>LCDOUT </b></font><font color="#800000"><b>$FE</b></font>, <font color="#800000"><b>2             </b></font><font color="#0000FF"><b><i>; LCD Home Cursor
    
        </i></b></font><font color="#008000"><b>FOR </b></font><b>BarCount </b>= <font color="#800000"><b>0 </b></font><font color="#008000"><b>TO </b></font><b>CountLength </b><font color="#008000"><b>STEP </b></font><b>FlashSize
            </b><font color="#008000"><b>IF </b></font>(<b>BarCount </b>= <b>MidChar</b>) <font color="#008000"><b>THEN LCDOUT </b></font><font color="#800000"><b>$FE</b></font>,<font color="#800000"><b>$C0  </b></font><font color="#0000FF"><b><i>; Second half of 1x8 LCD
            </i></b></font><b>PEEKCODE </b>(<b>_BarSteps</b>+<b>BarCount</b>),<b>BarValue       </b><font color="#0000FF"><b><i>; Get BarValue from Table
            </i></b></font><b>R0</b>.<b>LowByte </b>= <b>Uin </b>- <b>BarValue
            </b><font color="#008000"><b>IF </b></font><b>Carry </b><font color="#008000"><b>THEN                                </b></font><font color="#0000FF"><b><i>; Uin &gt; BarValue
                </i></b></font><font color="#008000"><b>LCDOUT </b></font><font color="#800000"><b>255                               </b></font><font color="#0000FF"><b><i>;  show a Full char
            </i></b></font><font color="#008000"><b>ELSE                                         </b></font><font color="#0000FF"><b><i>; Uin &lt;= BarValue
                </i></b></font><font color="#008000"><b>LCDOUT </b></font><font color="#FF0000">&quot; &quot;                               </font><font color="#0000FF"><b><i>;  show a blank
            </i></b></font><font color="#008000"><b>ENDIF
        NEXT </b></font><b>BarCount
        
        R0</b>.<b>LowByte </b>= <b>Uin </b>- <font color="#800000"><b>22                            </b></font><font color="#0000FF"><b><i>; if Uin &lt; 22
        </i></b></font><font color="#008000"><b>IF </b></font>!<b>Carry </b><font color="#008000"><b>THEN </b></font><b>MsgAddr </b>= <b>_TooLow </b>: <font color="#008000"><b>GOSUB </b></font><b>ShowMsg
    
        R0</b>.<b>LowByte </b>= <b>Uin </b>+ <font color="#800000"><b>29                           </b></font><font color="#0000FF"><b><i>; if Uin &gt; 227
        </i></b></font><font color="#008000"><b>IF </b></font><b>Carry </b><font color="#008000"><b>THEN </b></font><b>MsgAddr </b>= <b>_TooRich </b>: <font color="#008000"><b>GOSUB </b></font><b>ShowMsg
    </b><font color="#008000"><b>GOTO </b></font><b>Main
    
    </b><font color="#0000FF"><b><i>;=============================================================================
    </i></b></font><b>ShowMsg</b>:
        <b>PEEKCODE MsgAddr</b>, <b>B0
        </b><font color="#008000"><b>IF </b></font><b>B0 </b>= <font color="#800000"><b>0 </b></font><font color="#008000"><b>THEN RETURN
        LCDOUT </b></font><b>B0
        MsgAddr </b>= <b>MsgAddr </b>+ <b>FlashSize
        </b><font color="#008000"><b>GOTO </b></font><b>ShowMsg
    </b><font color="#008000"><b>RETURN
    </b></font>
    DT

  3. #3
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    Good one Paul.

    Completely different way, and saved 18 words.

    And you're right. The multiplication uses a lot of code space.
    So I thought I'd remove it from my last version too and see what happened.
    ...
    "TOO " is used twice. This could be arranged to save a few words more.

    :>)
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

Similar Threads

  1. Multiple PICS from Same Crystal?
    By WOZZY-2010 in forum General
    Replies: 2
    Last Post: - 6th February 2010, 16:18
  2. Multiple analog inputs ?
    By rngd in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 25th February 2008, 16:13
  3. Multiple PIC programming
    By Nicholas in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 8th May 2007, 00:47
  4. Multiple PIC's with 1 crystal
    By puma in forum Schematics
    Replies: 11
    Last Post: - 20th March 2007, 18:02
  5. Multiple IR LEDs from 1 port using transistor
    By belpe123 in forum General
    Replies: 3
    Last Post: - 20th May 2005, 23:07

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