Verilog写题笔记 | #状态机与时钟分频#
状态机与时钟分频
https://www.nowcoder.com/practice/25d694a351b748d9808065beb6120025
`timescale 1ns/1ns
module huawei7(
	input wire clk  ,
	input wire rst  ,
	output reg clk_out
);
//*************code***********//
reg [1:0] cnt;
reg [2:0] state;
parameter WAIT = 'b001;
parameter SEND = 'b010;
parameter WRIT = 'b100;
always @(posedge clk or negedge rst) 
begin
    if(rst == 1'b0)
        begin
            state <= WAIT;
        end
    case (state)
        WAIT: begin
            if(rst == 1'b1)
                begin
                    state <= SEND;
                end 
            else
                begin
                    state <= WRIT;
                end
            end
        WRIT: begin
            if(cnt == 2'd3)
                begin
                    state <= SEND;
                end
            else
                begin
                    state <= WRIT;
                end 
            end
        SEND:begin
            if(cnt == 2'd0)
                begin
                    state <= WRIT;
                end
            else
                begin
                    state <= SEND;
                end
        end
        default: state <= WAIT;
    endcase
end
always@(posedge clk or negedge rst)
begin
	if(rst == 1'b0)
		begin
			cnt <= 2'd3;
			clk_out <= 1'b0;
		end
	else if(cnt == 2'd3)
		begin
			cnt <= 2'd0;
			clk_out <= 1'b1;
		end
	else
		begin
			cnt <= cnt + 1'b1;
			clk_out <= 1'b0;
		end
end
//*************code***********//
endmodule
这个题出的不是太好,最简单的一个计数器就搞定了,非要用状态机的话,就会导致状态机用起来不伦不类。换言之就是复杂度用不到状态机。所以我这里就象征性的用了一个一阶段状态机表示一下状态,输出还是用平常的计数器做了。二阶段状态机真用不到,要是通过状态的变化来控制计数器,就得考虑延拍,这样就会出现状态跟输出不一致的问题。
vivo公司氛围 351人发布

