主要是用这个示例来验证上一篇的按键消抖
当每次按键0按下时,4个led的显示状态以二进制加法的格式+1
当每次按键1按下时,4个led的显示状态以二进制加法的格式-1
两个按键输入自然要用两个按键消抖的模块
那么如何判断按键的输入呢
用按键消抖的模块的两个输出 相与结果为1 就说明有完整的一次按键按下。
计数器的实现就看代码吧
module thetop (ina,inb,clka,rsta,leda);
input ina;
input inb;
input clka;
input rsta;
output [3:0] leda;
wire w1,w2,w3,w4;
top a1(
.in1(ina),
.clk1(clka),
.rst_n1(rsta),
.outkey1(w1),
.outflag1(w2)
);
top a2(
.in1(inb),
.clk1(clka),
.rst_n1(rsta),
.outkey1(w3),
.outflag1(w4)
);
addsub a3(
.clk(clka),
.rst_n(rsta),
.inflag0(w2),
.inkey0(w1),
.inflag1(w3),
.inkey1(w4),
.led(leda)
);
endmodule
module addsub(clk,rst_n,inflag0,inkey0,inflag1,inkey1,led);
input clk;
input rst_n;
input inflag0,inflag1;
input inkey0,inkey1;
output reg[3:0] led;
always@(posedge clk or negedge rst_n)
if (!rst_n)
led <= 4'b0000;
else if(inflag0 && inkey0)
led <= led + 1'b1;
else if(inflag1 && inkey1)
led <= led - 1'b1;
else
led <= led;
endmodule
直接搬运上一篇按键消抖的端口列表就可以了
module top(in1,clk1,rst_n1,outkey1,outflag1);
https://blog.csdn.net/helloworld573/article/details/104966731
这里仅模拟了 按键0 按了两次
`timescale 1ns/1ns
`define shizhong 20
module top_tb;
reg clk2;
reg rst_n2;
reg in1;
reg in2;
wire [3:0]leda2;
thetop u1(
.ina(in2),
.inb(in1),
.clka(clk2),
.rsta(rst_n2),
.leda(leda2)
);
initial clk2 = 1;
always#(`shizhong/2) clk2 = ~clk2;
initial begin
rst_n2 = 1'b0;
in2 = 1'b1;
#(`shizhong*10);
rst_n2 = 1'b1;
//模拟出按键的抖动过程 按下抖动
#(`shizhong*10 + 1);
in2 = 0 ;
#1000;
in2 = 1 ;
#2000;
in2 = 0 ;
#1400;
in2 = 1 ;
#2600;
in2 = 0 ;
#1300;
in2 = 1 ;
#200;
in2 = 0 ;
#2_000_000_000; //按键稳定
//释放时抖动
in2 = 1 ;
#2000;
in2 = 0 ;
#1000;
in2 = 1 ;
#2000;
in2 = 0 ;
#1000;
in2 = 1 ;
#2600;
in2 = 0 ;
#1300;
in2 = 1 ;
#2_000_000_000; //按键稳定松开
//模拟出按键的抖动过程 按下抖动
#(`shizhong*10 + 1);
in2 = 0 ;
#1000;
in2 = 1 ;
#2000;
in2 = 0 ;
#1400;
in2 = 1 ;
#2600;
in2 = 0 ;
#1300;
in2 = 1 ;
#200;
in2 = 0 ;
#2_000_000_000; //按键稳定
//释放时抖动
in2 = 1 ;
#2000;
in2 = 0 ;
#1000;
in2 = 1 ;
#2000;
in2 = 0 ;
#1000;
in2 = 1 ;
#2600;
in2 = 0 ;
#1300;
in2 = 1 ;
#2_000_000_000; //按键稳定松开
//模拟出按键的抖动过程 按下抖动
#(`shizhong*10 + 1);
in2 = 0 ;
#1000;
in2 = 1 ;
#2000;
in2 = 0 ;
#1400;
in2 = 1 ;
#2600;
in2 = 0 ;
#1300;
in2 = 1 ;
#200;
in2 = 0 ;
#2_000_000_000; //按键稳定
//释放时抖动
in2 = 1 ;
#2000;
in2 = 0 ;
#1000;
in2 = 1 ;
#2000;
in2 = 0 ;
#1000;
in2 = 1 ;
#2600;
in2 = 0 ;
#1300;
in2 = 1 ;
#2_000_000_000; //按键稳定松开
$stop;
end
endmodule