我需要创建一个计数器,该计数器的频率为1Hz,它具有3个异步元素:crementMinutes,incrementSeconds和Reset(从anding Minutes and Seconds获得。我的代码如下:
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
entity Counter is
Port ( Clock : in STD_LOGIC; --Clock 1Hz
ECU : in STD_LOGIC;--Enable Count Up
ECD : in STD_LOGIC;--Enable Count Down
Num : out STD_LOGIC_VECTOR (15 downto 0);--Output displayed on BCD7Seg
incS : in STD_LOGIC;--increment seconds
incM : in STD_LOGIC;--increment minutes
Reset : in STD_LOGIC);--reset( and betweeen seconds and minutes)
end Counter;
architecture Behavioral of Counter is
signal count : STD_LOGIC_VECTOR(15 downto 0):= x"0000";
signal lastb_S: STD_LOGIC;--previous state of pushbutton S used for edge detection
signal lastb_M: STD_LOGIC;--previous state of pushbutton M used for edge detection
begin
Num <= count;
process(Clock,Reset,incS,incM)
begin
lastb_S <= incS;
lastb_M <= incM;
if(Reset = '1') then
count <= x"0000";
else
if(incS = '1' and lastb_S = '0') then
if(count(3 downto 0) = 9) then
count(3 downto 0) <= x"0";
count(7 downto 4) <= count(7 downto 4) + 1;
if(count(7 downto 4) = 5) then
count(7 downto 0) <= x"00";
end if;
else
count(3 downto 0) <= count(3 downto 0) + 1;
end if;
elsif(incM = '1' and lastb_M = '0') then
if(count(11 downto 8) = 9) then
count(11 downto 8) <= x"0";
count(15 downto 12) <= count(15 downto 12) + 1;
if(count(15 downto 12) = 9) then
count(15 downto 8) <= x"00";
end if;
else
count(11 downto 8) <= count(11 downto 8) + 1;
end if;
elsif(rising_edge(Clock)) then
if(ECU = '1') then
if(count(3 downto 0) = 9) then
count(3 downto 0) <= X"0";
count(7 downto 4) <= count(7 downto 4) + 1;
if(count(7 downto 4) = 5) then
count(7 downto 4) <= x"0";
count(11 downto 8) <= count(11 downto 8) + 1;
if(count(11 downto 8) = 9) then
count(11 downto 8) <= x"0";
count(15 downto 12) <= count(15 downto 12) + 1;
if(count(15 downto 12) = 9) then
count(15 downto 12) <= x"0";
end if;
end if;
end if;
else
count(3 downto 0) <= count(3 downto 0) + 1;
end if;
elsif(ECD = '1') then
if(count(3 downto 0) = 0) then
if(count(15 downto 4) /= 0) then
count(3 downto 0) <= x"9";
count(7 downto 4) <= count(7 downto 4) - 1;
if(count(7 downto 4) = 0) then
if(count(15 downto 8) /= 0) then
count(7 downto 4) <= x"5";
count(11 downto 8) <= count(11 downto 8) - 1;
if(count(11 downto 8) = 0) then
if(count(15 downto 12) /= 0) then
count(11 downto 8) <= x"9";
count(15 downto 12) <= count(15 downto 12) - 1;
end if;
end if;
end if;
end if;
end if;
else
count(3 downto 0) <= count(3 downto 0) - 1;
end if;
end if;
end if;
end if;
end process;
end Behavioral;
我在Basys 3上尝试了该代码,增量按钮不起作用。它可以正常计数并且可以重置。