-
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.
-
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
-
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 ;)
-
Code:
btn_x = PORTB & %00111110
If btn_x=2 would mean that the other inputs are all low, but PORTB.1
-
Quote:
Originally Posted by
flotulopex
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.
-
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".