题解 | #状态机与时钟分频#

状态机与时钟分频

https://www.nowcoder.com/practice/25d694a351b748d9808065beb6120025

`timescale 1ns/1ns

module huawei7(
	input wire clk  ,
	input wire rst  ,
	output reg clk_out
);
parameter s0 = 0,s1 = 1;
reg state;

reg [1:0] fsm_cnt;

always @(posedge clk or negedge rst)begin
	if(rst  == 1'b0) begin
		state <= s0;
		fsm_cnt <= 0;
	end
	else begin
		case(state)
			s0 : begin
				fsm_cnt <= 0;
				state <= s1;
			end
			s1 : begin
				fsm_cnt <= fsm_cnt + 1;
				if(fsm_cnt >= 2)begin
					state <= s0;
					fsm_cnt <= 0;
				end
			end
		endcase
	end
end

always @(posedge clk or negedge rst)begin
	if(rst == 1'b0)
		clk_out <= 0;
	else if(state == s0)
		clk_out <= 1;
	else
		clk_out <= 0;
end



endmodule

全部评论

相关推荐

评论
1
收藏
分享
牛客网
牛客企业服务