📄 keyboard.vhd
字号:
--***********************************************************
--********Filename:keyboard.vhd******************************
--********Origin date:3/15/2006******************************
--********Function:keyboard scan and LED display*************
--***********************************************************
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
library altera;
use altera.maxplus2.all;
--
--*******************************
entity keyboard is
port
(
clk_4M: in std_logic; --系统原始时钟脉冲(假设为4M)
key_in: in std_logic_vector(3 downto 0); --按键输入
clk_scan: out std_logic_vector(3 downto 0); --扫描序列
out_numb: out std_logic_vector(3 downto 0); --数字输出
a,b,c,d,e,f,g: out std_logic; --数码管显示输出
--flag_numb: out std_logic; --数字输出标志
clk_debounce: out std_logic --弹跳电路取样时钟脉冲
--clk_display: out std_logic_vector(1 downto 0) --显示器扫描信号
);
end keyboard;
--
--*********************************
architecture arc of keyboard is
component debouncing
port(d_in: in std_logic;
clk: in std_logic;
d_out: out std_logic);
end component;
signal clk: std_logic;
--电路工作时钟脉冲(从4M降频而来)
signal c_keyboard: std_logic_vector(1 downto 0);
signal c_debounce: std_logic;
--signal c_display: std_logic_vector(1 downto 0);
signal cc: std_logic_vector(3 downto 0);
signal n: std_logic_vector(3 downto 0);
--signal fn: std_logic;
signal sel: std_logic_vector(3 downto 0);
begin
--**********************************
--connection 管脚信号连接
out_numb<=n;
--flag_numb<=fn;
clk_debounce<=c_debounce;
--clk_display<=c_display;
c_debounce<=clk;
--
--***********************************
--scan signal generator 产生不同频率的信号
counter: block
signal q: std_logic_vector(19 downto 0);--free counter
signal s: std_logic_vector(1 downto 0); --00-01-10-11
signal sel:std_logic_vector(3 downto 0);--1110-1101-1011-0111
begin
process(clk_4M)
begin
if clk_4M'event and clk_4M='1' then
q<=q+1;
end if;
--The following 3 lines is used in hardware
--c_kyeboard <= q(18 downto 17);
--c_debounce <= q(14)
--c_display <= q(18 downto 17);
--simulating to observe the results
c_keyboard <= q(5 downto 4);
clk <= q(0);
--c_display <= q(5 downto 4);
end process;
sel<="1110" when c_keyboard = 0 else
"1101" when c_keyboard = 1 else
"1011" when c_keyboard = 2 else
"0111" when c_keyboard = 3 else
"1111";
clk_scan<=sel;
end block counter;
--
--************************************
--debounce program
debounce:block
begin
u1:debouncing port map
(
d_in => key_in(0),
d_out => cc(0),
clk => c_debounce
);
u2:debouncing port map
(
d_in => key_in(1),
d_out => cc(1),
clk => c_debounce
);
u3:debouncing port map
(
d_in => key_in(2),
d_out => cc(2),
clk => c_debounce
);
u4:debouncing port map
(
d_in => key_in(3),
d_out => cc(3),
clk => c_debounce
);
end block debounce;
--
--***************************************
--key decoder
key_decoder: block
signal z: std_logic_vector(5 downto 0); --key position
signal yout: std_logic_vector(6 downto 0);--LED value
begin
process(clk)
begin
z <= c_keyboard&cc;
if clk'event and clk='1' then
case z is
when "001110" => n <= "0000" ; yout <= "1111110";--0
when "001101" => n <= "0001" ; yout <= "0110000";--1
when "001011" => n <= "0010" ; yout <= "1101101";--2
when "000111" => n <= "0011" ; yout <= "1111001";--3
when "011110" => n <= "0100" ; yout <= "0110011";--4
when "011101" => n <= "0101" ; yout <= "1011011";--5
when "011011" => n <= "0110" ; yout <= "1011111";--6
when "010111" => n <= "0111" ; yout <= "1110000";--7
when "101110" => n <= "1000" ; yout <= "1111111";--8
when "101101" => n <= "1001" ; yout <= "1111011";--9
when "101011" => n <= "1010" ; yout <= "1110111";--a
when "100111" => n <= "1011" ; yout <= "0011111";--b
when "111110" => n <= "1100" ; yout <= "1001110";--c
when "111101" => n <= "1101" ; yout <= "0111101";--d
when "111011" => n <= "1110" ; yout <= "1001111";--e
when "110111" => n <= "1111" ; yout <= "1000111";--f
when others => yout <= "0000000";
end case;
end if;
end process;
a <= yout(6); b <= yout(5); c <= yout(4); d <= yout(3);
e <= yout(2); f <= yout(1); g <= yout(0);
--fn <= not (n(3) and n(2) and n(1) and n(0)); --产生数字按键标志
end block key_decoder;
end arc;
--**************The end******************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -