PDA

View Full Version : Using parentheses for operation order when setting an IO pin?



markedwards
- 14th September 2018, 22:31
Is there a way to wrap code in parentheses to set order when setting an IO pin?

I have a number of IO pins I want to toggle based on a mode variable. Essentially only one pin will be low at a time while the others will all be high. I originally did this:


PinA = Mode ^ 1 Min 1
PinB = Mode ^ 2 Min 1
PinC = Mode ^ 3 Min 1
PinD = Mode ^ 4 Min 1
PinE = Mode ^ 5 Min 1

but I then realized that the statement is evaluated as "Mode ^ (2 Min 1)" rather than "(Mode ^ 2) Min 1" and so it makes them all XOR'd by 1.

I then tried to specify order:


PinA = (Mode ^ 1) Min 1
PinB = (Mode ^ 2) Min 1
PinC = (Mode ^ 3) Min 1
PinD = (Mode ^ 4) Min 1
PinE = (Mode ^ 5) Min 1

but this generates a compile error: "ERROR: Macro MIN?WCT not found in macro file."

I could create an extra byte variable and set that:


ExtraVariable = (Mode ^ 1) Min 1
PinA = ExtraVariable
ExtraVariable = (Mode ^ 2) Min 1
PinB = ExtraVariable
ExtraVariable = (Mode ^ 3) Min 1
PinC = ExtraVariable
ExtraVariable = (Mode ^ 4) Min 1
PinD = ExtraVariable
ExtraVariable = (Mode ^ 5) Min 1
PinE = ExtraVariable


This works but it's a bit more clunky. Is there a way I can directly set the IO pin based on the result of the XOR with a min operator to limit it to 0 (low) and 1 (high)? I tried without the "Min 1" but setting the output pins to other numbers aside from 0 and 1 also seems to be problematic.

richard
- 14th September 2018, 23:30
Sometimes you can just brute force it
this complies to less then 1/3 the size


PINA=1
PINB=1
PINC=1
PIND=1
PINE=1
BRANCH MODE,[M0,M1,M2,M3,M4,M5]
M0:
GOTO DONE
M1:
PINA=0
GOTO DONE
M2:
PINB=0
GOTO DONE
M3:
PINC=0
GOTO DONE
M4:
PIND=0
GOTO DONE
M5:
PINE=0
DONE:

markedwards
- 15th September 2018, 20:47
After having issues with the original code, I did switch mine to



GoSub SetAllPinsHigh
Select Case Mode
Case 1
PinA = 0
Case 2
PinB = 0
Case 3
PinC = 0
Case 4
PinD = 0
Case 5
PinE = 0
End Select


Which did work and I did notice the word count dropped a bit. Your code looks like a pretty good way to do it too, thanks! Is there much difference between BRANCH and SELECT CASE? It looks like BRANCH needs GOTOs to avoid running through any following modes but is BRANCH a more simple / smaller option?

Dave
- 15th September 2018, 21:46
Mark, I did notice that some combined statements like :

IF JUNK & %11110000 = %11110000 THEN 'LOOK AT CURRENT STATE "this doesn't work"
don't work unless surrounded by parenthesis are :

IF ((JUNK & %11110000) = %11110000) THEN 'LOOK AT CURRENT STATE "This works"

I noticed it with such directives as the "&" and the "%".

sayzer
- 16th September 2018, 09:23
After having issues with the original code, I did switch mine to



GoSub SetAllPinsHigh
Select Case Mode
Case 1
PinA = 0
Case 2
PinB = 0
Case 3
PinC = 0
Case 4
PinD = 0
Case 5
PinE = 0
End Select


Which did work and I did notice the word count dropped a bit. Your code looks like a pretty good way to do it too, thanks! Is there much difference between BRANCH and SELECT CASE? It looks like BRANCH needs GOTOs to avoid running through any following modes but is BRANCH a more simple / smaller option?


if you can change the Mode to arrive as 1-2-4-8-16 then ;



GoSub SetAllPinsHigh
PinA = ~Mode.0 '%00000001
PinB = ~Mode.1 '%00000010
PinC = ~Mode.2 '%00000100
PinD = ~Mode.3 '%00001000
PinE = ~Mode.4 '%00010000



Could work.