基本原理很简单,把键盘的列扫描信号作为程序输出信号,再次基础上再进行行信号行扫描确定是哪一个按键,再对应用case-when语句设定7段数码管显示出按键的信息,如按“1”键气短数码管显示“1”,按“A”键显示“A”。
——————————————————————————————
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY dd IS
PORT (
clk : IN std_logic;
lie : OUT std_logic_vector(3 DOWNTO 0);
hang : IN std_logic_vector(3 DOWNTO 0);
outl : OUT std_logic_vector(7 DOWNTO 0);
outs : OUT std_logic_vector(5 downto 0));
END dd;
ARCHITECTURE arch OF dd IS
SIGNAL scan_key : std_logic_vector(3 DOWNTO 0);
SIGNAL div_cnt : std_logic_vector(3 DOWNTO 0):="0000";
SIGNAL key_code : std_logic_vector(3 DOWNTO 0);
SIGNAL dyp_tmp : std_logic_vector(7 DOWNTO 1);
BEGIN
process(clk)
begin
if(clk'event and clk='1')then
CASE div_cnt(2 downto 1) IS
WHEN "00"=> scan_key<="1110";
WHEN "01"=> scan_key<="1101";
WHEN "10"=> scan_key<="1011";
WHEN "11"=> scan_key<="0111";
scan_key<=lie;
end case;
end if;
end process;
--process(clk)
process(scan_key)
begin
if(clk'event and clk='1')then
case scan_key IS
WHEN "1110" =>
CASE hang IS
WHEN "1110" =>
key_code <= "0000";
WHEN "1101" =>
key_code <= "0001";
WHEN "1011" =>
key_code <= "0010";
WHEN "0111" =>
key_code <= "0011";
WHEN OTHERS =>
NULL;
END CASE;
WHEN "1101" =>
CASE column IS
WHEN "1110" =>
key_code <= "0100";
WHEN "1101" =>
key_code <= "0101";
WHEN "1011" =>
key_code <= "0110";
WHEN "0111" =>
key_code <= "0111";
WHEN OTHERS =>
NULL;
END CASE;
WHEN "1011" =>
CASE column IS
WHEN "1110" =>
key_code <= "1000";
WHEN "1101" =>
key_code <= "1001";
WHEN "1011" =>
key_code <= "1010";
WHEN "0111" =>
key_code <= "1011";
WHEN OTHERS =>
NULL;
END CASE;
WHEN "0111" =>
CASE column IS
WHEN "1110" =>
key_code <= "1100";
WHEN "1101" =>
key_code <= "1101";
WHEN "1011" =>
key_code <= "1110";
WHEN "0111" =>
key_code <= "1111";
WHEN OTHERS =>
NULL;
END CASE;
WHEN OTHERS =>
key_code <= "1111";
END CASE;
END IF;
END PROCESS;
process(key_code)
begin
case scan_code is
when"0000"=>outl<="11111100";
when"0001"=>outl<="01100000";
when"0010"=>outl<="11011010";
when"0011"=>outl<="11110010";
when"0100"=>outl<="11100110";--"11110010";
when"0101"=>outl<="10110110";
when"0110"=>outl<="10111110";--6
when"0111"=>outl<="11100000";
when"1000"=>outl<="11111110";
when"1001"=>outl<="11110110";
when"1010"=>outl<="11101110";
when"1011"=>outl<="00111100";
when"1100"=>outl<="10011100";
when"1101"=>outl<="01111100";
when"1110"=>outl<="10011110";
when"1111"=>outl<="10001110";
end case;
end process;