|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
-- Keyboard
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_ARITH.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY keyboard IS
PORT(
CLK_1K: IN STD_LOGIC; --系统原始时钟脉冲(1KHz)
KEY_IN: IN STD_LOGIC_VECTOR(3 DOWNTO 0); --按键输入
CLK_SCAN,CC: OUT STD_LOGIC_VECTOR(3 DOWNTO 0); --键盘扫描输出 KBY_OUT: OUT STD_LOGIC_VECTOR(9 DOWNTO 0)); --键盘数据输出
END ENTITY KEYBOARD;
ARCHITECTURE ART OF KEYBOARD IS
COMPONENT DEBOUNCING IS
PORT(
D_IN,CLK:IN STD_LOGIC;
D_OUT: OUT STD_LOGIC);
END COMPONENT DEBOUNCING; --其中DEBOUNCING是消抖电路
SIGNAL CLK:STD_LOGIC; --电路工作时钟脉冲
SIGNAL C_KEYBOARD:STD_LOGIC_VECTOR(1 DOWNTO 0); --键扫信号“00-01-10-11”寄存器
SIGNAL C_DEBOUNCE:STD_LOGIC; --去抖动电路时钟
SIGNAL C:STD_LOGIC_VECTOR(3 DOWNTO 0); --键盘输入去抖动后的寄存器
SIGNAL N,F:STD_LOGIC_VECTOR(3 DOWNTO 0); --数字、功能按键译码值的寄存器
SIGNAL FN,FF: STD_LOGIC; --数字、功能按键标志值数字、功能按键
SIGNAL SEL: STD_LOGIC_VECTOR(3 DOWNTO 0);
BEGIN
KBY_OUT <= FF & FN & F & N;
--扫描信号发生器
COUNTER: BLOCK IS
SIGNAL Q: STD_LOGIC_VECTOR(9 DOWNTO 0);
SIGNAL SEL: STD_LOGIC_VECTOR(3 DOWNTO 0);
BEGIN
PROCESS(CLK_1K,Q)
BEGIN
IF(CLK_1K'EVENT AND CLK_1K='1') THEN
Q <= Q + 1;
END IF;
C_DEBOUNCE <= Q(2);
C_KEYBOARD <= Q(9 DOWNTO 8);
CLK <= Q(0);
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;
--键盘去抖
DEBOUNUING :BLOCK IS
BEGIN
U1: DEBOUNCING PORT MAP (D_IN => NOT KEY_IN(0), CLK => C_DEBOUNCE, D_OUT => C(0));
U2: DEBOUNCING PORT MAP (D_IN => NOT KEY_IN(1), CLK => C_DEBOUNCE, D_OUT => C(1));
U3: DEBOUNCING PORT MAP (D_IN => NOT KEY_IN(2), CLK => C_DEBOUNCE, D_OUT => C(2));
U4: DEBOUNCING PORT MAP (D_IN => NOT KEY_IN(3), CLK => C_DEBOUNCE, D_OUT => C(3));
CC <= C;
END BLOCK DEBOUNUING;
--键盘译码
KEY_DECODER: BLOCK
SIGNAL Z: STD_LOGIC_VECTOR(5 DOWNTO 0); --按键位置
BEGIN
PROCESS(CLK,C_KEYBOARD,C)
BEGIN
Z <= C_KEYBOARD & C; --读入的编码与扫描结果拼接
IF CLK'EVENT AND CLK='1' THEN
CASE Z IS
WHEN "111011" => N <= "0000"; --按键 '0'
WHEN "000111" => N <= "0001"; --按键 '1'
WHEN "001011" => N <= "0010"; --按键 '2'
WHEN "001101" => N <= "0011"; --按键 '3'
WHEN "010111" => N <= "0100"; --按键 '4'
WHEN "011011" => N <= "0101"; --按键 '5'
WHEN "011101" => N <= "0110"; --按键 '6'
WHEN "100111" => N <= "0111"; --按键 '7'
WHEN "101011" => N <= "1000"; --按键 '8'
WHEN "101101" => N <= "1001"; --按键 '9'
WHEN OTHERS => N <= "1111";
END CASE;
END IF;
IF CLK'EVENT AND CLK = '1' THEN
CASE Z IS
WHEN "110111" => F <= "0001"; --按键 '*'
WHEN "111101" => F <= "0010"; --按键 '#'
WHEN "001110" => F <= "0011"; --按键 'F1'
WHEN "011110" => F <= "0100"; --按键 'F2'
WHEN "101110" => F <= "0101"; --按键 'F3'
WHEN "111110" => F <= "0110"; --按键 'F4'
WHEN OTHERS => F <= "1111";
END CASE;
END IF;
END PROCESS;
FN <= NOT(N(3) AND N(2) AND N(1) AND N(0));
FF <= NOT(F(3) AND F(2) AND F(1) AND F(0));
END BLOCK KEY_DECODER;
END ARCHITECTURE ART; |
|