下降沿触发的事件----控制led灯亮灭(edge test)
防止持续触摸 才让led持续点亮;
采用register打拍的方式确定 输入信号touch_flag的 negedge;
module touch_ctrl_led(
input wire sys_clk,
input wire sys_rst_n,
input wire touch_key,
output reg led
);
//touch_key1将输入signal同步到sys_clk下;
//touch_key2对touch_key1打一拍;
reg touch_key1;
reg touch_key2;
wire touch_flag;
always@(posedge sys_clk or negedge sys_rst_n)
if(!sys_rst_n)begin
touch_key1 <=1'b1;
touch_key2 <=1'b1;
end
else begin
touch_key1 <= touch_key;
touch_key2 <=touch_key1;
end
//组合逻辑采集边沿
assign touch_flag =(!touch_key1 && touch_key2)? 1'b1:1'b0;
always@(posedge sys_clk or negedge sys_rst_n)
if(!sys_rst_n)
led <=1'b1;
else if (touch_flag ==1'b1)
led <=~led;
else
led <=led;
endmodule
边沿检测
使用组合逻辑会产生一个周期的高脉冲;
采用时序逻辑则会延迟一拍产生高脉冲;
module posedge_test(
input clk,
input rst_n,
input din,
output key_flag,key_flag1
);
reg r1;
reg r2;
always @(posedge clk or negedge rst_n) begin
if(!rst_n)
r1 <=1'b0;
r2 <=1'b0;
else
r1 <=din;
r2 <=r1;
end
assign key_flag = (!r2 && r1);// posedge test
assign key_flag1 = (r2 && !r1);//negedge test
endmodule
**water_Led
module water_led#(
parameter CNT_MAX=25'd24_999_999 ;
)
(
input wire sys_clk,
input wire sys_rst_n,
output reg [3:0]led_out
);
reg [24:0] cnt;
reg cnt_flag;
always@(posedge sys_clk or negedge sys_rst_n)
if (!sys_rst_n)
cnt <=25'd0;
else if (cnt == CNT_MAX)
cnt <=25'd0;
else
cnt <=cnt +25'd1;
always@(posedge sys_clk or negedge sys_rst_n)
if (!sys_rst_n)
cnt_flag <= 1'b0;
else if (cnt == CNT_MAX)
cnt_flag <= 1'b1;
else
cnt_flag <= 1'b0;
always@(posedge sys_clk or negedge sys_rst_n)
if (!sys_rst_n)
led_out <= 4'b1110;
else if(led_out == 4'b0111) && (cnt_flag == 1'b1)
led_out <= 4'b1110;
else (cnt_flag == 1'b1)
led_out <=(led_out<<1);
endmodule