VHDL---基于分频器的加法计数器与LED显示
发布日期:2021-05-06 06:57:17 浏览次数:28 分类:精选文章

本文共 4251 字,大约阅读时间需要 14 分钟。

基于VHDL的加法计数器设计与实现

本项目旨在设计并实现一个基于VHDL的加法计数器,将加法计数器的结果通过3个LED输出,并通过修改分频参数来改变闪烁时长。本设计程序主要分为三部分,分别是顶层元件调用程序、通用偶数分频器和加法计数器。

1. 通用偶数分频器

通用偶数分频器用于将输入时钟信号分频为2倍的指定参数(默认为2)。该分频器采用标准的VHDL库ieee.std_logic_1164,并通过一个计数器实现分频功能。具体实现如下:

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity gen_div is
generic(div_param: integer := 1); -- 分频因子,分频为2*div_param,默认为2分频
port (
clk: in std_logic; -- 输入时钟信号
bclk: out std_logic; -- 分频输出信号
resetb: in std_logic -- 复位信号
);
end gen_div;
architecture behave of gen_div is
signal tmp: std_logic;
signal cnt: integer range 0 to div_param := 0; -- 计数寄存器
begin
process(clk, resetb)
begin
if resetb = '1' then -- 复位信号有效时,bclk始终为'0'
cnt <= 0;
tmp <= '0';
elsif rising_edge(clk) then -- 检查时钟的上升沿
cnt <= cnt + 1;
if cnt = div_param - 1 then -- 当计数器达到分频参数减一时
tmp <= not tmp; -- 取反输出信号
cnt <= 0; -- 重置计数器
end if;
end if;
end process;
bclk <= tmp; -- 输出分频信号
end behave;

2. 顶层调用文件

顶层调用文件负责整合各个模块的功能,并实现最终的LED显示。该文件主要包含分频器和加法计数器的调用,以及LED编码处理。具体实现如下:

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity led_adder is
port (
inCLK: in std_logic; -- 输入时钟信号
RST_in: in std_logic; -- 复位输入信号
EN_in: in std_logic; -- 启动输入信号
LOAD_in: in std_logic; -- 加载输入信号
LED: out std_logic_vector(2 downto 0) -- LED输出信号
);
end entity;
architecture Lin of led_adder is
signal clk_tmp: std_logic; -- 中继时钟信号
component gen_div is
generic(div_param: integer := 40000000); -- 设置分频参数为40,000,000,产生半秒脉冲
port (
clk_in: in std_logic; -- 输入时钟信号
bclk: out std_logic; -- 分频输出信号
resetb: in std_logic -- 复位输入信号
);
end component gen_div;
component CNT10 is
port (
CLK: in std_logic; -- 输入时钟信号
RST: in std_logic; -- 复位输入信号
EN: in std_logic; -- 启动输入信号
LOAD: in std_logic; -- 加载输入信号
DOUT: out std_logic_vector(3 downto 0) -- 数据输出信号
);
end component CNT10;
signal num: std_logic_vector(3 downto 0); -- 输入数据
signal data_in: std_logic_vector(3 downto 0); -- 数据输入信号
begin
-- 初始化分频器
gen_div port map (
clk_in => inCLK,
resetb => not RST_in,
bclk => clk_tmp
);
-- 初始化加法计数器
CNT10 port map (
CLK => clk_tmp,
RST => RST_in,
EN => EN_in,
LOAD => LOAD_in,
DOUT => num
);
-- 数据输入处理
data_in <= num;
end process;
-- LED编码处理
process(data_in)
begin
case data_in is
when "0000" =>
LED <= "000";
when "0001" =>
LED <= "001";
when "0010" =>
LED <= "010";
when "0011" =>
LED <= "011";
when "0100" =>
LED <= "100";
when "0101" =>
LED <= "101";
when "0110" =>
LED <= "110";
when "0111" =>
LED <= "111";
when others =>
LED <= "000";
end case;
end process;
end architecture;

3. 加法计数器

加法计数器负责接收输入数据并进行加法运算,最终输出结果。该计数器采用异位计数器结构,支持加法操作。具体实现如下:

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity CNT10 is
port (
CLK: in std_logic; -- 输入时钟信号
RST: in std_logic; -- 复位输入信号
EN: in std_logic; -- 启动输入信号
LOAD: in std_logic; -- 加载输入信号
DOUT: out std_logic_vector(3 downto 0) -- 数据输出信号
);
end entity;
architecture behav of CNT10 is
begin
if RST = '0' then
Q := (others => '0');
elsif rising_edge(CLK) then -- 检查时钟的上升沿
if EN = '1' then
if Q < 7 then
Q := Q + 1;
else
Q := (others => '0');
end if;
end if;
end if;
DOUT <= Q;
end architecture;
end behav;
上一篇:基于matlab的数字图像处理--图像拼接
下一篇:VHDL-边沿触发的加法计数器与七段数码管显示

发表评论

最新留言

关注你微信了!
[***.104.42.241]2025年04月30日 11时04分36秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章