📄 counter.vhd
字号:
LIBRARY IEEE;
use IEEE.std_logic_1164.all;
use IEEE.std_logic_arith.all;
use IEEE.std_logic_unsigned.all;
ENTITY counter IS
PORT
( CNT_EN :in STD_LOGIC; -- 计数使能端
D :in STD_LOGIC_VECTOR(6 downto 0);
CNT_LD :in STD_LOGIC; -- 同步置数端
CNT_CLR :in STD_LOGIC; -- 异步复位端
CLK :in STD_LOGIC;
Q :out STD_LOGIC_VECTOR(6 downto 0);
CO:out STD_LOGIC); -- 进位输出端
end counter;
ARCHITECTURE rtl OF counter IS
SIGNAL Q_tmp : positive; -- 计数值输出过渡信号
SIGNAL CO_tmp: STD_LOGIC; -- 计数器进位过渡信号,
CONSTANT num_bit : positive :=7; -- 设置计数器位数num_bit = 7 位
CONSTANT N : positive := 99; -- 设置计数器模数N = 99
begin
process(clk,cnt_clr)
begin
if cnt_en='1' then
--q_tmp<=0;
co_tmp<='0';
else
if (clk'event and clk='1') then --时钟上升沿触发
if cnt_ld='1' then -- 计数器置位
q_tmp<=CONV_INTEGER(D); -- 二进制位向量转化为整数
co_tmp<='0';
else
if cnt_en='1' then -- 计数允许
if q_tmp=(N-1) then -- N-1为计数器最大容量
--q_tmp<=0;
co_tmp<='0';
else
if q_tmp=(N-2) then --最大容量时产生进位信号CO ='1
q_tmp<=q_tmp+1;
co_tmp<='1';
else
q_tmp<=q_tmp+1; -- 计数器加1
co_tmp<='0';
end if;
end if;
end if; -- end CNT_EN ='1'
end if; -- end CNT_LD ='1'
end if; -- end clk ='1'
end if; -- end clr ='1'
q<=CONV_std_logic_vector(q_tmp,num_bit); -- 将整数转化为二进制位向量输出
end process;
process(cnt_en,co_tmp) -- 产生进位信号
begin
co<=cnt_en and co_tmp;
end process;
end rtl;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -