Urban said:
hi
how do i detect a state change of a signal in vhdl.
i want to detect if a signal changes from high to low or reverse.
is there a way so i can reset a counter for example?
but i only want to reset it when the change occures.
You can convert a rising input into a synchronous
strobe with a flop to watch the previous value
and gating for last_low AND now_high.
Here's a related example, counting rising edges.
-- Mike Treseler
-- rising edge counter example Tue Aug 31 22:58:37 2004 Mike Treseler
library ieee;
use ieee.std_logic_1164.all;
package edge_package is
procedure ck_rising(watch : in std_ulogic;
last : inout boolean;
strobe : out boolean);
end package edge_package;
package body edge_package is
procedure ck_rising(watch : in std_ulogic;
last : inout boolean;
strobe : out boolean)
is begin
strobe := watch = '1' and last;
last := watch = '0'; -- assign variable for next time
end procedure ck_rising;
end package body edge_package;
-------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
use work.edge_package.ck_rising;
entity edge_count is
port (clk : in std_ulogic;
rst : in std_ulogic;
edge : in std_ulogic;
count: out unsigned(15 downto 0)
);
end entity edge_count;
architecture synth of edge_count is
begin -- architecture synth
process (clk, rst) is
variable last_v : boolean;
variable strobe_v : boolean;
variable count_v : unsigned(count'range);
begin -- process
clked:if rst = '1' then
count <= (others => '0');
elsif rising_edge(clk) then
ck_rising(watch => edge,
last => last_v,
strobe => strobe_v);
if strobe_v then
count_v := count_v+1;
end if;
count <= count_v;
end if clked;
end process;
end architecture synth;