Step 4Software Theory of Operation
We know the grey coded AB pattern repeats every four positions so if we make the routine work for transitions between those four positions it'll work for all of the others. Notice that in one four position cycle, there are four edges. A rising edge and a falling edge for the A input as well as the B input. The microprocessor will be interrupted each time there is an edge which means that the microcontroller will be interrupted any time the knob is turned. As a result, the ISR needs to figure out which way the knob was turned. To help us figure out how to do this, we turn to the waveform for clockwise rotation.
Notice that any time A has an edge, its new value is always different from that of B. When the knob goes from position 1 to 2, A transitions from logic-0 to logic-1. B is still 0 for this transition and does not match the new value of A. When the knob goes from position 3 to 4, A has a falling edge while B remains at logic-1. Notice again, that B and the new value of A are different. Right now, we can see that any time A causes the interrupt during clockwise rotation, its new value is different from that of B. Let's check B to see what happens. B has a rising edge when the switch transitions from position 2 to 3. Here, the new value of B is the same as A. Looking at the last remaining edge for clockwise rotation, B has a falling edge moving from position 4 to 5. (Position 5 is the same as position 1.) The new value of B is the same as A here as well! We can now make some deductions! If A causes the interrupt and the new value of A is different from that of B, the rotation was clockwise. In addition, if B causes the interrupt and the new value of B is the same as A, then the rotation was clockwise.
Let's quickly examine the case of counterclockwise rotation. Just like clockwise rotation, counterclockwise rotation will cause four interrupts in one cycle: two for input A and two for input B. Input A has a rising edge when the knob moves from position 4 to 3 and a falling edge moving from position 2 to 1. When the knob moves from position 4 to 3, the new value of A is the same as the value of B. Notice that when A moves from position 2 to 1 its new value is the same as that of B as well. Now, we can see that when A causes the interrupt and its new value matches that of B the rotation was counterclockwise. Quickly, we'll look at input B to verify everything. B will cause an interrupt when the knob moves from position 5 (which is the same as 1) to 4 and when the knob moves from position 3 to 2. In both of these cases, the new value of B does not match the existing value of A which is the opposite of the cases when B causes the interrupt for clockwise rotation. This is good news. Everything checks out like it should.
To summarize, if A causes the interrupt and its new value does not match the value of B or if B causes the interrupt and the new value of B matches the value of A we know there was clockwise rotation. We can check the other cases for counterclockwise rotation in software or we can assume that because it wasn't clockwise rotation it was counterclockwise. My routine simply made the assumption.
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|









































