题解 | #Johnson Counter#
Johnson Counter
https://www.nowcoder.com/practice/7ee6e9ed687c40c3981d7586a65bc22d
//方法1 `timescale 1ns/1ns module JC_counter( input clk , input rst_n, output reg [3:0] Q ); always@(posedge clk or negedge rst_n) begin if(!rst_n) begin Q <= 4'd0; end else begin Q <= {!Q[0],Q[3:1]};//低位取反右移 end end endmodule //方法2 `timescale 1ns/1ns module JC_counter( input clk , input rst_n, output reg [3:0] Q ); reg [2:0] cnt; always@(posedge clk or negedge rst_n) begin if(!rst_n) cnt <= 3'd0; else cnt <= cnt + 1'b1; end always@(*) begin if(!rst_n) Q <= 3'd0; else case(cnt) 3'd0:Q <= 4'b0000; 3'd1:Q <= 4'b1000; 3'd2:Q <= 4'b1100; 3'd3:Q<= 4'b1110; 3'd4:Q <= 4'b1111; 3'd5:Q <= 4'b0111; 3'd6:Q <= 4'b0011; 3'd7:Q <= 4'b0001; default:Q <= 4'b0000; endcase end endmodule