library ieee;
use ieee.std_logic_1164.all;
entity key_scan is
port (
clk : in std_logic;
keyin : in std_logic_vector(3 downto 0); --4*4键盘扫描4输入
keyout : out std_logic_vector(3 downto 0); --4*4键盘扫描4输出
numb : out std_logic_vector(6 downto 0) --输出键值,
);
end key_scan;
architecture rtl of key_scan is
signal code : std_logic_vector(7 downto 0);
signal countA : integer range 0 to 3; --以计数的方式产生扫描信号
signal scn : std_logic_vector(3 downto 0);
begin
code <= scn&keyin;
keyout <= scn;
process(clk)
begin
if clk 'event and clk = '1' then --产生扫描信号
if countA = 3 then
countA <= 0;
else
countA <= countA + 1;
end if;
case countA is
when 0 => scn <= "1110";
when 1 => scn <= "1101";
when 2 => scn <= "1011";
when 3 => scn <= "0111";
when others => scn <= "1111";
end case;
end if;
end process;
process(clk)
begin
if clk 'event and clk = '0' then -- 上升沿产生扫描信号,下降沿读入键码
case code is
when "01111110" => numb <= "0111111" ; -- X"3F"'0
when "01111101" => numb <= "0000110" ; -- X"06"'1
when "01111011" => numb <= "1011011" ; -- X"5B"'2
when "01110111" => numb <= "1001111" ; -- X"4F"'3
when "10111110" => numb <= "1100110" ; -- X"66"'4
when "10111101" => numb <= "1101101" ; -- X"6D"'5
when "10111011" => numb <= "1111101" ; -- X"7D"'6
when "10110111" => numb <= "0000111" ; -- X"07"'7
when "11011110" => numb <= "1111111" ; -- X"7F"'8
when "11011101" => numb <= "1101111" ; -- X"6F"'9
when "11011011" => numb <= "1110111" ; -- X"77"'10
when "11010111" => numb <= "1111100" ; -- X"7C"'11
when "11101110" => numb <= "0111001" ; -- X"39"'12
when "11101101" => numb <= "1011110" ; -- X"5E"'13
when "11101011" => numb <= "1111001" ; -- X"79"'14
when "11100111" => numb <= "1110001" ; -- X"71"'15
when others => NULL;
end case;
end if;
end process;
end rtl;