use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity countern is
generic (N: integer:=24);
port
(
clk:in std_logic;
reset:in std_logic;---------------------------复位端
enable:in std_logic;--------------------------使能端
q:out integer range 0 to N-1
);
end entity countern;
architecture bhv of countern is
begin
process (clk)
variable cnt : integer range 0 to 23;
begin
if reset = '1' then cnt:= 0;--reset为1时计数器复位
elsif enable = '1'then--使能端为1时计数器正常工作
if(clk'event and clk='1') then--时钟上升沿到来时计数
if(cnt<23) then--加法计数
cnt:= cnt+1;
else
cnt := 0;
end if;
end if;
end if;
q <= cnt; ----输出计数值
end process;
end bhv;
仿真波形: