2013-07-02 185 views
4

欲檢測所述串行數據信號(DIN)的邊緣。我已經寫下面的代碼在其被成功運行VHDL但具有一個時鐘週期的延遲被檢測的邊緣即改變輸出與在每個邊緣一個clk_50mhz週期延遲產生的。任何人都可以幫助我毫不遲疑地檢測邊緣。謝謝。VHDL邊緣檢測

process (clk_50mhz) 
begin 
     if clk_50mhz'event and clk_50mhz = '1' then 
      if (rst = '0') then 
       shift_reg <= (others => '0'); 
      else 
       shift_reg(1) <= shift_reg(0); 
       shift_reg(0) <= din;  
         end if;  
     end if; 
end process; 

    process (clk_50mhz) 
    begin 
     if clk_50mhz'event and clk_50mhz = '1' then 
      if rst = '0' then 
       change <= '0' ; 
      elsif(clk_enable_2mhz = '1') then 
       change <= shift_reg(0) xor shift_reg(1);      
      end if ; 
     end if ; 
    end process ; 

當我改變了我的代碼下面我能夠檢測邊緣

process (clk_50mhz) 
begin 
    if clk_50mhz'event and clk_50mhz = '1' then 
     if (RST = '0') then 
      shift_reg <= (others=>'0'); 
     else 
      shift_reg(1) <= shift_reg(0); 
      shift_reg(0) <= din;  
    end if;  
    end if; 
end process; 

change <= shift_reg(1) xor din; 

回答

2

我發現正是你需要的。

也許你應該尋找多一點:http://fpgacenter.com/examples/basic/edge_detector.php

編輯:

在這裏你去

library ieee; 
use ieee.std_logic_1164.all; 

entity double_edge_detector is 
    port ( 
     clk_50mhz : in std_logic; 
     rst   : in std_logic; 
     din   : in std_logic; 
     change  : out std_logic 
    ); 
end double_edge_detector; 

architecture bhv of double_edge_detector is 

signal din_delayed1 :std_logic; 

begin 
    process(clk_50mhz) 
    begin 
     if rising_edge(clk_50mhz) then 
      if rst = '1' then 
       din_delayed1 <= '0'; 
      else 
       din_delayed1 <= din; 
      end if; 
     end if; 

    end process; 

    change <= (din_delayed1 xor din); --rising or falling edge (0 -> 1 xor 1 -> 0) 


end bhv; 
+0

請注意,該頁面上提供的解決方案是用於檢測上升沿或下降沿,但不是兩者。 –

+0

謝謝你的代碼Passepartout。我想檢測上升沿和下降沿。當我實現你所建議的din_delayed1與din相同的代碼時,所以沒有改變。 – user24883

+0

你是什麼意思din_delayed1與din相同?它們相隔1個時鐘週期。 – Passepartout

1

你必須使用一個組合過程中檢測的差異,而不會產生額外的時鐘週期延遲。 (您仍然需要一個寄存器來延遲輸入。)

DELAY: process(clk_50mhz) 
begin 
    if clk_50mhz'event and clk_50mhz = '1' then 
     din_reg <= din; 
    end if; 
end process; 

change <= din xor din_reg; 
+0

它不一定必須在組合邏輯中完成。他可以在流程聲明中輸入'din_reg/= din'。 –

+2

你沒有說明你的意思是什麼類型的過程,但是如果他在時鐘進程中做了'如果din_reg/= din',那麼會導致輸出在上升沿之後的一個時鐘週期內激活。如果這個過程沒有時鐘,它是組合的,並且基本上與我使用的陳述相同。 –

+1

這取決於你的「改變」信號的使用方式。如果在另一個鐘控過程中讀取它,則時間將與在鍾控過程中使用'if din_reg/= din'的時間相同。 –