//这是书上的一个消抖的例子
module key_debounce_3 (
clk_10m,
rst_n,
key_in,
key_out
);
input clk_10m,rst_n,key_in;
output key_out;
//-----------------------------------------
reg key_rst;
always @ (posedge clk_10m or negedge rst_n)
if(~rst_n)
key_rst <= 1'b1;
else
key_rst <= key_in;
reg key_rst_r;
always @ (posedge clk_10m or negedge rst_n)
if(~rst_n)
key_rst_r <= 1'b1;
else
key_rst_r <= key_rst;
wire key_an = key_rst_r & (~key_rst);
//--------------------------------------
reg [19:0] cnt;
always @ (posedge clk_10m or negedge rst_n)
if(~rst_n)
cnt <= 20'd0;
else if(key_an)
cnt <= 20'd0;
else
cnt <= cnt + 1'b1;
reg low_sw;
always @ (posedge clk_10m or negedge rst_n)
if(~rst_n)
low_sw <= 1'b1;
else if(cnt == 20'h2ffff)
low_sw <= key_in;
//---------------------------------------------
reg low_sw_r;
always @ (posedge clk_10m or negedge rst_n)
if(~rst_n)
low_sw_r <= 1'b1;
else
low_sw_r <= low_sw;
wire key_out = low_sw_r & (~low_sw);
endmodule
//key_in是按键输入,key_out是消抖后的输出。下面3个是寄存器,在复位时已经赋初值为1'b1了,可是采集的信号还为0;按理来说当key_in按下为0时,下面的寄存器也变成0,可恰恰相反却成了1。
下图是我signal tapii采集的信号
这到底是怎么回事啊???这问题困扰了我好久。。。