limiting to "no less than" zero


Closed Thread
Results 1 to 31 of 31

Hybrid View

  1. #1
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    Thanks guys!

    Well... I guess the answer to my question is that there isn't really a way that's as easy as using "min" to limit the upper end.

    The method that Charles posted is what I've been using. But I was hoping for something that was... simpler.
    I guess I'd hoped there was some way as clean and easy as "MAX" but that worked all the way down to zero.

    Darrels method using volume.15 is great, but it "wastes" half of a variable.
    Most of the variables that I need to limit are declared as bytes, and I need the full 255 step range for some of them.
    I *can* use it for my temperature setpoint because that needs a word anyway and I can afford to "lose" part of it.

    I'm starting to run low on code space on this project so hoping to find more efficient ways to do a few things...

    Thanks for the input!

    Steve

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


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    Quote Originally Posted by Byte_Butcher View Post
    Darrels method using volume.15 is great, but it "wastes" half of a variable.
    Most of the variables that I need to limit are declared as bytes, and I need the full 255 step range for some of them.
    With 8-bit values, the CARRY flag is valid after addition or subtraction.

    So it can be ...
    Code:
    if buttonB then
        Volume = Volume - Vstep
        IF !STATUS.0 THEN Volume = 0   ; Zero if BORROWED
    endif
    DT

  3. #3
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    STATUS ?
    STATUS.0 ?

    Ahhh, Darrel, you confuseth me. From whence comes... STATUS ?

    steve

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


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    STATUS is a register in the PIC that contains the results (status) of arithmetic operations.
    Depening on the chip family, it may also have Watchdog Timer status and FSR/page bits.

    STATUS.0 is the CARRY flag.
    After an addition, if the result is larger than 255 the carry bit will be set (1).
    After a subtraction, if the result is less than 0 (BORROW), the carry bit will be cleared (0).

    The carry bit is only valid after addition/subtraction with 8-bit values.
    After math with WORD values, the carry bit cannot be used because not all of the math is done by the hardware.
    DT

  5. #5
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    Quote Originally Posted by Darrel Taylor View Post
    The carry bit is only valid after addition/subtraction with 8-bit values.
    After math with WORD values, the carry bit cannot be used because not all of the math is done by the hardware.
    OK, curiosity got me so I tried it with WORD values and it still seems to work...


    x = 32768
    Y = 32769
    x = x + y
    Result: X = 1, STATUS.0 = 1
    '--------------------------------

    x = 32768
    Y = 32765
    x = x + y
    Result: X = 65533, STATUS.0 = 0
    '-------------------------------------

    x = 32768
    Y = 32765
    x = x - y
    Result: X = 3, STATUS.0 = 1
    '--------------------------------------

    x = 32765
    Y = 32771
    x = x - y
    Result: X = 65530, STATUS.0 = 0
    '------------------------------------


    I thought it was only 'sposed to work with 8 bit numbers?


    Steve

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


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    Try it with ...

    X = 32768
    Y = 65523
    DT

  7. #7
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    Quote Originally Posted by Darrel Taylor View Post
    Try it with ...

    X = 32768
    Y = 65523
    Nope, that didn't work.
    OK, 8 bit math then....

    Thanks!

  8. #8
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    4,170


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    If the previous subtraction produced negative results, bit 0 of STATUS is set (to 1). Then you check this condition and if true, set Volume to 0.

    By the way which chip are you controlling?

    Ioannis

  9. #9
    Join Date
    Jan 2009
    Location
    California, USA
    Posts
    323


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    Wow... thanks Darrel. THAT is an interesting item to know about.
    I'm sure I can make good use of that!

    Ioannis, I'm using a 16F887. And thanks for helping explain the STATUS thing!


    steve

  10. #10
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    Darrel = After a subtraction, if the result is less than 0 (BORROW), the carry bit will be cleared (0).
    Ioannis = If the previous subtraction produced negative results, bit 0 of STATUS is set (to 1)

    OK guys(gals?)
    if I read those two statements correctly... they are contradictory. You can't have it both ways... which is it??
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

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


    Did you find this post helpful? Yes | No

    Default Re: limiting to "no less than" zero

    Don't believe anything just because somebody said so.
    Especially when there's conflicting information.

    Check the datasheet, and test it for yourself. Then you will know the truth.

    This program will help with the testing part.

    Code:
    Volume   VAR BYTE
    Vstep    VAR BYTE
    Result   VAR BYTE
    Carry    VAR BIT
    HIGH PORTD.0     ; initialize serial line
    PAUSE 100
    FOR Volume = 5 TO 0 STEP -1
        FOR Vstep = 5 TO 0 STEP - 1
            Result = Volume - Vstep
            Carry = STATUS.0
            SEROUT2 PORTD.0,84,[DEC Volume," - ",DEC Vstep," = ",DEC Result,"   C = ",DEC Carry,13,10]
        NEXT Vstep
    NEXT Volume
    STOP
    DT

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