verilog检测上升沿、下降沿和双边检测

一、代码

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2022/10/25 21:22:45
// Design Name: 
// Module Name: deceted_negedge
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module deceted_negedge(
input clk,
input rst_n,
input in,

output raising_edge_detect,
output falling_edge_detect,
output double_edge_detect
    );
reg reg_r1,reg_r0;    
//打两个拍子
always@(posedge clk or negedge rst_n) begin
    if(~rst_n)begin
        reg_r0<='b0;
        reg_r1<='b1;
    end
    else begin
        reg_r0<=in;
        reg_r1<=reg_r0;
    end
end 
assign raising_edge_detect= reg_r0 &(~reg_r1);//检测上升沿
assign falling_edge_detect= reg_r1 &(~reg_r0);//检测下降沿
assign double_edge_detect= reg_r1 ^reg_r0 ;//双边检测
    
endmodule

二、仿真文件

`timescale 1ns / 1ps
//
// Company: 
// Engineer: 
// 
// Create Date: 2022/10/25 21:35:02
// Design Name: 
// Module Name: tb
// Project Name: 
// Target Devices: 
// Tool Versions: 
// Description: 
// 
// Dependencies: 
// 
// Revision:
// Revision 0.01 - File Created
// Additional Comments:
// 
//


module tb();
reg clk;
reg rst_n;
reg in;

wire  raising_edge_detect;
wire  falling_edge_detect;
wire   double_edge_detect;

initial begin
    clk=0;
    rst_n=0;
    in=0;
    #100
    clk=1;
    rst_n=1;
    in=1;
    #100
    in=0;
     #100
    in=1;
    #100
    in=0;

end

always #5 clk=~clk   ; 
    
deceted_negedge u0(
.clk(clk),
.rst_n(rst_n),
.in(in),

.raising_edge_detect(raising_edge_detect),
.falling_edge_detect(falling_edge_detect),
.double_edge_detect(double_edge_detect)
);
    
    
    
endmodule

三、结果

在这里插入图片描述
在这里插入图片描述

总结

边沿检测用到的知识点:首先要打两个拍子,然后经过取反操作,再进行于运算,如果是双边检测的话就需要经过异或操作。昨天是程序员的节日,祝愿所有的程序员少遇到bug,一次通过。

--晓凡  20221025日于桂林书
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值