计数器的原理及VHDL实现
计数器: 计数器用来计数一个随机发生或者有规律发生的事件(在电路中表示为脉冲)。
模: 计数器所能记忆的最大脉冲个数称作该计数器的“模”,或者可以说是计数器所能表示的状态总数。模N的计数器即N进制计数器。
容量: 计数器所能表示的最大数值称为计数器的容量。
应用: 时序发生器、时间分配器、分频器、指令计数等。
实现:
(a) EDA综合工具:在设计中使用“+”就可以让综合工具生成一个计数器,但是综合工具往往对较大(超过 8 位)的计数器综合效果不好。
(b) FPGA 的计数器宏单元。适用于关键路径用较大的计数器。
带有使能信号、置数、异步复位的 8 位加计数器的例子实现
异步复位: 复位端出现低电平,触发器立即置零,不受时钟信号控制。
同步复位: 复位端出现低电平,要等时钟信号才能置零。
同步置数: 时钟信号到达才能置数。
异步置数: 无需等待时钟脉冲。
code
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.std_logic_unsigned.all;
use IEEE.std_logic_arith.all;
entity counter8 is
port (clk, en, n_rst, load : in std_logic;
data: in std_logic_vector (7 downto 0); --8位预置数
count : out std_logic_vector (7 downto 0)); --8位计数值输出
end counter8;
architecture Behavioral of counter8 is
signal cntin: std_logic_vector (7 downto 0);
signal cnt: std_logic_vector (7 downto 0);
begin
process (en, cnt, load, data)
begin
cntin <= cnt; --保持原状态
if (load = '1') then
cntin <= data ; --置数
elsif (en = '1') then
cntin <= cnt + '1'; --计数值加一
end if;
end process;
process (clk, n_rst)
begin
if (n_rst = '0') then --复位
cnt <= (others => '0'); --cnt均赋值为0
elsif (clk'event and clk = '1') then --上升沿使能
cnt <= cntin;
end if;
end process;
count <= cnt;
end Behavioral;
仿真
参考资料:
[1] https://blog.csdn.net/weixin_44378835/article/details/106242621.
[2] 航天器FPGA硬件描述语言设计指南. 2008.04.