Bitwise XOR
mojorizing
Posts: 249
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.
Bad spellers of the world untie!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔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
Andy
Kevin
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Bad spellers of the world untie!