A volume control is one example. There are a lot of other controls, depending upon the equipment, that will be simply easier to use with twisting of some knob, rather than with repeated pushing of a button.
The quadrature encoder switch, or simply encoder switch, is a pure digital device masquerading as an analog potentiometer. It looks and feels like a potentiometer from the outside, but its all digital from the inside. Being a digital device, it works and interfaces directly with any digital port, and does not require special analog to digital conversion circuitry.This blog describes the encoder switch and its usage in brief details, and introduces simple ways of using it with any microcontroller, mimicking the functions of a digital potentiometer in particular.
Photo 4. Three wiper contacts are shown mounted atop the encoder shaft. |
How it works
The encoder switch can be viewed as two ganged switches A and B that alternately connects to C terminal every time you rotate the shaft. Each step will produce a transition on either output. This can be better explained by looking at an actual oscilloscope trace shown in Photo 5.
The upper window shows the A and B output with a shaft rotation. The lower window shows the darkened part of the same trace magnified (horizontal) ten times. Let’s focus our discussion at the yellow trace in the upper window and call this the A trace.
A trace reveals how the shaft was rotated. Starting from the extreme left, the first falling edge shows when rotation is first started. As you start to rotate, slowly at first, pulses appear sparsely spaced, and then comes more closely packed as you pick up speed. Finally, the pulses separation widens again as you deccelerate to a stop.
This sequence of event already gives you useful information- how fast the knob was rotated (by measuring pulse spacing) and by how much (by counting the number of pulses).
Photo 5. Encoder switch output pulse pattern as it is rotated in clockwise direction. |
Photo 6. Pulse pattern output from the same encoder when it is rotated in counterclockwise direction. |
Photo 5 shows the pulse pattern when the encoder shaft is rotated in cw direction, while Photo 6 shows what happens when it is rotated in ccw direction. It is important to remind you at this point that the two rotations are entirely separate event, hence the pulse pattern would look entirely different. What is important this time is to examine what the B output is doing while A makes a transition.
During clockwise rotation:
when A output transits from high to low, output B is low
when B output transits from high to low, output A is high.
During counterclockwise rotation:
when A output transits from high to low, output B is high
when B output transits from high to low, output A is low.Summarizing what we have learn so far
Pulse spacing is a measure of the rate of shaft rotation. This is seldom used, but could be useful with some application.
Number of pulses determines by how much the shaft is rotated. A typical switch encoder produces 32 pulses on each channel in a full (360 degrees) rotation. Hence if we counted 16 pulses, we know that the encoder is rotated halfway. 8 pulses is ¼ of the way, and so on.
The phase relation between the two outputs tells us whether the rotation is in the cw or ccw direction.
Using the Encoder Switch
Now that we know how the encoder switch works, let us discuss how we can use it as a digital potentiometer in a microcontroller circuit. No specific microcontroller will be mentioned in this section, so that we can formulate a general algorithm that could be easily applied to any microcontroller of your choice.
First, lets pick a microcontroller port for the encoder switch. As discussed above, the encoder switch has two outputs, A and B. Two microcontroller inputs are then required to read this. Not just two general purpose inputs however. These inputs must be interrupt enabled, sensitive to the falling edge of the input. And then, connect the encoder switch with the MCU, like as shown in the schematic diagram in figure 1. The pull up resistors may be omitted if your microcontroller has pull up circuitry built-in.
In your program, assign a register as your digital potentiometer. A single 8 bit register will suffice in most cases, but you can extend this to any width as you like. Just put in mind that an 8 bit register will already take 8 full rotations to fill. Every bit you add doubles this number, so use only the number of bits your application requires.
For the purposes of this discussion, let us call the encoder register, or variable, encoder_value. The interrupt routines should be built along the following algorithm:
Interrupt routine A
If B=low then increment encoder_value,
else decrement encoder_value
Interrupt routine B
If A=high then increment encoder_value,Writing the code in C, the above statement should look like as follows:
else decrement encoder_value
Interrupt routine A
If(B==0) encoder_value++;Interrupt routine B
Else encoder_value–;
If(A==0) encoder_value++;Conclusion:
Else encoder_value–;
Photo 7. Encoder switch with push switch |
No comments:
Post a Comment