Skip to content

Instantly share code, notes, and snippets.

@DonDrews

DonDrews/quad.v Secret

Created October 11, 2021 15:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DonDrews/cd14d55def2f3abf6d5aed5ac0b41703 to your computer and use it in GitHub Desktop.
Save DonDrews/cd14d55def2f3abf6d5aed5ac0b41703 to your computer and use it in GitHub Desktop.
Quadrature with Reset Line
module quad(clk, rst, quadA, quadB, count);
input clk, rst, quadA, quadB;
output [7:0] count;
reg [2:0] quadA_delayed;
reg [2:0] quadB_delayed;
always @(posedge clk) quadA_delayed <= {quadA_delayed[1:0], quadA};
always @(posedge clk) quadB_delayed <= {quadB_delayed[1:0], quadB};
wire count_enable = quadA_delayed[1] ^ quadA_delayed[2] ^ quadB_delayed[1] ^ quadB_delayed[2];
wire count_direction = quadA_delayed[1] ^ quadB_delayed[2];
reg [7:0] count;
always @(posedge clk) begin
if(rst) begin
count <= 0;
end
else if(~rst & count_enable)
begin
if(count_direction) count<=count+1; else count<=count-1;
end
end
endmodule
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment