新聞中心

用FPGA邏輯消抖動

作者: 時間:2023-12-26 來源:電子森林 收藏

我們將一個開關(guān)連接到上,連接方式如下圖:機械開關(guān)的問題就是有抖動,每次按一下開關(guān),你會得到下面的信號:

本文引用地址:http://www.butianyuan.cn/article/202312/454265.htm

這種信號很少碰到,多數(shù)情況是下面的這種:

我們可以用的計數(shù)器來記錄按鍵的次數(shù),并通過數(shù)碼管顯示出來: 上電的時候,一起是好的:

如果按十次鍵,得到下面的結(jié)果:

顯然不對。

那如何解決呢? 一種方式是添加一個R/C濾波器,再跟一個施密特觸發(fā)器之后送給,當(dāng)然還有更簡單的方式,就是在FPGA內(nèi)部進行消抖動。 FPGA擅長簡單的運算,讓我們使用FPGA中的計數(shù)器來查看按下或釋放按鈕的時間。只有當(dāng)計數(shù)器達到最大值時,我們才確定按鈕已經(jīng)改變了狀態(tài)。

PB是按鈕信號(在本例中為低電平有效)。它可能包含毛刺,并且對任何時鐘都是異步的,所以它基本上是不可用的。我們將使PB與時鐘(本例中為20MHz)同步,然后創(chuàng)建三個無毛刺、與時鐘同步的按鈕輸出。每個輸出都將是高電平有效的,并指示按鈕的不同狀態(tài)(按鈕狀態(tài),剛剛按下,剛剛釋放)。

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 onelsebegin
    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

我們使用了一個16位計數(shù)器。如果使用20MHz的系統(tǒng)時鐘,則需要3ms才能達到最大值。從用戶的角度來看,3ms是很短暫的,但毛刺已經(jīng)消失了。根據(jù)你的按鈕的毛刺程度和你的系統(tǒng)時鐘速度,你可能需要調(diào)整計數(shù)器的寬度。



關(guān)鍵詞: FPGA 消除抖動

評論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉