Shop OBEX P1 Docs P2 Docs Learn Events
Bitwise XOR — Parallax Forums

Bitwise XOR

mojorizingmojorizing Posts: 249
edited 2009-03-14 22:31 in Propeller 1
The following post·suggests code to detect state of change on a pin· Low to High.··It mentions that one can do this with several pins, which I'd like to do. However, isn't ^ bitwise XOR, not meant for more than a bit? I'd like to detect a change of state on INA[noparse][[/noparse]11..14] then branch off to different code depending on what pin went high.
Assuming a main repeat loop that executes "often enough", it would look something like this snippet:

oldstate := inA[noparse][[/noparse]0]  ' initialize state variable


repeat
  newstate := inA[noparse][[/noparse]0]   ' current state of input
  xstate := newstate ^ oldstate & newstate   ' detect change of state, 0->1
  oldstate := newstate   ' push state down
  if xstate := 1
    !myvariable     ' or do whatever needs to be done. xstate=1 only once through the loop
  ' other program action here, then repeat
  ' this has to repeat often enough so as not to miss both the high state and the low state



This does not include debouncing, but sometimes it is not necessary, or else it can easily be accomplished in hardware with a capacitor. The same state machine can detect change in several pins simultaneously, as in
newstate := inA[noparse][[/noparse]0..7]
for 8 at once. Then the xstate variable is nonzero only when there is a change, and the decoding of which event occured can follow the IF.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Bad spellers of the world untie!

Comments

  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2009-03-14 03:01
    The ^ operator operates on all the bits of its arguments simultaneously. That's what "bitwise" means, versus "logical" (e.g. & | vs. AND OR), the latter applying to the zero- or non-zero-ness of its operands as a whole.

    -Phil
  • AribaAriba Posts: 2,690
    edited 2009-03-14 03:08
    XOR can be used do detect the change of any amount of bits (0..32). For Pins 11..14:
    oldstate := inA[noparse][[/noparse]14..11]   ' initial state of input pins
    repeat
      newstate := inA[noparse][[/noparse]14..11]   ' current state of input pins
      xstate := newstate ^ oldstate & newstate   ' detect change of 1 to 4 bits, 0->1
      oldstate := newstate   ' push state down
      if xstate & |<0            'the bits 14..11 are shifted to 3..0 in xstate
        ' call methode for Pin11
      if xstate & |<1
        ' call methode for Pin12
      if xstate & |<2
        ' call methode for Pin13
      if xstate & |<3
        ' call methode for Pin14
    
    



    Andy
  • mojorizingmojorizing Posts: 249
    edited 2009-03-14 22:31
    Thanks for clearing up my confusion, and thanks for the code.

    Kevin

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Bad spellers of the world untie!
Sign In or Register to comment.