Học liệu

Thiết kế bộ đếm chẵn lẻ trên FPGA

  • 18/05/2019
  • Học liệu

Thiết kế 1 mạch đếm lên từ 00-150 có chế độ hoạt động chế độ đếm chẵn 0-2-4-6-...-150

library ieee;

use ieee.std_logic_1164.all;

use ieee.std_logic_unsigned.all;

entity baitap is

                port (clk, clr: in std_logic;

                                                up_dow: in std_logic;

                                                seg1, seg2, seg3: out std_logic_vector(6 downto 0));

end entity baitap;

 

architecture BEH of baitap is

 

                signal dem_xung: integer range 0 to 10000000 :=0;

                signal donvi: integer :=0;

                signal chuc: integer :=0;

                signal tram: integer :=0;

                signal dem: integer :=0;

                function giai_ma(so: in integer)

                                return std_logic_vector is variable seg_out: std_logic_vector( 6 downto 0);

                begin

                                case so is

                                                when 0 => seg_out :="1000000";--0

                                                when 1 => seg_out :="1111001";--1

                                                when 2 => seg_out :="0100100";--2

                                                when 3 => seg_out :="0110000";--3

                                                when 4 => seg_out :="0011001";--4

                                                when 5 => seg_out :="0010010";--5

                                                when 6 => seg_out :="0000010";--6

                                                when 7 => seg_out :="1111000";--7

                                                when 8 => seg_out :="0000000";--8

                                                when 9 => seg_out :="0010000";--9

                                                when others => seg_out :="1111111";

                                end case;

                                return std_logic_vector(seg_out);

                                end function giai_ma;

begin

                process(clk, clr)

                begin

                                if(clr='0') then

                                                donvi<=0;

                                                chuc<=0;

                                                tram<=0;

                                                dem<=0;

                                elsif rising_edge(clk) then

                                                if(dem_xung<20000000) then

                                                                dem_xung<=dem_xung +1;

                                                else

                                                                dem_xung<=0;

                                                end if;

                                               

                                                if(dem_xung=0) then

                                                                if(up_dow='1') then

                                                                                tram<=dem /100;

                                                                                chuc<= (dem / 10) mod 10;

                                                                                donvi<= dem mod 10 ;

                                                                                dem<=dem+2;

                                                                                if dem=150 then

                                                                                                dem<=0;

                                                                                               

                                                                                end if;

                                                                end if;

                                                end if;

                                end if;

                                seg1 <= giai_ma(donvi);

                                seg2 <= giai_ma(chuc);

                                seg3 <= giai_ma(tram);

                end process;

end beh;

Các tin khác