gpt4 book ai didi

verilog - 为什么在此 Verilog HDL 代码中使用两个触发器而不是一个?

转载 作者:行者123 更新时间:2023-12-02 16:59:14 25 4
gpt4 key购买 nike

此代码是一个按钮防抖器。但我不明白为什么有两个人字拖:

reg PB_sync_0;  always @(posedge clk) PB_sync_0 <= ~PB;  // invert PB to make PB_sync_0 active high
reg PB_sync_1; always @(posedge clk) PB_sync_1 <= PB_sync_0;

为什么这段代码的作者没有写这个?

reg PB_sync_1;  always @(posedge clk) PB_sync_1 <= ~PB;

完整代码如下:

module PushButton_Debouncer(
input clk,
input PB, // "PB" is the glitchy, asynchronous to clk, active low push-button signal

// from which we make three outputs, all synchronous to the clock
output reg PB_state, // 1 as long as the push-button is active (down)
output PB_down, // 1 for one clock cycle when the push-button goes down (i.e. just pushed)
output PB_up // 1 for one clock cycle when the push-button goes up (i.e. just released)
);

// First use two flip-flops to synchronize the PB signal the "clk" clock domain
reg PB_sync_0; always @(posedge clk) PB_sync_0 <= ~PB; // invert PB to make PB_sync_0 active high
reg PB_sync_1; always @(posedge clk) PB_sync_1 <= PB_sync_0;

// Next declare a 16-bits counter
reg [15:0] PB_cnt;

// When the push-button is pushed or released, we increment the counter
// The counter has to be maxed out before we decide that the push-button state has changed

wire PB_idle = (PB_state==PB_sync_1);
wire PB_cnt_max = &PB_cnt; // true when all bits of PB_cnt are 1's

always @(posedge clk)
if(PB_idle)
PB_cnt <= 0; // nothing's going on
else
begin
PB_cnt <= PB_cnt + 16'd1; // something's going on, increment the counter
if(PB_cnt_max) PB_state <= ~PB_state; // if the counter is maxed out, PB changed!
end

assign PB_down = ~PB_idle & PB_cnt_max & ~PB_state;
assign PB_up = ~PB_idle & PB_cnt_max & PB_state;
endmodule

谢谢!

最佳答案

该代码的作者使用 2 个触发器来将 PB 信号同步到 clk 域。正如他在评论中提到的"PB" is the glitchy, asynchronous to clk 。不同步时钟域转换上的信号可能会导致系统出现亚稳态,如工具引用 en.wikipedia.org/wiki/Metastability_in_electronics

关于verilog - 为什么在此 Verilog HDL 代码中使用两个触发器而不是一个?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24660371/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com