Reputation: 41
Now I'm coding VHDL to make a one-shot timer module. But I don't know which code is right in two kind of code, the first or second. i used the testbench i see the different. What the right code for monostable (one-shot) ?
This is the first code:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
library UNISIM;
use UNISIM.VComponents.all;
entity oneshot is
port ( clk : in STD_LOGIC;
ce : in STD_LOGIC;
trigger : in STD_LOGIC;
delay : in STD_LOGIC_VECTOR (7 downto 0);
pulse : out STD_LOGIC :='0');
end oneshot;
architecture Behavioral of oneshot is
signal count: INTEGER range 0 to 255; -- count variable
begin
process (clk,delay,trigger)
begin
-- wait for trigger leading edge
if rising_edge(clk) then
if trigger = '1' then
count <= to_integer(unsigned(delay));
end if;
if count > 0 then
pulse <= '1';
count <= count - 1;
else
pulse <= '0';
end if;
end if;
end process;
end Behavioral;
This is second one:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;
library UNISIM;
use UNISIM.VComponents.all;
entity oneshot is
port ( clk : in STD_LOGIC;
ce : in STD_LOGIC;
trigger : in STD_LOGIC:='0';
delay : in STD_LOGIC_VECTOR (7 downto 0);
pulse : out STD_LOGIC :='0');
end oneshot;
architecture Behavioral of oneshot is
signal count: INTEGER range 0 to 255; -- count variable
begin
process (clk,delay,trigger)
begin
-- wait for trigger leading edge
if trigger = '1' then
count <= to_integer(unsigned(delay));
elsif rising_edge(clk) then
if count > 0 then
pulse <= '1';
count <= count - 1;
else
pulse <= '0';
end if;
end if;
end process;
end Behavioral;
Upvotes: 1
Views: 3929
Reputation: 16221
Both versions cannot be synthesized:
rising_edge
condition.In general the second implementation is the closest to a solution. You can improve the down-counter by counting towards -1
und using the signed
type. -1 can be identified by the MSB being '1'
. No need to compare n bits for all zero.
Further issues:
trigger
is missingflag
is not readunisim
is unused. flag
is unused.Upvotes: 2