How to prevent variable going below zero?


Closed Thread
Results 1 to 28 of 28

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    Quote Originally Posted by CuriousOne View Post
    Code:
    reader:
    adcin 0,Z
    pause 100
    IF Z>100 AND Z<130 THEN I=I+5
    IF Z<100 THEN I=I-5
    if i>255 then i=255
    if i<5 then i=0
    HPWM 2,I,10000
    XY=I
    gosub decoder
    goto reader
    1. tumbleweed caught one problem in your code.

    2. The variable I is altered some where? What check are you making ensuring it will not get a value to lead in negative results doing a -5 subtraction inside the reader routine?

    Ioannis

  2. #2


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    I like Tumbleweed's code. Shorter and more to the point.

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,680


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    With a few proviso's mikes way beats it hands down


    Code:
    
    IF Z<100 THEN I=I-5
    asm
         BTFSS STATUS,C
         CLRF   _I
     ENDASM
    
    if i attempts to underflow its set to 0
    Warning I'm not a teacher

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


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    I like it how a simple logic problem CuriousOne had, lead to another topic with negative numbers and STATUS register!

    Quite interesting!

    Ioannis

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,680


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    anyone familiar with assembly programming will be well aware of the status reg and its use. what surprised me is that
    the value of the status reg was/is preserved even after the completion of the transaction. because the result is transferred from the
    working regs back into the result variable during the process i expected the status to be no longer representative of the calculation.
    surprised yet again !
    Warning I'm not a teacher

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


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    You are absolutely right! I do not trust that this may be the case for any PIC though.

    Ioannis

  7. #7
    Join Date
    May 2013
    Location
    australia
    Posts
    2,680


    Did you find this post helpful? Yes | No

    Default Re: How to prevent variable going below zero?

    I do not trust that this may be the case for any PIC though.
    its a quick easy test to check, it works on all chips that i tried just make sure the compiler dosn't compress the subtract into a decrement
    i var can be byte or word

    Code:
    
    j=8 
    i=6 
    while j 
    status=1
     i=i-2  ;subtractant cannot be 1 
    s=status 
    debug 13,10,"status ",bin8 s ,"  i ",    dec i 
    j=j-1 
    wend
    
    Warning I'm not a teacher

Similar Threads

  1. Assigning a string variable to another string variable
    By Christopher4187 in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 2nd June 2013, 18:55
  2. Variable within Variable
    By munromh in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th January 2009, 15:44
  3. Electrical Current Does Prevent Corrosion?
    By T.Jackson in forum Off Topic
    Replies: 13
    Last Post: - 18th March 2008, 14:13
  4. One variable made up of another variable
    By DynamoBen in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 1st September 2007, 01:18
  5. 2 variable
    By isaac in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 24th May 2004, 22:59

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