<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<p>I realized my previous post doesn't make much sense without the
context. So here is maybe a better attempt at context. <br>
</p>
<p>This is derived from an email exchange with Murray and Dave
Ackley. It concerns decoding quadrature encoder signals. <br>
</p>
<p>Below is Murrays ASCII art waveform of two outputs of a
quadrature encoder, with my labels. Essentially two square waves
90 degrees out of phase.<br>
</p>
<pre>In the graph below high is logic 1, low is logic 0.
Forward ----> <---- Reverse
A rising fwd A falling fwd
A falling rev A rising rev
| |
V V
+---------+ +---------+ 1
| | | |
A | | | |
| | | |
+---------+ +---------+ +----- 0
+---------+ +---------+ 1
| | | |
B | | | |
| | | |
----+ +---------+ +---------+ 0
Forward is defined as moving left to right across the ASCII art graph. Reverse is moving right to left.
Channel A, rising edge.
Forward: A = 1 and B = 1
Reverse: A = 1 and B = 0
Channel B, rising edge.
Forward: A = 0 and B = 1
Reverse: A = 1 and B = 1
Channel A, falling edge.
Forward: A = 0 and B = 0
Reverse: A = 0 and B = 1
Channel B, falling edge.
Forward: A = 1 and B = 0
Reverse: A = 0 and B = 0
</pre>
<p>Dave pointed out that this can be reduced to two tests using a
couple of XORs. He added a column for the result of XOR'ing the
two channels and whether it increment or decrement the count.<br>
</p>
<p>The following differs from Dave's only in that encoder counts are
incremented in the forward direction and decremented in reverse.
<br>
</p>
<pre>Channel A, rising edge. XOR Action
Forward: A = 1 and B = 1 0 => increment \
Reverse: A = 1 and B = 0 1 => decrement \
\ Channel_A_Any_Change:
Channel A, falling edge. / If ( channel_A ^ channel_B) count--;
Forward: A = 0 and B = 0 0 => increment / else count++;
Reverse: A = 0 and B = 1 1 => decrement /
------------------------------------------------------
Channel B, rising edge.
Forward: A = 0 and B = 1 1 => increment \
Reverse: A = 1 and B = 1 0 => decrement \
\ Channel_B_Any_Change:
Channel B, falling edge. / If ( channel_A ^ channel_B) count++;
Forward: A = 1 and B = 0 1 => increment / else count--;
Reverse: A = 0 and B = 0 0 => decrement /
</pre>
<p><br>
</p>
<p>So when an interrupt happens on channel A, regardless of whether
by rising or falling edge, if (A XOR B) count--; else count++.
Similarly when an interrupt happens on channel B, regardless of
edge, if (A XOR B) count++; else count--;</p>
<p>thanks</p>
<p>David</p>
<p><br>
</p>
<p><br>
</p>
<p><br>
</p>
<br>
</body>
</html>