边沿监测代码常用在接口逻辑设计中,通过监测接口信号的高低电平边沿的变化控制模块中其它信号的操作;也可用在时序的实现中,通过监测时钟沿的监测信号,做出相应的逻辑操作;逻辑代码如下:
`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Engineer: FPGABo
// Create Date: 19:02:36 05/23/2012
// Module Name: EdgeCheck
// Target Devices: XS3C500E-5PQ208
// Tool versions: ISE 12.3
// QQ:851220539
// Revision:
// Revision 0.01 - File Created
//////////////////////////////////////////////////////////////////////////////////
module EdgeCheck(//文件名,模块名采用帕斯卡命名法
input CLK,
input RSTn,
input Signal,
output [4:0] FallEdge,
output [4:0] RiseEdge
);
/////////////////////////////////////////
//监测输入信号的边沿
//方法1.1
/////////////////////////////////////////
reg fedge11,fedge12,fedge13,fedge14;
reg redge1,redge2;
always@(posedge CLK or negedge RSTn)begin
if(!RSTn)begin
{fedge11,fedge12,fedge13,fedge14} <= 4'hf;
{redge1,redge2} <= 2'b0;
end
else begin
fedge11 <= Signal;
fedge12 <= fedge11;
fedge13 <= fedge12;
fedge14 <= fedge13;
redge1 <= Signal;
redge2 <= redge1;
end
end
assign FallEdge[0] = (fedge14)&(fedge13)&(!fedge12)&(!fedge11);//1100_, 延时一个clk
assign RiseEdge[0] = (!redge2)&(redge1); // 01_,不延时
/////////////////////////////////////////
//监测输入信号的边沿
//方法1.2
/////////////////////////////////////////
reg fedge21,fedge22;
reg redge21,redge22,redge23,redge24;
always@(posedge CLK or negedge RSTn)begin
if(!RSTn)begin
{fedge21,fedge22} <= 2'b11;
{redge21,redge22,redge23,redge24} <= 4'b0000;
end
else begin
fedge21 <= Signal;
fedge22 <= fedge21;
redge21 <= Signal;
redge22 <= redge21;
redge23 <= redge22;
redge24 <= redge23;
end
end
assign FallEdge[1] = (fedge22)&(!fedge21);//10
assign RiseEdge[1] = (!redge24)&(!redge23)&(redge22)&(redge21); // 0011
/////////////////////////////////////////
//监测输入信号的边沿
//方法2.1
/////////////////////////////////////////
reg [3:0]fedger1;
reg [3:0]redger1;
always@(posedge CLK or negedge RSTn)begin
if(!RSTn)begin
fedger1 <= 4'b1111;
redger1 <= 4'b0000;
end
else begin
fedger1 <= {fedger1[2:0],Signal};
redger1 <= {redger1[2:0],Signal};
end
end
assign FallEdge[2] = (fedger1[3:1] == 3'b110) ?1'b1 : 1'b0;//110_高电平在时钟沿过后的第一个clk出现
assign RiseEdge[2] = (redger1[3:1] == 3'b001) ?1'b1 : 1'b0;//001_
/////////////////////////////////////////
//监测输入信号的边沿
//方法2.2
/////////////////////////////////////////
reg [2:0]fedger2;
reg [2:0]redger2;
always@(posedge CLK or negedge RSTn)begin
if(!RSTn)begin
fedger2 <= 3'b111;
redger2 <= 3'b000;
end
else begin
fedger2 <= {fedger2[1:0],Signal};
redger2 <= {redger2[1:0],Signal};
end
end
assign FallEdge[3] = (fedger2[2:1] == 2'b10) ? 1'b1 : 1'b0;//10_
assign RiseEdge[3] = (redger2[2:1] == 2'b01) ? 1'b1 : 1'b0;//01_
/////////////////////////////////////////
//监测输入信号的边沿
//方法2.3和2.2都不监测寄存器的最低位,而这效果相同
/////////////////////////////////////////
reg [1:0]fedger3;
reg [1:0]redger3;
always@(posedge CLK or negedge RSTn)begin
if(!RSTn)begin
fedger3 <= 2'b11;
redger3 <= 2'b00;
end
else begin
fedger3 <= {fedger3[0],Signal};
redger3 <= {redger3[0],Signal};
end
end
assign FallEdge[4] = (fedger3[1:0] == 2'b10) ? 1'b1 : 1'b0;//10
assign RiseEdge[4] = (redger3[1:0] == 2'b01) ? 1'b1 : 1'b0;//01
endmodule
|