Shop OBEX P1 Docs P2 Docs Learn Events
ICCV7 C Best or Better way to get a pin input state. — Parallax Forums

ICCV7 C Best or Better way to get a pin input state.

TJHJTJHJ Posts: 243
edited 2009-03-30 23:23 in Propeller 1
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

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-03-29 17:14
    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
  • virtuPICvirtuPIC Posts: 193
    edited 2009-03-29 18:35
    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 for tools & toys
  • mparkmpark Posts: 1,305
    edited 2009-03-29 22:06
    To get 1 or 0 this might be a little faster:
    int getPinState(int pin) { 
     return (INA >> pin) & 1;
    }
    
  • jazzedjazzed Posts: 11,803
    edited 2009-03-29 23:36
    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://forums.parallax.com/showthread.php?p=788230
  • TJHJTJHJ Posts: 243
    edited 2009-03-30 23:23
    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
Sign In or Register to comment.