Shop OBEX P1 Docs P2 Docs Learn Events
what happens if pin number is out of range — Parallax Forums

what happens if pin number is out of range

TWRackersTWRackers Posts: 33
edited 2011-01-03 14:05 in Propeller 1
I'm looking at a piece of code I downloaded from OBEX, and there's a variable named "Strobe" which is used to select one of the 32 I/O pins on the Propeller. The documentation says "Strobe" can be set to -1 if the pin is to remain unused. Farther down are several places where OUTA[Strobe] is used to set the state of the selected pin, but there's no code to guard against the Strobe == -1 case. What do the DIRA / OUTA / INA commands do if the selected pin number is < 0 or > 31? Would these be NO-OPs, or would only the low 5 bits of the pin number be used to force it in range?

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-03 11:13
    The Spin interpreter uses shift instructions to position a one-bit in a 32-bit register (DIRA / OUTA / INA) to implement the corresponding Spin statements. If you'll look at the description of any of the shift instructions, you'll see that they use just the least significant 5 bits of a 32 bit shift count and ignore the rest. As a result, the Spin DIRA / OUTA / INA commands use just the least significant 5 bits of the supplied 32 bit value and ignore the rest of the value.

    Since -1 is just 32 one bits, OUTA[Strobe] would access I/O pin 31 if Strobe is -1. I/O pin 31 is normally an input pin, so the use of OUTA[ 31 } would have no effect.
  • TWRackersTWRackers Posts: 33
    edited 2011-01-03 12:54
    Ahhhh... then the author gets away with it only because near the beginning there are two lines which read

    [/FONT]
    [FONT=Courier New]if Strobe>-1 'if strobe active[/FONT]
    [FONT=Courier New]  DIRA[Strobe]:=1 'set strobe to output[/FONT]
    [FONT=Courier New]
    


    so the direction bit is left alone if Strobe == anything negative. I assume it's done that way so that either (non-negative) the pin is set to output, or (negative) it's left alone for the other cogs to deal with. But wouldn't that imply that if ANOTHER cog were to set pin 31 (if Strobe == -1) to output, that THIS cog would start changing pin 31's state too even if it was intended that there be no strobe generated? Recall that ONLY the DIRA command is guarded by an IF, not the OUTA commands.
  • Mike GreenMike Green Posts: 23,101
    edited 2011-01-03 13:15
    Each cog has its own DIRA and OUTA registers. There's a diagram showing this in the Propeller Manual (see here). Notice how each cog's DIRA gates the cog's OUTA register. If the DIRA bit is zero (input), the OUTA bit doesn't affect the I/O pin.
  • TWRackersTWRackers Posts: 33
    edited 2011-01-03 14:05
    Oh yeah, it's been a while since I went over the I/O details. So the code should be correct (at least with regard to this question). Thanks, I think I learned something today.
Sign In or Register to comment.