Multiple if then optimization


Closed Thread
Results 1 to 36 of 36

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default

    The */ operator first multiplies the 2 values (Uin * 124).
    Then it discards the low byte, and returns the rest.
    So effectively, it divides the result by 256, without having to do the divide part.

    100 / 207 = 0.48309178
    124 / 256 = 0.484375 ; not exact, but pretty close

    You're correct that there are small differences between the 2 formulas, but the error is less than +/- 1. And, you will never see a difference with the resolution of a 16 block bargraph.

    And, there probably is a need to test it.
    I was using a 16F877 with a 4x20 LCD. Could have missed something for your 16F628A and 1x16 LCD.
    <br>
    DT

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Question

    Hi, Darrel

    Could you tell us for LCD Initialization at powerup ...If Uin first value is > 10 ... I do not see any ???

    you just suppose exhaust is air filled ???

    Alain
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

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


    Did you find this post helpful? Yes | No

    Default

    I'm not sure what you mean Alain.

    If the first value is > 10, it will just draw the bargraph like normal.
    It doesn't do anything until a serial byte is received.
    <br>
    DT

  4. #4
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,653


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Darrel Taylor View Post
    I'm not sure what you mean Alain.

    If the first value is > 10, it will just draw the bargraph like normal.
    It doesn't do anything until a serial byte is received.
    <br>
    Hi,Darrel

    What I ment is you initialize the LCD ... ONLY if the value is < 10 ... ( when writing TOO LOW )

    What if you stop the engine and start it again ??? ... first value will be > 10 , due to the exhaust gases still in the exhaust tubing ... and I do not see any LCDOUT $FE,1 in this figure.

    Alain
    Last edited by Acetronics2; - 4th March 2008 at 14:35.
    ************************************************** ***********************
    Why insist on using 32 Bits when you're not even able to deal with the first 8 ones ??? ehhhhhh ...
    ************************************************** ***********************
    IF there is the word "Problem" in your question ...
    certainly the answer is " RTFM " or " RTFDataSheet " !!!
    *****************************************

  5. #5
    skimask's Avatar
    skimask Guest


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    What if you stop the engine and start it again ??? ... first value will be > 10 , due to the exhaust gases still in the exhaust tubing ... and I do not see any LCDOUT $FE,1 in this figure.
    Alain
    Exhaust gases might still be in the tubing, but I wonder how quickly the sensor will cool off and not make a decent output. In my experience, those things cool off fairly quick. But then again, the 3/4 wire ones heat up pretty fast too...

  6. #6
    Join Date
    Jan 2008
    Location
    Selm, Germany
    Posts
    116


    Did you find this post helpful? Yes | No

    Talking Lambda probes

    Hi Guys,

    now thats stuff I´m into ! Those lambda probes are reacting pretty fast when hot, about the whole scale from too lean to too rich in half a second . Reliable results will be in after about half a minute with a cold engine starting when using 4 wire probes, 2 minutes or more with one wire probes and those one wire probes without heating are useless for idle control, heats too low. You can see lots of hidden stuff like choke or accelerator pump work or fuel cutoff when stepping off the gas. Funny to watch but most times absolut neccessary for the first setup.

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


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by Acetronics View Post
    What I ment is you initialize the LCD ... ONLY if the value is < 10 ... ( when writing TOO LOW )

    What if you stop the engine and start it again ??? ... first value will be > 10 , due to the exhaust gases still in the exhaust tubing ... and I do not see any LCDOUT $FE,1 in this figure.
    Oh, you mean the "Clear Screen".
    Since it writes to all 16 characters on each pass, I used "Home Cursor" ($FE,2) instead of "Clear Screen" ($FE,1) to prevent blinking of the display during updates.

    Or, if you're referring to the Power-On initialization that gets rid of the black blocks and makes the LCD work in the first place .... Mugel didn't have any specific initialization sequences in his original program and reported it to be working. So I assumed his display didn't need it.

    The first character sent to the LCD will cause PBP to initialize the display (if it hasn't already). It doesn't have to be $FE,1.
    <br>
    DT

  8. #8
    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
    __________________

  9. #9
    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

Similar Threads

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