[轉]FPGA的邊沿檢測

來源:互聯網
上載者:User

標籤:

以前我曾一度傻乎乎的使用

always @ (posedge signal)

這樣的代碼去檢測signal的上升沿,鬧出了很多問題。

當受實驗室的一同學指教後,再也不會傻乎乎的這樣幹了。當然,你看完下文也不會這樣幹了。

檢測上升沿的原理:使用高頻的時鐘對訊號進行採樣,因此要實現上升沿檢測,時鐘頻率至少要在訊號最高頻率的2倍以上,否則就可能出現漏檢測。具體請參見下面代碼。

module edge_check(clk, rst_n, signal, pos_edge, neg_edge, both_edge);input clk;input rst_n;input signal;output pos_edge;output neg_edge;output both_edge;reg sig_r0, sig_r1;  // 狀態寄存器always @ (posedge clk or negedge rst_n)    if (!rst_n) begin         sig_r0 <= 1‘b0;        sig_r1 <= 1‘b0;    end else begin         sig_r0 <= signal;        sig_r1 <= sig_r0;    end assign pos_edge = (~sig_r1) & (sig_r0);assign neg_edge = sig_r1 & (~sig_r0);   assign both_edge = sig_r1 ^ sig_r0;  // 雙邊沿檢測,或pos_edge|neg_edgeendmodule 

用Quartus II綜合布線之後的RTL視圖如下:

從RTL視圖中可以看出,電路是通過一個非同步複位的D觸發器實現的。

ModelSim的模擬視圖如下,從中可看出已檢測出上升和下降沿,但存在一個延時,這是因為使用了時鐘同步的檢測。

或者上面的Verilog代碼還可以換一種寫法,效率上差不了太多;

module edge_check(clk, rst_n, signal, pos_edge, neg_edge, both_edge);input clk;input rst_n;input signal;output pos_edge;output neg_edge;output both_edge;reg [1:0]sig_fifo;reg sig_r0, sig_r1;  // 狀態寄存器always @ (posedge clk or negedge rst_n)    if (!rst_n) begin         sig_fifo <= 2‘b0;    end else begin         sig_fifo <= {sig_fifo[0], signal};    end assign pos_edge = (sig_fifo == 2‘b01);assign neg_edge = (sig_fifo == 2‘b10);  assign both_edge = sig_fifo[0] ^ sig_fifo[1];  // 雙邊沿檢測,或pos_edge|neg_edgeendmodule 

產生的RTL視圖為

 

[轉]FPGA的邊沿檢測

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.