要求:
1.分频器的设计与实现。
输入为4Hz的时钟,输出为1Hz的时钟。
2.设计24秒倒计时器。
(1)输入为固定频率脉冲、使能端、复位端,输出为计时状态(用8421码表示)。
(2)用VHDL编程实现,QuartusⅡ下编译并仿真。
具体VHDL代码实现:
--------分频器(四分频)
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity clockpulse is
port(
CP:in std_logic; --假设输入的是4HZ的时钟
CO:buffer std_logic --要求输出的是1HZ的时钟
);
end entity clockpulse;
architecture isclockpulse of clockpulse is
signal Q : std_logic_vector(1 downto 0);
begin
process(CP , CO)
begin
if(CP'event and CP = '1') ---高电平有效
then CO <= Q(1); Q <= Q + '1';
if(Q = "11") then Q <= "00"; --清零
end if;
end if;
end process;
end isclockpulse;
----------计数器(25进制)
library ieee;
use ieee.std_logic_1164.ALL;
use ieee.std_logic_unsigned.all;
entity couter_25 is
port(
CP , RESET , EN : in std_logic;
CO: out std_logic;
Q : out std_logic_vector(4 downto 0)
);
end couter_25;
architecture iscouter_25 of couter_25 is
signal iq : std_logic_vector(4 downto 0) := (others => '0');
begin
process(CP , RESET , EN)
begin
if CP'event and CP = '1' then --上升沿触发
if RESET = '1' then --复位端高电平清零
iq <= "11000";
elsif EN = '1' then --使能端高电平有效
iq <= iq - '1';
else
iq <= iq;
end if;
end if;
end process;
Q <= iq;
CO <= '1' when (iq = "00000" and EN='1')
else '0';
end iscouter_25;
-----------编码器
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity decode_25 is
port(
D : in std_logic_vector(4 downto 0); ---输入的5位二进制
Q1 : out std_logic_vector(3 downto 0); ---8421BCD码的高位
Q2 : out std_logic_vector(3 downto 0) ---8421BXD码的低位
);
end entity decode_25;
architecture isdecode_25 of decode_25 is
signal f : std_logic_vector(4 downto 0);
begin
process(D)
begin
if(D >= "00000" and D <= "01001") then ---D是0到9的时候
Q1 <= "0000";
Q2 <= D(3 downto 0);
elsif(D <= "10011") then ---D是10到19的时候
Q1 <= "0001";
f <= (D - "01010");
Q2 <= f(3 downto 0);
elsif(D <= "11000") then ---D是20到24的时候
Q1 <= "0010";
f <= (D - "10100");
Q2 <= f(3 downto 0); ---这样就实现了0到24的二进制到8421BCD码的转换
end if;
end process;
end isdecode_25;
最后将这三个模块用连线的方法连接在一起就可以实现24秒倒计时器的功能啦!