VHDL边缘检测

时间:2013-07-02 15:12:49

标签: vhdl

我想检测串行数据信号(din)的边缘。我已经在VHDL中编写了以下代码,该代码运行成功,但是在一个时钟周期延迟时检测到边沿,即在每个边沿处产生一个clk_50mhz周期延迟的变化输出。谁能帮助我毫不拖延地检测边缘。谢谢。

 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 个答案:

答案 0 :(得分:2)

你去吧

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;

答案 1 :(得分: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;