View Full Version : 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.
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
Mike Green
03-30-2009, 12:14 AM
Because C, like Spin, treats a non-zero value as true, you could say:
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
virtuPIC
03-30-2009, 01:35 AM
Right, Mike. That's the easy way to get the pin value as boolean true or false. If you need the value as int 1 or 0 - maybe you merge several pin values or use it for masking - then it gets only a little more complicated:
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 (http://www.airspace-v.com/ggadgets) for tools & toys
mpark
03-30-2009, 05:06 AM
To get 1 or 0 this might be a little faster:
int getPinState(int pin) {
return (INA >> pin) & 1;
}
jazzed
03-30-2009, 06:36 AM
Nice optimization mpark. Remember that all function calls have overhead. The macro like Mike mentioned may make code bigger if used several times, but will be faster than a function ... just watch out passing values to some macros and check your friendly C reference for problems. #define GETBIT(pin) ((INA >> (pin)) & 1) /* pin >= 0 && pin < 32 */
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
--Steve
Propalyzer: Propeller PC Logic Analyzer (http://www.brouhaha.com/~sdenson/Propalyzer)
http://forums.parallax.com/showthread.php?p=788230 (http://forums.parallax.com/showthread.php?p=788230)
Thanks all, mpark that works perfectly for what I wanted.
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