| |
hdl;toolbar:false">library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.NUMERIC_STD.ALL; entity counter is Port ( clk : in STD_LOGIC; reset : in STD_LOGIC; count_out : out STD_LOGIC_VECTOR(3 downto 0)); end counter; architecture Behavioral of counter is signal count : unsigned(3 downto 0) := (others => '0'); -- 使用unsigned类型定义内部计数器变量 begin process(clk, reset) begin if reset = '1' then count <= (others => '0'); -- 复位操作使用unsigned类型直接赋值0 elsif clk'event and clk = '1' then -- 时钟上升沿触发计数操作 count <= count + 1; -- 使用unsigned类型进行加法操作,自动处理溢出问题 end if; count_out <= std_logic_vector(count); -- 将unsigned类型转换为std_logic_vector以匹配输出端口类型 end process; end Behavioral;