You have no items in your shopping cart.

Subtotal: 0.00

Matrix Keypad interfacing with Spartan3 XC3S200 FPGA Development Kit

Matrix Keypad

Keypad is most commonly used input device in electronics. To Simply number of input keypad lines to controller device leads to formation of matrix keypad. Matrix keyboard consist of NxN number of push button arranged in rows and column. 2N is the number of lines required to get the input of NxN Push Button.

 

Matrix keypad consists of a set of buttons similar to an alphanumeric keyboard provided with keys usually marked with letters or numbers and various extra keys. Embedded systems which require user interaction must be interfaced with devices that accept user input such as a keypad.

Interfacing Keypad with Spartan3 FPGA Development Kit

The Spartan-3 FPGA board has 4x4 Matrix Keypad, indicated as in Figure. Keypads arranged by matrix format, each row and column section pulled by high, all row lines and column lines connected directly by the I/O pins.

mimic-diagram-for-triac

Matrix Keypad Placement in Spartan3 FPGA Development Kit



mimic-diagram-for-triac

VHDL Code Description for Matrix Keypad to LED Interface

The Spartan3 FPGA Scan continuously for key press. Once the NxN key pressed the following key press will be represented in 4- bit binary value and displayed in the LED’s.

VHDL Code for Matrix Keypad to LED Interface

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;

LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_ARITH.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;

entity matrix_keypad is
port(
clock,rst : in std_logic;
col_line : in   std_logic_vector(3 downto 0);
row_line : out std_logic_vector(3 downto 0);
led : out   std_logic_vector(15 downto 0)
);
end matrix_keypad;

architecture Behavioral of matrix_keypad is
signal temp : std_logic_vector(29 downto 0);
begin
test: process(clock,rst) is
begin
if(rst='0') then
led <= "1111111111111111";
elsif rising_edge(clock) then
temp <= temp + 1;
	case temp(10 downto 8) is
	when "000" =>
	 row_line <= "0111"; --first row
	when "001" =>   
	 if col_line = "0111" then
		  led <= "0011001100110011"; -- 3
	 elsif col_line = "1011" then
		  led <= "0010001000100010"; -- 2
	 elsif col_line = "1101" then
		  led <= "0001000100010001"; -- 1
	 elsif col_line = "1110" then
		  led <= "0000000000000000"; -- 0
	 end if;
	when "010" =>
	 row_line <= "1011"; --second row
	when "011" =>
	 if col_line = "1110" then
		  led <= "0100010001000100"; -- 4
	 elsif col_line = "1101" then
		  led <= "0101010101010101"; -- 5
	 elsif col_line = "1011" then
		  led <= "0110011001100110"; -- 6
	 elsif col_line = "0111" then
		  led <= "0111011101110111"; -- 7
	 end if;
	when "100" =>
	 row_line <= "1101"; --third row
	when "101" =>
	 if col_line = "1110" then
		  led <= "1000100010001000"; -- 8
	 elsif col_line = "1101" then
		  led <= "1001100110011001"; -- 9
	 elsif col_line = "1011" then
		  led <= "1010101010101010"; -- a
	 elsif col_line = "0111" then
		  led <= "1011101110111011"; -- b
	 end if;
	when "110" =>
	 row_line <= "1110"; --fourth row
	when "111" =>
	 if col_line = "1110" then
		  led <= "1100110011001100"; -- C
	 elsif col_line = "1101" then
		  led <= "1101110111011101"; -- d
	 elsif col_line = "1011" then
		  led <= "1110111011101110"; -- e
	 elsif col_line = "0111" then
		  led <= "1111111111111111"; -- f
	 end if;
	when others => led <= "0000000000000000";
	end case;
end if;
end process;
end Behavioral;

User Constraint File

NET "row_line(0)"  LOC = "p85";
NET "row_line(1)"  LOC = "p86";
NET "row_line(2)"  LOC = "p87";
NET "row_line(3)"  LOC = "p89";
NET "clock"  LOC = "p55"  ;
NET "col_line(0)"  LOC = "p90"  ;
NET "col_line(1)"  LOC = "p92"  ;
NET "col_line(2)"  LOC = "p95"  ;
NET "col_line(3)"  LOC = "p96"  ;
NET "led(0)"  LOC = "p14"  ;
NET "led(1)"  LOC = "p15"  ;
NET "led(2)"  LOC = "p17"  ;
NET "led(3)"  LOC = "p18"  ;
NET "led(4)"  LOC = "p20"  ;
NET "led(5)"  LOC = "p21"  ;
NET "led(6)"  LOC = "p23"  ;
NET "led(7)"  LOC = "p24"  ;
NET "rst"  LOC = "p32"  ;

VHDL Code Description for Matrix Keypad to LCD Interface The Spartan3 FPGA Scan continuously for key press. Once the NxN key pressed the following key press displayed in the LCD as N Row, N Column Pressed. VHDL Code for Matrix Keypad to LCD Interface

library ieee; 
use ieee.std_logic_1164.all; 
use ieee.std_logic_arith.all; 
use ieee.std_logic_unsigned.all; 

entity lcd is 
port(clock:in std_logic;    				--clock i/p
     rw: out std_logic;     				--read write control
     rs:out std_logic;      				--command data control
     en:out std_logic;      				--lcd enable
     a1 : inout std_logic_vector(3 downto 0) ;  --row scan line
     a2 : inout std_logic_vector(3 downto 0) ;  --column scan line
     data:out std_logic_vector(7 downto 0));    --data lines 
end lcd; 

architecture arch of lcd is  
type state is(state1,state2,state3,state4,state5,state6);    --fsm for data
signal ro : state := state1;
begin  
process(clock)
variable output:std_logic_vector(3 downto 0):="1111"; --logic high connection
variable input:std_logic_vector(3 downto 0):="0000";  --logic low connection
variable verify : std_logic_vector(3 downto 0) ;
variable verify1 : std_logic_vector(3 downto 0)  ;  
variable i,j : integer := 0 ;
variable datas,datam : std_logic_vector(7 downto 0) := "00110001";
begin  
rw<='0';
-------------------------------------------------------------------------
if clock'event and clock='1' then 
if ro = state1 then
a2 <= input;
else
a2 <= (OTHERS => 'Z');
end if;
if ro /= state1 then
a1 <= output;
else
a1 <= (OTHERS => 'Z');
end if;
verify1 := a1 ;
verify := a2 ;

case ro is
when state1 =>       	--column scan state
case verify1 is
when "1111" =>     	--no key pressed
ro <= state1 ;
when "0111" =>   		--1st column
datas  := x"31";
ro <= state2;
when "1011" =>   		--2nd column
datas  := x"32";
ro <= state3;
when "1101" =>    	--3rd column
datas  := x"33";
ro <= state4;
when "1110" =>   		--4th column
datas  := x"34";
ro <= state5;
when others =>
ro <= state1 ;
end case;
when state2 =>   		--1st row scan logic
case verify is
when "1000" =>
datam := x"31";
ro <= state6 ;
when "0100" =>
datam := x"32";
ro <= state6;
when "0010" =>
datam := x"33";
ro <= state6 ;
when "0001" =>
datam := x"34";
ro <= state6 ;
when others =>
ro <= state2 ;
end case;
when state3 =>   		--2nd row scan logic
case verify is
when "1000" =>
datam := x"31";
ro <= state6 ;
when "0100" =>
datam := x"32";
ro <= state6 ;
when "0010" =>
datam := x"33";
ro <= state6 ;
when "0001" =>
datam := x"34";
ro <= state6 ;
when others =>
ro <= state3 ;
end case;
when state4 =>    	--3rd row scan logic
case verify is
when "1000" =>
datam := x"31";
ro <= state6 ;
when "0100" =>
datam := x"32";
ro <= state6 ;
when "0010" =>
datam := x"33";
ro <= state6 ;
when "0001" =>
datam := x"34";
ro <= state6 ;
when others =>
ro <= state4 ;
end case;
when state5 =>   		--4th row scan logic
case verify is
when "1000" =>
datam := x"31";
ro <= state6 ;
when "0100" =>
datam := x"32";
ro <= state6 ;
when "0010" =>
datam := x"33";
ro <= state6 ;
when "0001" =>
datam := x"34";
ro <= state6 ;
when others =>
ro <= state5 ;
end case;
------------------------------------------------------------------------- 
when state6 =>
if i <= 500000 then
i := i + 1;
elsif i > 500000 then
i := 0 ;
if j < 36 then
j := j + 1 ;
ro <= state6;
elsif j = 36 then
j := 0 ;
ro <= state1;
end if;
end if;
case j is 
when 1=> 
data<="00111000";     --ascii code of data to be displayed
en<='1'; 
rs<='0';   
when 2=> 
en<='0';   
when 3=> 
data<="00001110"; 
en<='1'; 
rs<='0';  
when 4=> 
en<='0';  
when 5=> 
data<="00000001"; 
en<='1'; 
rs<='0';   
when 6=> 
en<='0';  
when 7=> 
data<="00000110"; 
en<='1'; 
rs<='0';   
when 8=> 
en<='0';  
when 9=> 
data<="10000000"; 
en<='1'; 
rs<='0';   
when 10=> 
en<='0';  
--------------------------------------------------------------------------------
when 11=> 
data<="01010010";    	--  ascii code for "R" 
en<='1'; 
rs<='1';   
when 12=> 
en<='0';  
when 13=> 
data<="01001111";    	--  ascii code for "O" 
en<='1'; 
rs<='1';   
when 14=> 
en<='0';  
when 15=> 
data<=  "01010111";    	--  ascii code for "W" 
en<='1'; 
rs<='1';   
when 16=> 
en<='0';  
when 17=> 
data<="00100000";       --  ascii code for "SPACE" 
en<='1'; 
rs<='1';  
when 18=> 
en<='0';  
when 19=> 
data<="00111010";       --  ascii code for ":"
en<='1'; 
rs<='1';   
when 20=> 
en<='0';  
----------------------------------------------------------------
when 21=> 
data<=datam;          	--  ascii code for " " 
rs<='1';  
en<='1'; 
when 22=> 
en<='0'; 
when 23=> 
data<="00100000";       --  ascii code for "SPACE" 
rs<='1';  
en<='1'; 
when 24=> 
en<='0'; 
when 25=> 
data<="01000011";       --  ascii code for "C" 
rs<='1';  
en<='1'; 
when 26=> 
en<='0'; 
-----------------------------------------------------------------
when 27=> 
data<="01001111";       --  ascii code for "O" 
rs<='1';  
en<='1'; 
when 28=> 
en<='0'; 
when 29=> 
data<="01001100";       --  ascii code for "L" 
en<='1'; 
rs<='1';  
when 30=> 
en<='0'; 
when 31=> 
data<="00100000";       --  ascii code for "SPACE" 
rs<='1';  
en<='1'; 
when 32=> 
en<='0'; 
when 33=> 
data<="00111010";       --  ascii code for ":" 
rs<='1';  
en<='1'; 
when 34=> 
en<='0'; 
when 35=> 
data<=datas;          	--  ascii code for " " 
rs<='1';  
en<='1'; 
when 36=> 
en<='0'; 
----------------------------------------------------------------- 
when others=> 
data<=datas;          	--  ascii code for " " 
rs<='1';  
en<='0'; 
end case; 
end case;
----------------------------------------------
end if;
----------------------------------------------
end process; 
end arch;

User Constraint File

NET "a2(0)"  LOC = "p85"  ;
NET "a2(1)"  LOC = "p86"  ;
NET "a2(2)"  LOC = "p87"  ;
NET "a2(3)"  LOC = "p89"  ;
NET "a1(0)"  LOC = "p90"  ;
NET "a1(1)"  LOC = "p92"  ;
NET "a1(2)"  LOC = "p95"  ;
NET "a1(3)"  LOC = "p96"  ;
NET "clock"  LOC = "p55"  ;
NET "data(0)"  LOC = "p108"  ;
NET "data(1)"  LOC = "p112"  ;
NET "data(2)"  LOC = "p113"  ;
NET "data(3)"  LOC = "p116"  ;
NET "data(4)"  LOC = "p118"  ;
NET "data(5)"  LOC = "p119"  ;
NET "data(6)"  LOC = "p122"  ;
NET "data(7)"  LOC = "p123"  ;
NET "en"  LOC = "p107"  ;
NET "rs"  LOC = "p104"  ;
NET "rw"  LOC = "p105"  ;