پروژه شمارنده و نمایشگر ۷ سگمنتی در VHDL


در این پروژه با استفاده از زبان VHDL، یک شمارنده ۴ بیتی طراحی می‌کنیم که از عدد ۰ تا ۹ شمارش می‌کند و سپس مقدار شمارش‌شده را روی یک نمایشگر ۷ سگمنتی نشان می‌دهد.

۱. ماژول شمارنده

این شمارنده در هر لبه‌ی بالا رونده کلاک یک واحد به مقدارش اضافه می‌کند و بعد از رسیدن به عدد ۹، مجدداً به صفر باز می‌گردد.


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.NUMERIC_STD.ALL;

entity counter_4bit_mod10 is
 Port (
 clk : in STD_LOGIC;
 reset : in STD_LOGIC;
 count_out : out STD_LOGIC_VECTOR (3 downto 0)
 );
end counter_4bit_mod10;

architecture Behavioral of counter_4bit_mod10 is
 signal count_reg : unsigned(3 downto 0) := (others => '0');
begin
 process(clk)
 begin
 if rising_edge(clk) then
 if reset = '1' then
 count_reg <= (others => '0');
 elsif count_reg = "1001" then
 count_reg <= (others => '0');
 else
 count_reg <= count_reg + 1;
 end if;
 end if;
 end process;

 count_out <= std_logic_vector(count_reg);
end Behavioral;
 

۲. مبدل BCD به ۷ سگمنت

این ماژول عدد ورودی بین ۰ تا ۹ را گرفته و معادل آن را برای روشن کردن سگمنت‌های نمایشگر تولید می‌کند (active-low).


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity bcd_to_7seg is
 Port (
 bcd_in : in STD_LOGIC_VECTOR (3 downto 0);
 seg_out : out STD_LOGIC_VECTOR (6 downto 0)
 );
end bcd_to_7seg;

architecture Behavioral of bcd_to_7seg is
begin
 process(bcd_in)
 begin
 case bcd_in is
 when "0000" => seg_out <= "0000001"; -- 0
 when "0001" => seg_out <= "1001111"; -- 1
 when "0010" => seg_out <= "0010010"; -- 2
 when "0011" => seg_out <= "0000110"; -- 3
 when "0100" => seg_out <= "1001100"; -- 4
 when "0101" => seg_out <= "0100100"; -- 5
 when "0110" => seg_out <= "0100000"; -- 6
 when "0111" => seg_out <= "0001111"; -- 7
 when "1000" => seg_out <= "0000000"; -- 8
 when "1001" => seg_out <= "0000100"; -- 9
 when others => seg_out <= "1111111"; -- خاموش
 end case;
 end process;
end Behavioral;
 

۳. تاپ‌ماژول

این ماژول دو بخش قبلی را به هم متصل کرده و یک سیستم شمارنده کامل با خروجی برای ۷ سگمنت می‌سازد.


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity counter_7seg_top is
 Port (
 clk : in STD_LOGIC;
 reset : in STD_LOGIC;
 segments: out STD_LOGIC_VECTOR (6 downto 0)
 );
end counter_7seg_top;

architecture Structural of counter_7seg_top is
 signal count_val : STD_LOGIC_VECTOR (3 downto 0);
begin

 U1: entity work.counter_4bit_mod10
 port map (
 clk => clk,
 reset => reset,
 count_out => count_val
 );

 U2: entity work.bcd_to_7seg
 port map (
 bcd_in => count_val,
 seg_out => segments
 );

end Structural;
 

۴. تست‌بنچ

در این تست‌بنچ، سیگنال‌های کلاک و ریست تولید شده و خروجی ماژول نهایی بررسی می‌شود.


library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity tb_counter_7seg is
end tb_counter_7seg;

architecture behavior of tb_counter_7seg is

 component counter_7seg_top
 Port (
 clk : in STD_LOGIC;
 reset : in STD_LOGIC;
 segments : out STD_LOGIC_VECTOR (6 downto 0)
 );
 end component;

 signal clk_tb : STD_LOGIC := '0';
 signal reset_tb : STD_LOGIC := '0';
 signal segments_tb: STD_LOGIC_VECTOR(6 downto 0);

 constant clk_period : time := 10 ns;

begin

 uut: counter_7seg_top
 port map (
 clk => clk_tb,
 reset => reset_tb,
 segments => segments_tb
 );

 clk_process : process
 begin
 while true loop
 clk_tb <= '0';
 wait for clk_period/2;
 clk_tb <= '1';
 wait for clk_period/2;
 end loop;
 end process;

 stim_proc: process
 begin
 reset_tb <= '1';
 wait for 20 ns;
 reset_tb <= '0';

 wait for 200 ns;

 wait;
 end process;

end behavior;