-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder.v
More file actions
52 lines (51 loc) · 1.29 KB
/
encoder.v
File metadata and controls
52 lines (51 loc) · 1.29 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
module fpga_top (
input clk,
input p1,
input p2,
input btn,
input rst,
output reg [7:0] leds,
output reg [7:0] btndowns,
output reg [7:0] btnups
);
wire [7:0] ledtmp;
wire [7:0] downtmp;
wire [7:0] uptmp;
encoder jerry (.p1(p1), .p2(p2), .btn(btn), .rst(rst), .spin(ledtmp), .btndowns(downtmp), .btnups(uptmp));
always @ * begin
leds = ledtmp;
btndowns = downtmp;
btnups = uptmp;
end
endmodule
module encoder (
input p1,
input p2,
input btn,
input rst,
output reg[7:0] spin,
output reg[7:0] btndowns,
output reg[7:0] btnups
);
reg [1:0] prevstate;
always @ * begin
if (prevstate != {p1,p2}) begin
case (prevstate)
2'b00 : spin = spin + (2'b01 == {p1,p2}) ? 1 : -1;
2'b01 : spin = spin + (2'b11 == {p1,p2}) ? 1 : -1;
2'b11 : spin = spin + (2'b10 == {p1,p2}) ? 1 : -1;
2'b10 : spin = spin + (2'b00 == {p1,p2}) ? 1 : -1;
endcase
prevstate = {p1,p2};
end
end
reg prevbtn;
always @ * begin
if(btn != prevbtn) begin
case (btn)
1: btndowns = btndowns + 1;
0: btnups = btnups + 1;
endcase
end
end
endmodule