module control_cnt(clk_100hz,reset,on_off,pause);
input clk_100hz,reset,on_off;
output reg pause;
reg [1:0] current_state;
reg [1:0] next_state;
parameter [1:0] clear=2'b00;
parameter [1:0] on=2'b01;
parameter [1:0] off=2'b10;
always@(posedge clk_100hz or negedge reset)
begin
if(!reset)
current_state<=clear;
else
current_state<=next_state;
end
always@(current_state or on_off or reset)
begin
case(current_state)
clear:
begin
if(!reset)
begin
next_state=clear;
pause=0;
end
else
begin
if(on_off)
begin
next_state=on;
pause=1;
end
else
begin
next_state=clear;
pause=0;
end
end
end
on:
begin
if(!reset)
begin
next_state=clear;
pause=0;
end
else
begin
if(on_off)
begin
next_state=off;
pause=0;
end
else
begin
next_state=on;
pause=1;
end
end
end
off:
begin
if(!reset)
begin
next_state=clear;
pause=0;
end
else
begin
if(on_off)
begin
next_state=on;
pause=1;
end
else
begin
next_state=off;
pause=0;
end
end
end
default: next_state=off;
endcase
end
endmodule
使用边沿检测试试
reg key1,key2;
wire key_pos,key_neg;
always@(posedge clk) begin
if(!rse_n) begin
key1 <= 1'b1; key2 <= 1'b1;
end
else begin
key1 <= key;//key连接按键的IO口
key2 <= key1;
end
end
assign key_neg = key2&&(!key1);
assign key_pos = (!key2) && key1;
这是监测上升沿和下降沿的,按键不是自锁按键,按下后自动弹起,这个解决了,用状态机写的
input clk_100hz,reset,on_off;
output reg pause;
reg [1:0] current_state;
reg [1:0] next_state;
parameter [1:0] clear=2'b00;
parameter [1:0] on=2'b01;
parameter [1:0] off=2'b10;
always@(posedge clk_100hz or negedge reset)
begin
if(!reset)
current_state<=clear;
else
current_state<=next_state;
end
always@(current_state or on_off or reset)
begin
case(current_state)
clear:
begin
if(!reset)
begin
next_state=clear;
pause=0;
end
else
begin
if(on_off)
begin
next_state=on;
pause=1;
end
else
begin
next_state=clear;
pause=0;
end
end
end
on:
begin
if(!reset)
begin
next_state=clear;
pause=0;
end
else
begin
if(on_off)
begin
next_state=off;
pause=0;
end
else
begin
next_state=on;
pause=1;
end
end
end
off:
begin
if(!reset)
begin
next_state=clear;
pause=0;
end
else
begin
if(on_off)
begin
next_state=on;
pause=1;
end
else
begin
next_state=off;
pause=0;
end
end
end
default: next_state=off;
endcase
end
endmodule
最后用了状态机控制输出pause信号,同一个非自锁开关按键作开始和暂停键,低电平表示开始,高电平表示暂停
一周热门 更多>