Shop OBEX P1 Docs P2 Docs Learn Events
Is it possible to reference outa[Px..Py]? — Parallax Forums

Is it possible to reference outa[Px..Py]?

lyassalyassa Posts: 52
edited 2011-08-09 17:19 in General Discussion
I am new to Spin so execuse me if this is a simple question ...

In the program snippet below, "outa[4..7]" is used few times. If later I wanted to change it to "outa[5..8]", I will have to make many changes. How can I avoid that?

dira[4..7]~~ ' set as output
outa[4..7] := %1111
...
outa[4..7] := %0011
...

I am hoping for something like this:

LEDs := [4..7]
outa[LEDs] := %1111
...
outa[LEDs] := %0101
...

Comments

  • Tracy AllenTracy Allen Posts: 6,666
    edited 2011-08-09 09:29
    Welcome to the forums!

    The first thing that comes to mind is,
    ledp :=4   ' base pin
          leds := %1101      ' desired state
          dira := dira | (%1111 << ledp)     ' base and 4 pins are outputs
          outa := outa & !(%1111 << ledp) | (leds << ledp)
    
    Someone probably has a more efficient way.
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-08-09 09:47
    You can use variables to specify the bit range for dira and outa, such as "dira[a..b] := c".
  • Tracy AllenTracy Allen Posts: 6,666
    edited 2011-08-09 10:06
    I may have misunderstood what you asked, and over complicated. Do you need to redefine those pins dynamically, as the program runs, or statically at the time the program is compiled? If the latter, it is simply a matter of defining constants for the pins and using the names instead of the numbers.

    CON
    ledpins = 4

    ...
    PUB
    ...

    dira[ledpin..ledpin+4]~~
    outa[ledpin..ledpin+4] := %1010
  • lyassalyassa Posts: 52
    edited 2011-08-09 16:31
    I was hoping to be able to refer to outa[x], or outa[x..y] with some kind of name related to what I am doing, e.g. in the code, instead of saying outa[4], I would say "OverFlowLED", or instead of outa[2..5] I would say "LevelLeds". The suggestion from Dave and what you suggested in your last reply is definitely better than hard coding the pin numbers, but does not provide the level of abstract reference I was hoping for.

    I thought I could use address reference, e.g. ScoreLeds := @outa[4..7], and then set the value at that address to whatever I need. However, that statement does not compile. I think because outa is a register, not a memory locaton? Not sure.
  • Dave HeinDave Hein Posts: 6,347
    edited 2011-08-09 17:01
    outa, outa[4] and outa[4..7] are implemented with different Spin bytecodes. Also, outa is a register, and not a hub RAM location, so the @ operation doesn't work with it. You can use outa[x..y], and set x and y to whatever you need at the time. x could be equal to y, or you could make x equal to 31 and y equal to y equal to 0 to access the whole register. Note that outa[4..7] is different than outa[7..4]. I believe 7..4 will give you the normal bit ordering, and 4..7 will be the bit reversed order.
  • lyassalyassa Posts: 52
    edited 2011-08-09 17:19
    Ha ... an idea just popped up ...

    PUB main
    ...
    ScoreLEDS(%1101)
    ...
    ScoreLEDS(%1010)


    PRI ScoreLEDS (val)
    outa[4..7] := val

    This way, outa[4..7] appears only once in the code.
Sign In or Register to comment.