IF - WHILE and variables


Closed Thread
Results 1 to 6 of 6
  1. #1
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938

    Default IF - WHILE and variables

    Hello,

    Trying different ways to make some code more efficient, I found that variables may not be used the same way in IF or WHILE commands.

    Code:
    btn_x = VAR BYTE
    btn_x = PORTB & %00111110
    
    if btn_x = 2  then .... => this works
    
    while PORTB & %00111110 = 2 ... => this works too
    
    while btn_X = 2 ... this won't work (?!)
    In the case of IF, btn_x variable will be recognised as in the WHILE, it will not as long as I don't write it as in the code above.

    I understand the difference between both commands functionalities; it is about to know why they can't be handled the same way.
    Roger

  2. #2
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,166


    Did you find this post helpful? Yes | No

    Default

    flotulopex, Some more code would be nice... I havent had any problem with the way you are using them.. Some more code however might solve the problem...

    Dave Purola,
    N8NTA

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    I know what you want to do, but unfortunately you can't do it.

    What you want it's something like
    MakePORTB CON PORTB & %00111110

    but this will not compile. What you may do is use a Macro, but there's no advantage in this case.

    btn_x = PORTB & %00111110
    Work, but only where it is called.

    You may use A timer interrupt to refresh btn_x, but still, there's no real advantages, and you also need another variable to confirm that btn_x have been refreshed.

    Assuming you want to poll some push buttons, then yes Timer interrupt could be interesting. That's how I deal with them most of the time.

    You can use the timer interrupt to read the push buttons, to confirm that it's not just a glitch and then, to do the debouncing. Now we talk
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    Code:
    btn_x = PORTB & %00111110
    If btn_x=2 would mean that the other inputs are all low, but PORTB.1
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  5. #5
    Join Date
    Sep 2007
    Location
    USA, CA
    Posts
    271


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by flotulopex View Post
    Code:
    btn_x = VAR BYTE
    btn_x = PORTB & %00111110
    That is a one-time calculation. It never changes in your loop. You've got to update btn_x in the loop if you don't want a never-ending loop.

  6. #6
    Join Date
    Aug 2006
    Location
    SWITZERLAND (french speaking)
    Posts
    938


    Did you find this post helpful? Yes | No

    Default

    Thank you All.

    The program is a "one-shot" reading (it is not a loop).

    Using the var is a little less words consuming (5 words) as if I use the full statement "PORTB & %00111110".
    Roger

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