Shop OBEX P1 Docs P2 Docs Learn Events
SPIN Theory Question1 — Parallax Forums

SPIN Theory Question1

Happy New Year, forum members!!

I came across bit of code:

con
#(true) ON, OFF

The parenthesis are to list parameters; the pound sign is an Object Constant reference. The Propeller Manual v1.1 has an example for the Object Constant reference as numbers (#1) and then some other user defined words. I understand that example in the manual. What I don't understand is why is "true" in parenthesis instead of a number?

Thank you

Comments

  • AribaAriba Posts: 2,690
    TheTech69 wrote: »
    Happy New Year, forum members!!

    I came across bit of code:

    con
    #(true) ON, OFF
    ...


    # is used for enumeration of constants in a Spin CON section.
    true is a predefined constant with value -1

    So the enumeration starts with -1 for ON, and OFF is set one higher which is 0.

    You could also write it like that:

    CON
    ON = -1
    OFF = 0


    Andy
  • TheTech69TheTech69 Posts: 51
    edited 2018-01-03 16:18
    Andy,

    Ok...but I know that "1" usually equates to a HIGH output on the pin. Since TRUE is predefined as "-1" how does that make the output HIGH?

    I might be thinking too much about this and probably should press the "I Believe" button but I am wanting to learn the theory behind why the constant declaration was written the way it is.

    Thank you
  • Happy New Year....

    1 = high

    0 = low

    1 =voltage

    0 =ground

    That is how I've always viewed it but maybe I'm wrong. I'm just learning SPIN and have little experience other then what I've learned so far.
  • AribaAriba Posts: 2,690
    -1 = $FFFFFFFF = %11111111111111111111111111111111

    so ON (or true) is all 32 bits set, and OFF is all bits cleared.

    If you set only one portpin you can use 0 and 1 to output low or high.
    But if you set several bits at once, it's helpful to have ON defined with all bits set:
       OUTA[7:4] = ON
    
    This sets pins P4..P7 to high. For this example ON needs to have the lowest 4 bits set. To make it always work one can just sets all bits to 1 for ON.

    It's BTW the same if you write:
       OUTA[7:4]~~
    
    But I don't like these ~~ and ~ to set and clear pins, it's very unreadable.

    Andy
  • JonnyMacJonnyMac Posts: 9,104
    edited 2018-01-03 21:52
    I use this style in my code and agree with everything in Andy's post.

    Like him, I don't like the trailing tildes, and if you do a test, using the form:
      outa[7..4] := ON
    
    ...is actually a little faster than:
      outa[7..4]~~
    
    ...which may not make sense to people new to some of the details of unique Spin operators.

    The parenthesis are not necessary when using true in that line, but if you change it to:
      #-1, ON, OFF
    
    ...it will not compile in BST. This does:
      #(-1), ON, OFF
    

Sign In or Register to comment.