Negative pin numbers
John Abshier
Posts: 1,116
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:
This program gave the results I wanted, all 1's, but I am uncomfortable using a nondocumented "feature."
John Abshier
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
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
'Just a few PropSTICK Kit bare PCBs left!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"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
The Parallax compiler does not flag it as illegal either.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Pull my finger!
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
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:
-Phil
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
'Just a few PropSTICK Kit bare PCBs left!
John Abshier
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·"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
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!