share cho các tập viết phần cứng bằng vhdl như mình. Đã test thành công trên kit DE2-115
Bài toán như sau:
1. đếm từ 0 đến 5p.
2. hiển thị giá trị lên LED 7 đoạn
CODE :
Bài toán như sau:
1. đếm từ 0 đến 5p.
2. hiển thị giá trị lên LED 7 đoạn
CODE :
-- Thiet ke dong ho dem den 5p
-- hien thi gia tri len 4 LED 7 doan tren KIT DE2-115
-- Разработан: Н. Д. Ч.
-- data: 17/06/2013
Library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity STOP_WATCH_5P is
port( clk: in std_logic; -- xung clock 50MHz pin Y2
clr, go: in std_logic; -- xoa hoac cho phep bat dau dem giay -- SW1, SW0
d3,d2,d1,d0: buffer std_logic_vector(3 downto 0); --d3 = kim phut, d2 chuc giay, d1 giay, d0 0.1s --gan vao pin LEDR14..0
HEX3, HEX2, HEX1, HEX0: out std_logic_vector(0 to 6) -- hien thi gia tri tuong ung cua d3 d2 d1 d0
);
end STOP_WATCH_5P;
architecture arch of STOP_WATCH_5P is
constant Divider: integer := 5000_000;
signal d3_reg, d2_reg, d1_reg, d0_reg: std_logic_vector(3 downto 0):=(others => '0');
signal d3_next, d2_next, d1_next, d0_next: std_logic_vector(3 downto 0):= (others => '0');
signal ms_tick: std_logic;
begin
process(clk, go, clr) -- dem den 5tr thi tang 0.1s
variable count : integer := 0;
begin
if clr = '1' then
count := 0;
elsif rising_edge(clk) then
d3_reg <= d3_next;
d2_reg <= d2_next;
d1_reg <= d1_next;
d0_reg <= d0_next;
if go = '1' then
count := count + 1;
if count = divider then -- dem den 5tr = x"4C4B40"
ms_tick <= '1';
count := 0;
else
ms_tick <= '0';
end if;
end if;
end if;
end process;
process(d0_reg, d1_reg, d2_reg, d3_reg, ms_tick, clr)
begin
d0_next <= d0_reg;
d1_next <= d1_reg;
d2_next <= d2_reg;
d3_next <= d3_reg;
if clr = '1' then
d0_next <= (others => '0');
d1_next <= (others => '0');
d2_next <= (others => '0');
d3_next <= (others => '0');
elsif ms_tick = '1' then
if d0_reg /= "1001" then
d0_next <= d0_reg + 1;
else
d0_next <= "0000";
if d1_reg /= "1001" then
d1_next <= d1_reg + 1;
else
d1_next <= "0000";
if d2_reg /= "0101" then
d2_next <= d2_reg + 1;
else
d2_next <= "0000";
if d3_reg /= "0100" then
d3_next <= d3_reg + 1;
else
d3_next <= "0000";
end if;
end if;
end if;
end if;
end if;
end process;
d0 <= d0_reg;
d1 <= d1_reg;
d2 <= d2_reg;
d3 <= d3_reg;
with d3 select
HEX3 <= "0000001" when "0000",-- so 0
"1001111" when "0001",--1
"0010010" when "0010",--2
"0000110" when "0011",--3
"1001100" when "0100",--4
"0100100" when "0101",--5
"0100000" when "0110",--6
"0001111" when "0111",--7
"0000000" when "1000",--8
"0000100" when "1001",--9
"1111111" when others; -- LED toi
with d2 select
HEX2 <= "0000001" when "0000",-- so 0
"1001111" when "0001",--1
"0010010" when "0010",--2
"0000110" when "0011",--3
"1001100" when "0100",--4
"0100100" when "0101",--5
"0100000" when "0110",--6
"0001111" when "0111",--7
"0000000" when "1000",--8
"0000100" when "1001",--9
"1111111" when others; -- LED toi
with d1 select
HEX1 <= "0000001" when "0000",-- so 0
"1001111" when "0001",--1
"0010010" when "0010",--2
"0000110" when "0011",--3
"1001100" when "0100",--4
"0100100" when "0101",--5
"0100000" when "0110",--6
"0001111" when "0111",--7
"0000000" when "1000",--8
"0000100" when "1001",--9
"1111111" when others; -- LED toi
with d0 select
HEX0 <= "0000001" when "0000",-- so 0
"1001111" when "0001",--1
"0010010" when "0010",--2
"0000110" when "0011",--3
"1001100" when "0100",--4
"0100100" when "0101",--5
"0100000" when "0110",--6
"0001111" when "0111",--7
"0000000" when "1000",--8
"0000100" when "1001",--9
"1111111" when others; -- LED toi
end arch;
-- hien thi gia tri len 4 LED 7 doan tren KIT DE2-115
-- Разработан: Н. Д. Ч.
-- data: 17/06/2013
Library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity STOP_WATCH_5P is
port( clk: in std_logic; -- xung clock 50MHz pin Y2
clr, go: in std_logic; -- xoa hoac cho phep bat dau dem giay -- SW1, SW0
d3,d2,d1,d0: buffer std_logic_vector(3 downto 0); --d3 = kim phut, d2 chuc giay, d1 giay, d0 0.1s --gan vao pin LEDR14..0
HEX3, HEX2, HEX1, HEX0: out std_logic_vector(0 to 6) -- hien thi gia tri tuong ung cua d3 d2 d1 d0
);
end STOP_WATCH_5P;
architecture arch of STOP_WATCH_5P is
constant Divider: integer := 5000_000;
signal d3_reg, d2_reg, d1_reg, d0_reg: std_logic_vector(3 downto 0):=(others => '0');
signal d3_next, d2_next, d1_next, d0_next: std_logic_vector(3 downto 0):= (others => '0');
signal ms_tick: std_logic;
begin
process(clk, go, clr) -- dem den 5tr thi tang 0.1s
variable count : integer := 0;
begin
if clr = '1' then
count := 0;
elsif rising_edge(clk) then
d3_reg <= d3_next;
d2_reg <= d2_next;
d1_reg <= d1_next;
d0_reg <= d0_next;
if go = '1' then
count := count + 1;
if count = divider then -- dem den 5tr = x"4C4B40"
ms_tick <= '1';
count := 0;
else
ms_tick <= '0';
end if;
end if;
end if;
end process;
process(d0_reg, d1_reg, d2_reg, d3_reg, ms_tick, clr)
begin
d0_next <= d0_reg;
d1_next <= d1_reg;
d2_next <= d2_reg;
d3_next <= d3_reg;
if clr = '1' then
d0_next <= (others => '0');
d1_next <= (others => '0');
d2_next <= (others => '0');
d3_next <= (others => '0');
elsif ms_tick = '1' then
if d0_reg /= "1001" then
d0_next <= d0_reg + 1;
else
d0_next <= "0000";
if d1_reg /= "1001" then
d1_next <= d1_reg + 1;
else
d1_next <= "0000";
if d2_reg /= "0101" then
d2_next <= d2_reg + 1;
else
d2_next <= "0000";
if d3_reg /= "0100" then
d3_next <= d3_reg + 1;
else
d3_next <= "0000";
end if;
end if;
end if;
end if;
end if;
end process;
d0 <= d0_reg;
d1 <= d1_reg;
d2 <= d2_reg;
d3 <= d3_reg;
with d3 select
HEX3 <= "0000001" when "0000",-- so 0
"1001111" when "0001",--1
"0010010" when "0010",--2
"0000110" when "0011",--3
"1001100" when "0100",--4
"0100100" when "0101",--5
"0100000" when "0110",--6
"0001111" when "0111",--7
"0000000" when "1000",--8
"0000100" when "1001",--9
"1111111" when others; -- LED toi
with d2 select
HEX2 <= "0000001" when "0000",-- so 0
"1001111" when "0001",--1
"0010010" when "0010",--2
"0000110" when "0011",--3
"1001100" when "0100",--4
"0100100" when "0101",--5
"0100000" when "0110",--6
"0001111" when "0111",--7
"0000000" when "1000",--8
"0000100" when "1001",--9
"1111111" when others; -- LED toi
with d1 select
HEX1 <= "0000001" when "0000",-- so 0
"1001111" when "0001",--1
"0010010" when "0010",--2
"0000110" when "0011",--3
"1001100" when "0100",--4
"0100100" when "0101",--5
"0100000" when "0110",--6
"0001111" when "0111",--7
"0000000" when "1000",--8
"0000100" when "1001",--9
"1111111" when others; -- LED toi
with d0 select
HEX0 <= "0000001" when "0000",-- so 0
"1001111" when "0001",--1
"0010010" when "0010",--2
"0000110" when "0011",--3
"1001100" when "0100",--4
"0100100" when "0101",--5
"0100000" when "0110",--6
"0001111" when "0111",--7
"0000000" when "1000",--8
"0000100" when "1001",--9
"1111111" when others; -- LED toi
end arch;
Comment