⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyboard.vhd

📁 键盘扫描程序
💻 VHD
字号:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;

entity keyboard is
	port (  
	      clk ,rst: in  std_logic;
          keyin   : in  std_logic_vector(3 downto 0);
   	      selout  : out std_logic_vector(2 downto 0);
          test: out std_logic;
          keyval  : out std_logic_vector(3 downto 0)
          );
end keyboard;
 
architecture a of keyboard is
  signal clk1,koff,tst: std_logic;                  ---koff=1,the key pressed is not answered
  signal counter: std_logic_vector(1 downto 0);
  signal counter1: std_logic_vector(7 downto 0);
  signal sel: std_logic_vector(2 downto 0);
  signal chkkey: std_logic_vector(6 downto 0);
begin

tst<=keyin(0) and keyin(1) and keyin(2) and keyin(3);     --test=0 indicate that there is a key pressed
test<=koff;
----------------2_bit counter uses to generate clk1----------------------------------
p0:process(clk,rst)
begin
   if rst='1' then
     counter<="00";
   elsif clk'event and clk='1' then
       counter<=counter+1;
   end if;
end process p0;

clk1<='0' when counter<="01" else                        --clk1 4 divided by clk
      '1';

-------------------------------3_bit counter----------------------------------------
p1: process(rst,clk1)
 begin
   if rst='1' then
     sel<="000";
   elsif clk1'event and clk1='1' then
     if tst='0' or koff='1' then
       sel<=sel;
     else
       sel<=sel+1;
     end if;
   end if;
end process p1;

selout<=sel;
chkkey<=sel & keyin;

-------------------------scan code translate into hex--------------------------------
p2: process(clk,rst)
 begin
   if(rst='1') then
     keyval<="0000";
   elsif(clk'event and clk='0') then              --use the descending edge when the signal is stable
     if(chkkey="0001110") then
        keyval<="0000";
     elsif(chkkey="0011110") then
        keyval<="0001";
     elsif(chkkey="0101110") then
        keyval<="0010";
     elsif(chkkey="0111110") then
        keyval<="0011";
     elsif(chkkey="1101110") then
        keyval<="0100";
     elsif(chkkey="1111110") then
        keyval<="0101";
     elsif(chkkey="0001101") then
        keyval<="0110";
     elsif(chkkey="0011101") then
        keyval<="0111";
     elsif(chkkey="1001101") then
        keyval<="1000";
     elsif(chkkey="1011101") then
        keyval<="1001";
     elsif(chkkey="1101101") then
        keyval<="1010";
     elsif(chkkey="1111101") then
        keyval<="1011";
     elsif(chkkey="0101011") then
        keyval<="1100";
     elsif(chkkey="0111011") then
        keyval<="1101";
     elsif(chkkey="1001011") then
        keyval<="1110";
     elsif(chkkey="1011011") then
        keyval<="1111";
     end if;
   end if;
end process p2;

---------------------after test=0,there is a delay-------------------
  p3:process(koff,clk,rst)
  begin
    if rst='1' then
      counter1<="00000000";
      koff<='0';
    elsif clk'event and clk='1' then
      if tst='0' then
        counter1<="00000000";
        koff<='1';
      elsif counter1<"11111111" then
        counter1<=counter1+1;
      elsif counter1="11111111" then
        koff<='0';
      end if;
    end if;
  end process p3;
end a;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -