+ Reply to Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jun 2005
    Posts
    51

    Default Using parentheses for operation order when setting an IO pin?

    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:
    Code:
    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:
    Code:
    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:
    Code:
    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.

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    1,475

    Default Re: Using parentheses for operation order when setting an IO pin?

    Sometimes you can just brute force it
    this complies to less then 1/3 the size

    Code:
    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:
    This is more entertaining than Free to Air TV

  3. #3
    Join Date
    Jun 2005
    Posts
    51

    Default Re: Using parentheses for operation order when setting an IO pin?

    After having issues with the original code, I did switch mine to

    Code:
    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?

  4. #4
    Join Date
    Mar 2003
    Location
    Commerce Michigan USA
    Posts
    1,107

    Default Re: Using parentheses for operation order when setting an IO pin?

    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 "%".
    Dave Purola,
    N8NTA
    EN82fn

  5. #5
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,147

    Default Re: Using parentheses for operation order when setting an IO pin?

    Quote Originally Posted by markedwards View Post
    After having issues with the original code, I did switch mine to

    Code:
    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 ;

    Code:
    GoSub SetAllPinsHigh
    PinA = ~Mode.0 '%00000001
    PinB = ~Mode.1 '%00000010
    PinC = ~Mode.2 '%00000100
    PinD = ~Mode.3 '%00001000
    PinE = ~Mode.4 '%00010000
    Could work.
    Last edited by sayzer; - 16th September 2018 at 09:26.
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

Similar Threads

  1. Strange Behavior in setting several pin outputs states.
    By kappuz in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 5th August 2017, 15:57
  2. How do you time an operation?
    By Hylan in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 18th May 2010, 10:41
  3. Setting a pin twice
    By RUBiksCUbe in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 19th September 2006, 01:45
  4. Must reset PIC to get correct operation?
    By Christopher4187 in forum General
    Replies: 5
    Last Post: - 2nd June 2006, 01:59
  5. Minimum power operation - 16F877A
    By btaylor in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 2nd March 2006, 02:44

Members who have read this thread : 18

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