vhdl实现bcd计数器
时间: 2024-02-18 08:05:29 浏览: 27
好的,下面是一个简单的 VHDL 代码实现一个 BCD 计数器:
```vhdl
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity bcd_counter is
port(
clk : in std_logic;
rst : in std_logic;
count : out std_logic_vector(3 downto 0)
);
end bcd_counter;
architecture rtl of bcd_counter is
signal count_reg : unsigned(3 downto 0) := (others => '0');
begin
process(clk, rst)
begin
if rst = '1' then
count_reg <= (others => '0');
elsif rising_edge(clk) then
if count_reg = 9 then
count_reg <= (others => '0');
else
count_reg <= count_reg + 1;
end if;
end if;
end process;
count <= std_logic_vector(count_reg);
end rtl;
```
这是一个简单的 4 位 BCD 计数器,每秒钟会从 0 到 9 递增,当计数器的值达到 9 时,会从 0 开始重新计数。在这个例子中,我们使用了一个 unsigned 类型的寄存器来存储计数器的值,这样方便进行加法运算和比较运算。最后,将 unsigned 类型的计数器值转换为 std_logic_vector 类型,以供输出使用。