ICCV7 C Best or Better way to get a pin input state.
I have got to be making this harder than it is, but I cant seem to find a pre defined method in the include files for getting a pin input state. Have I just been missing it? If not the only way I can seem to come up with to get a pins state from INA seems clunky.
Here is how I am currently doing it.
It just seems like a waste of operations to get a pin state.
Any suggestions or ideas would be greatly appreciated as always.
TJ
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I owe everyone here a bunch, So thanks again for answering my dumb questions.
Projects. RG500 ECU system. PropCopter. Prop CanSat. Prop Paste Gun.
Suzuki RG500 in a RGV 250 frame.
Bimota V-Due (Running on the fuel injection system)
Aprilia RS250
Here is how I am currently doing it.
int getPinState(int pin)
{
int pinMask;
pinMask = 1 << pin;
if ( (INA & pinMask) == pinMask)
{
return 1;
}
else
{
return 0;
}
}
It just seems like a waste of operations to get a pin state.
Any suggestions or ideas would be greatly appreciated as always.
TJ
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I owe everyone here a bunch, So thanks again for answering my dumb questions.
Projects. RG500 ECU system. PropCopter. Prop CanSat. Prop Paste Gun.
Suzuki RG500 in a RGV 250 frame.
Bimota V-Due (Running on the fuel injection system)
Aprilia RS250

Comments
int getPinState(int pin) { return INA & (1 << pin) }This is the way it would be done in assembly or Spin. Alternatively, you could have:
int getPinState(int pin) { return (INA & (1 << pin)) != 0 }You could also use the C macro preprocessor to do:
#define getPinState(pin) ((INA & (1 << pin)) != 0)
and let the compiler's optimizer improve the efficiency, particularly if pin is a constant.
Post Edited (Mike Green) : 3/29/2009 5:20:40 PM GMT
int getPinState(int pin) { return (NA & (1 << pin)) >> pin; }This should only be one more instruction in compiled code.
If pin is a constant the following code to be faster on the prop if the compiler does some optimization:
#define PIN 3 int getPinState(int pin) { return (NA & (1 << PIN) ? 1 : 0; }▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Airspace V - international hangar flying!
www.airspace-v.com/ggadgets for tools & toys
int getPinState(int pin) { return (INA >> pin) & 1; }▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propalyzer: Propeller PC Logic Analyzer
http://forums.parallax.com/showthread.php?p=788230
Thanks again,
TJ
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
I owe everyone here a bunch, So thanks again for answering my dumb questions.
Projects. RG500 ECU system. PropCopter. Prop CanSat. Prop Paste Gun.
Suzuki RG500 in a RGV 250 frame.
Bimota V-Due (Running on the fuel injection system)
Aprilia RS250