边沿检测在编写FPGA程序时用的比较多,下面的代码实现了下降沿的检测,同理也可以实现上升沿的检测。
module falling_edge_detection(
input clk,
input rst_n,
input in,
output detection_res //检测结果
);
reg in_0,in_1,in_2;
always @(posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
in_0 <= 1'b0;
in_1 <= 1'b1;
in_2 <= 1'b2;
end
else
begin
in_0 <= in;
in_1 <= in_0;
in_2 <= in_1;
end
end
//---detection_res == 1,说明检测到下降沿
assign detection_res = (~in_0) & (~in_1) | (in_2);