Rotary Encoder
The following code is for a rotary encoder with 2-bit gray code output. It has an 8-bit output which increases/decreases by rotating the rotary encoder
Consider the following 2 waveforms:
Binary output:

Gray code output:

The state changes of the signals will most likely not occur at the exact same point in time. When looking at the binary coded rotary encoder, this means that it will jump between quadrants. For example it could produce an output of 0-1-0-2 or 0-1-3-2 instead of the desired 0-1-2. Looking at the grey coded output, this problem does not exist as only 1 signal changes at any crossing (all crossings are invariant).

Gray code
Most rotary encoders come with a gray code output. This is because it only has invariant crossings, making it less prone to errors then rotary encoders with binary output.Consider the following 2 waveforms:
Binary output:

Gray code output:

The state changes of the signals will most likely not occur at the exact same point in time. When looking at the binary coded rotary encoder, this means that it will jump between quadrants. For example it could produce an output of 0-1-0-2 or 0-1-3-2 instead of the desired 0-1-2. Looking at the grey coded output, this problem does not exist as only 1 signal changes at any crossing (all crossings are invariant).
Verilog code
C: rotary_encoder
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| module quad(clk, quadA, quadB, count); input clk; input quadA; input quadB; output reg [7:0] count; reg [2:0] quadA_delayed; reg [2:0] quadB_delayed; wire count_enable = quadA_delayed[1] ^ quadA_delayed[2] ^ quadB_delayed[1] ^ quadB_delayed[2]; wire count_direction = quadA_delayed[1] ^ quadB_delayed[2]; always @(posedge clk) begin quadA_delayed <= {quadA_delayed[1:0], quadA}; quadB_delayed <= {quadB_delayed[1:0], quadB}; end always @(posedge clk) begin if(count_enable) begin if(count_direction) count<=count+1; else count<=count-1; end end endmodule |
03-'10 VGA timing
02-'10 PS/2 Keyboard Communication
Comments
Comments are closed