Shop OBEX P1 Docs P2 Docs Learn Events
Negative pin numbers — Parallax Forums

Negative pin numbers

John AbshierJohn Abshier Posts: 1,116
edited 2008-11-21 18:09 in Propeller 1
What does the Spin compiler do with negative pin numbers?· First you may ask, why?· I have written an object for Pololu line sensors.· Some of them have an LED control pin.· I set this pin to output and set it high or low to control the LED.· The Start method is Start(ledPin, firstSensorPin, numSensors).· One of the sensors does not have an LED control pin.· If I have a spare pin, I could use it.· But it would be nice do use a dummy pin number when using the sensor with no LED control pin.· Another example is BS2_Functions.· It wants a tx and rx pin.· I would prefer to not use the debug functions and use extended full duplex serial instead.· BS2_Functions eats two pins.· I have run a small test with negative pin numbers:
CON
  _clkmode = xtal1 + pll16x
  _xinfreq = 5_000_000
OBJ
     Sio  : "Extended_FDSerial"        
VAR
   byte char
Pub Main
  waitcnt(clkfreq * 5 + cnt)    ' wait to select PST
  Sio.start(31,30,0,115200)     ' Rx,Tx, Mode, Baud
  dira[noparse][[/noparse]2]~                      ' input, pulled high
  sio.bin(ina[noparse][[/noparse]2],1)
  dira[noparse][[/noparse]-2]~~                    ' output
  sio.bin(ina[noparse][[/noparse]2],1)
  outa[noparse][[/noparse]-2]~                     ' low
  sio.bin(ina[noparse][[/noparse]2],1)

This program gave the results I wanted, all 1's, but I am uncomfortable using a nondocumented "feature."



John Abshier

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-11-20 23:16
    The Spin compiler doesn't "do" anything with negative pin numbers. They're just numbers. What happens will all depend on the Start method that you mentioned (but didn't list the source for). If it's well-written, it will detect the negative value and not try to assign a pin for it.

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Just a few PropSTICK Kit bare PCBs left!
  • Ken PetersonKen Peterson Posts: 806
    edited 2008-11-21 01:34
    I would expect it to behave as if the number were rounded off to 5 LSBs, but perhaps this is not true? The question is, if you did outa[noparse][[/noparse]-1]~, what would happen? BradC's BST doesn't flag it as an error when compiling. I'm working in Linux now so haven't tried it in Windoze

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"I have always wished that my computer would be as easy to use as my telephone.· My wish has come true.· I no longer know how to use my telephone."

    - Bjarne Stroustrup
  • BradCBradC Posts: 2,601
    edited 2008-11-21 06:16
    outa[noparse][[/noparse]-1]~ would be the same as outa[noparse][[/noparse]$FFFFFFFF]~ from quick memory. It's a legal construct if not a very clever one.
    The Parallax compiler does not flag it as illegal either.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Pull my finger!
  • John AbshierJohn Abshier Posts: 1,116
    edited 2008-11-21 15:24
    Phil, the start method just sets the pin as an dira to output and outa to low. Other calls set the pin high or low.

    Ken, in the code I posted I set a pin , -2, as a low output. The real pin 2 was pulled high with a 10k resistor and ina was 1.

    John Abshier
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-11-21 17:28
    John,

    In that case, there's nothing that says you can't change the Start method. I would suggest editing it so as not to set DIRA or OUTA when the specified pin is negative. Whether or not any pins actually do get set when indexed by a negative value, you'll have better code that clearly spells out what's supposed to happen.

    In my objects, I sometimes define a constant called NONE that's set to $8000_0000. When I want to specify no pin, I use NONE like this:

    OBJ
    
      io : "MyIO"
    
    PUB Main
    
      io.Start(1, io#NONE)
    
    
    


    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Just a few PropSTICK Kit bare PCBs left!
  • John AbshierJohn Abshier Posts: 1,116
    edited 2008-11-21 17:50
    Phil, I know that I can edit the program to check for negative numbers. I was just looking for a way to avoid the if pin < 0's in the code. Using you constant, $8000_0000, is great. Is there some reason that that constant is better than any other negative number?

    John Abshier
  • Ken PetersonKen Peterson Posts: 806
    edited 2008-11-21 17:59
    Perhaps because it's the furthest from 0.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·"I have always wished that my computer would be as easy to use as my telephone.· My wish has come true.· I no longer know how to use my telephone."

    - Bjarne Stroustrup
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2008-11-21 18:09
    John,

    There's nothing in the world wrong with having a <0 test. Even in my objects that use NONE, I check for less-than-zero. There's nothing special about my choice of $8000_0000 value, BTW, except that it's negative.

    -Phil

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    'Just a few PropSTICK Kit bare PCBs left!
Sign In or Register to comment.