一种新的状态机写法:current_state <= "Idle";

一种新的状态机写法。用“字符串”替代4'd0,4'd1等等。但要注意,reg [39:0] next_state 只能有五个字符(5*8=40),比较耗费寄存器资源。




状态机源代码:

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2017/03/23 20:33:42
// Design Name: 
// Module Name: state
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module state(clk_100m,btnC,led,next_state);
	input clk_100m;
	input btnC;
	output [3:0] led;
	output [7:0] next_state;

reg [39:0] next_state = "Idle";
reg [3:0] led;

always @(posedge clk_100m) begin
	if (btnC) begin
		next_state <= "Idle";
		led <= 4'b0000;
	end
	else begin
		case(next_state)
			"Idle" :begin
				led <= 4'b0000;
				next_state <= "led1";
			end

			"led1" :begin
				led <= 4'b0001;
				next_state <= "next";
			end

			"next":begin
				led <= {led[2:0],1'b0};
				if (led == 4'b1000) begin
					next_state <= "led1";
				end
			end
		endcase
	end
end


/*
reg [7:0] next_state = 8'd0;
reg [3:0] led;

always @(posedge clk_100m) begin
	if (btnC) begin
		next_state <= 8'd0;
		led <= 4'b0000;
	end
	else begin
		case(next_state)
			8'd0 :begin
				led <= 4'b0000;
				next_state <= 8'd1;
			end

			8'd1 :begin
				led <= 4'b0001;
				next_state <= 8'd2;
			end

			8'd2 :begin
				led <= {led[2:0],1'b0};
				if (led == 4'b1000) begin
					next_state <= 8'd1;
				end
			end
		endcase
	end
end
*/

endmodule


仿真代码:

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2017/03/23 20:52:29
// Design Name: 
// Module Name: sim
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module sim();

	//input
	reg clk;
	reg reset;

	//output
	wire [3:0] LED;
	wire [39:0] State;

state u1(
	.clk_100m(clk),
	.btnC(reset),
	.led(LED),
	.next_state(State)
	);

	//initial
	initial begin
		clk = 0;
		reset = 1;
		#10 clk = !clk;
		#10 clk = !clk;
		#10 clk = !clk;
		reset = 0;
		forever #10 clk = !clk;
	end

endmodule

测试:






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
您好帮我用verilog改下一下这段状态机的代码,现在支持寄存器8bit读写,改写后使得支持寄存器16bit读写,分为高八位低八位,需要用代码加一段状态机还有高八位完了之后有一个ACK响应位: //FSM always @ (posedge clk or negedge rst) if (~rst) i2c_state<=3'b000;//idle else i2c_state<= next_i2c_state; //////////Modified on 25 november.write Address is 30H; Read Address is 31H///// always @(i2c_state or stopf or startf or cnt or sft or sadr or hf or scl_neg or cnt) case(i2c_state) 3'b000: //This state is the initial state,idle state begin if (startf)next_i2c_state<= 3 b001;//start else next_i2c_state <= i2c_state; end 3b001://This state is the device address detect & trigger begin if(stopf)next_i2c_state<=3'b000; else begin if((cnt==4'h9)&&({sft[0],hf} ==2'b00) && (scl_neg ==1'b1)&&(sadr ==sft[7:1])) next i2c_ state<=3'b010;//write: i2c adderss is 00110000 and ACK is sampled //so {sft[0],hf} is 2'b00 else if ((cnt==4'h9)&&({sft[0],hf} ==2'b10) && (scl_neg ==1'b1)&&(sadr ==sft[7:1])) next i2c_ state<=3'b011;//read:i2c adderss is 00110001 and ACK is sampled //so {sft[0],hf} is 2'b10 else if((cnt ==4'h9) && (scl_neg == 1'b1)) next_ i2c_state<=3 'b000;//when the address accepted does not match the SADR, //the state comes back else next_i2c_state<=i2c_state; end end 3'b010: //This state is the register address detect &&trigger begin if (stopf)next_i2c_state<=3'b000; else if (startf)next_i2c_state<=3'b001; else if ((cnt ==4'h9) && (scl_neg == 1'b1)) next_i2c _state<=3'b10 else next i2c_state<=i2c_state; end 3'b011: //This state is the register data read begin if (stopf)next_i2c _state<=3'b000; else if (startf) next_i2c _state<=3'b001; else next_12c_state<=i2c_state; end 3'b100: //This state is the register data write begin if (stopf)next_i2c _state<=3'b000; else if (startf) next_i2c _state<=3b001; else next_i2c_state<=i2c_state; end default://safe mode control next_i2c_state <= 3'b000; endcase
最新发布
06-13

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值