【问题标题】:VHDL edge detectionVHDL边缘检测
【发布时间】:2013-07-02 15:12:48
【问题描述】:

我想检测串行数据信号 (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; 

【问题讨论】:

    标签: vhdl


    【解决方案1】:

    给你

    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;
    

    【讨论】:

    • 请注意,该页面上提供的解决方案用于检测上升沿或下降沿,但不能同时检测两者。
    • 感谢您提供密码路路通。我想检测上升沿和下降沿。当我实现您建议的代码时,din_delayed1 与 din 相同,因此没有变化。
    • din_delayed1和din是什么意思?它们相隔 1 个时钟周期。
    • 感谢您的帮助。当我将代码修改为上面提到的代码时,我能够检测到边缘。
    • @TomiJunnila 您如何修改上面的代码以使其适用于两种情况?
    【解决方案2】:

    您必须使用组合过程来检测差异,而不会产生额外的时钟周期延迟。 (您仍然需要一个寄存器来延迟输入。)

    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;
    

    【讨论】:

    • 不一定要在组合逻辑中完成。他可以在流程声明中使用if din_reg /= din
    • 您没有指定您的意思是哪种进程,但如果他在时钟进程中执行if din_reg /= din,则会导致输出在上升沿后一个时钟周期处于活动状态。如果过程没有计时,它是组合的,基本上等于我使用的语句。
    • 这取决于如何使用“更改”信号。如果在另一个时钟进程中读取它,则时间将与您在时钟进程中使用if din_reg /= din 相同。
    • 感谢您的回复。如果我实现上面的代码,那么变化一直是“0”。
    • 你能得到clk_50mhzdindin_regchange的波形图吗? change 不应保持“0”,除非 din 上没有边。
    猜你喜欢
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 2021-03-01
    • 2016-09-12
    • 1970-01-01
    • 2017-01-02
    • 2010-12-01
    相关资源
    最近更新 更多