library ieee;
use ieee.std_logic_1164.all;
entity Keyboard is
port
(clock:in std_logic;
kin:in std_logic_vector(0 to 3);
ScanSignal:out std_logic_vector(0 to 3);
num:out integer range 0 to 15
);
end Keyboard;
architecture Scan of Keyboard is
signal ScanS:Std_logic_vector(0 to 7);
signal SCN:std_logic_vector(0 to 3);
signal Counter:integer range 0 to 3;
signal CounterB:integer range 0 to 3;
begin
process(clock)
begin
if rising_edge(clock) then
if Counter=3 then
Counter<=0;
else
Counter<=Counter+1;
end if;
case Counter is
when 0=>SCN<="1000";
when 1=>SCN<="0100";
when 2=>SCN<="0010";
when 3=>SCN<="0001";
end case;
end if;
end process;
process(clock)
begin
if falling_edge(clock) then
if kin="0000" then
if CounterB=3 then
num<=15;
CounterB<=0;
else
CounterB<=CounterB+1;
end if;
else
CounterB<=0;
case ScanS is
when "10000001"=>num<=0;
when "10000010"=>num<=1;
when "10000100"=>num<=2;
when "10001000"=>num<=3;
when "01000001"=>num<=4;
when "01000010"=>num<=5;
when "01000100"=>num<=6;
when "01001000"=>num<=7;
when "00100001"=>num<=8;
when "00100010"=>num<=9;
when "00100100"=>num<=10;
when "00101000"=>num<=11;
when "00010001"=>num<=12;
when "00010010"=>num<=13;
when "00010100"=>num<=14;
when others=>num<=15;
end case;
end if;
end if;
end process;
ScanS<=SCN&kin;
ScanSignal<=SCN;
end;