📄 keydecoder.vhd
字号:
--按键译码
LIBRARY IEEE;
USE IEEE.std_logic_1164.ALL;
USE IEEE.std_logic_unsigned.ALL;
ENTITY keydecoder IS
PORT (keyin :IN STD_LOGIC_VECTOR(1 DOWNTO 0);
keydrv :IN STD_LOGIC_VECTOR(3 DOWNTO 0);
clk_scan :IN std_logic;
clk :IN std_logic;
freq: buffer std_logic_vector(1 downto 0));
END keydecoder;
ARCHITECTURE rtl OF keydecoder IS
SIGNAL temp : STD_LOGIC_VECTOR(5 DOWNTO 0);
SIGNAL temp_pressed:std_logic;--用于产生temp_pressed1
SIGNAL temp_pressed1:std_logic;--用于产生keypressed
SIGNAL keypressed :STD_LOGIC;
SIGNAL keyvalue : STD_LOGIC_VECTOR(3 DOWNTO 0);
SIGNAL q1,q2,q3,q4,q5,q6:std_logic;--6个寄存器,用于消抖和同步化keypressed_asy
SIGNAL cnt4:integer range 0 to 3;
BEGIN
temp<=keyin&keydrv;
--按键译码进程
PROCESS(temp)
begin
if(clk_scan'event and clk_scan='1') then
CASE temp IS
WHEN"010001"=>keyvalue<="0001";--1
temp_pressed<='1';
WHEN"010010"=>keyvalue<="0010";--2
temp_pressed<='1';
WHEN"010100"=>keyvalue<="0011";--3
temp_pressed<='1';
WHEN"011000"=>keyvalue<="0100";--4
temp_pressed<='1';
WHEN"100001"=>keyvalue<="0101";--5
temp_pressed<='1';
WHEN"100010"=>keyvalue<="0110";--6
temp_pressed<='1';
WHEN"100100"=>keyvalue<="0111";--7
temp_pressed<='1';
WHEN"101000"=>keyvalue<="1000";--8
temp_pressed<='1';
WHEN OTHERS =>temp_pressed<='0';
END CASE;
END IF;
END PROCESS;
process(clk_scan)--键盘消抖
begin
if(clk_scan'event and clk_scan='1') then
q1<=temp_pressed;
q2<=q1;
q3<=q2;
q4<=q3;
end if;
temp_pressed1<=q1 or q2 or q3 or q4;
end process;
process(clk)--同步化
begin
if(clk'event and clk='1') then
q5<=temp_pressed1;
q6<=q5;
end if;
keypressed<=q5 and not q6;
end process;
--前提条件:有键被按下时,keypressed产生一个高电平
--此进程用来调频
process(keypressed)
begin
if(keypressed'event and keypressed='1') then
if(keyvalue="1000") then--按下8键
if(cnt4=3) then
cnt4<=1;
else
cnt4<=cnt4+1;
end if;
else
cnt4<=cnt4;
end if;
end if;
end process;
process(cnt4)
begin
case cnt4 is
when 0=>freq<="00";
when 1=>freq<="01";
when 2=>freq<="10";
when 3=>freq<="11";
when others=>freq<="00";
end case;
end process;
end rtl;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -