--*********************************************
--
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_ARITH.ALL ;
USE IEEE.STD_LOGIC_UNSIGNED.ALL ;
LIBRARY altera;
USE altera.maxplus2.ALL;
--*********************************************
ENTITY elec_lock IS
PORT (
CLK_4M : IN STD_LOGIC ;
CLK_SCAN : OUT STD_LOGIC_VECTOR (3 downto 0) ; --scan sequence
KEY_IN : IN STD_LOGIC_VECTOR (2 downto 0) ; --KEY IN button code
FLAG_NUMB : OUT STD_LOGIC ;
FLAG_FUNC : OUT STD_LOGIC ;
LED_COM : OUT STD_LOGIC ; -- for LP-2900 only
CLEAR : OUT STD_LOGIC ; --**
ENLOCK : OUT STD_LOGIC ; --1:LOCK, 0:UNLOCK
NUMB_CNT : OUT STD_LOGIC_VECTOR (2 DOWNTO 0) ;
BCD_CODE : OUT STD_LOGIC_VECTOR (15 DOWNTO 0) ;
SELOUT : OUT STD_LOGIC_VECTOR (1 DOWNTO 0) ; --FIT TO LP-2900
SEGOUT : OUT STD_LOGIC_VECTOR(6 DOWNTO 0) -- SEG7 Display
);
END elec_lock ;
--*********************************************
ARCHITECTURE a OF elec_lock IS
component debouncing
port( d_in : IN STD_LOGIC ;
clk : IN STD_LOGIC ;
d_out : OUT STD_LOGIC ) ;
end component ;
SIGNAL CLK : STD_LOGIC ;
SIGNAL CLK_KEYBOARD : STD_LOGIC_VECTOR(1 downto 0) ;
SIGNAL CLK_DEBOUNCE : STD_LOGIC ;
SIGNAL CLK_DISPLAY : STD_LOGIC_VECTOR(1 downto 0) ;
SIGNAL C : STD_LOGIC_VECTOR(2 downto 0) ;
SIGNAL N : STD_LOGIC_VECTOR(3 downto 0) ;
SIGNAL F : STD_LOGIC_VECTOR(3 downto 0) ;
SIGNAL FN : STD_LOGIC ;
SIGNAL FF : STD_LOGIC ;
SIGNAL SEL : STD_LOGIC_VECTOR (3 downto 0) ;
SIGNAL OUT_NUMB : STD_LOGIC_VECTOR(3 downto 0) ;
SIGNAL OUT_FUNC : STD_LOGIC_VECTOR(3 downto 0) ;
SIGNAL ACC : STD_LOGIC_VECTOR (15 DOWNTO 0) ;
SIGNAL REG : STD_LOGIC_VECTOR (15 DOWNTO 0) ;
SIGNAL RR2 : STD_LOGIC ; -- ** RR2 = Clear
SIGNAL QA, QB, BB : STD_LOGIC ;
SIGNAL NC : STD_LOGIC_VECTOR (2 DOWNTO 0) ;
SIGNAL DB : STD_LOGIC_VECTOR( 3 DOWNTO 0);-- Number TO Display
SIGNAL SEG : STD_LOGIC_VECTOR( 6 DOWNTO 0);-- SEG7 Display Signal
BEGIN
--*******************************************
-- CONNECTION
OUT_NUMB <= N ;
OUT_FUNC <= F ;
FLAG_NUMB <= FN ;
FLAG_FUNC <= FF ;
CLK_DEBOUNCE <= CLK ;
NUMB_CNT<= NC ;
SEGOUT(6 DOWNTO 0) <= SEG; -- Seven Segment Display
SELOUT <= CLK_DISPLAY ;
LED_COM <= '1' ; --For LP-2900 only
--*********************************************
-- scan signal generator
counter : block
Signal Q : STD_LOGIC_VECTOR(22 DOWNTO 0);
Signal S : STD_LOGIC_VECTOR(1 DOWNTO 0) ; --keyboard scan about 15H
SIGNAL SEL : STD_LOGIC_VECTOR (3 downto 0);
BEGIN
PROCESS (Clk_4M)
Begin
IF CLK_4M'Event AND CLK_4M = '1' then
Q <= Q+1;
END IF;
END PROCESS;
CLK <= Q(0) ; --CLK = CLK_DEBOUNCE
CLK_KEYBOARD <= Q(5 DOWNTO 4) ; -- ***00-01-10-11
CLK_DISPLAY <= Q(5 DOWNTO 4) ;
-- To generate keyboard scan sequence 1110->1101->1011->0111
SEL <= "1110" WHEN CLK_KEYBOARD=0 ELSE
"1101" WHEN CLK_KEYBOARD=1 ELSE
"1011" WHEN CLK_KEYBOARD=2 ELSE
"0111" ;
CLK_SCAN <= SEL ;
end block counter ;
--*********************************************
--debounuing ckt
debounuing : block
begin
U1: debouncing PORT MAP (
d_in => key_in(0) ,
d_out => C(0) ,
clk => CLK
);
U2: debouncing PORT MAP (
d_in => key_in(1) ,
d_out => C(1) ,
clk => CLK
);
U3: debouncing PORT MAP (
d_in => key_in(2) ,
d_out => C(2) ,
clk => CLK
);
END block debounuing ;
--******************************************************
--key_decoder
key_decoder : block
signal Z : std_logic_VECTOR(4 downto 0) ; --KEY POSITION
SIGNAL R1, R0 : STD_LOGIC ;
begin
PROCESS(clk)
begin
Z <= CLK_KEYBOARD & C ;
IF CLK'EVENT AND CLK = '1' THEN
case Z is
when "11101" => N <= "0000" ;--0
when "00011" => N <= "0001" ;--1
when "00101" => N <= "0010" ;--2
when "00110" => N <= "0011" ;--3
when "01011" => N <= "0100" ;--4
when "01101" => N <= "0101" ;--5
when "01110" => N <= "0110" ;--6
when "10011" => N <= "0111" ;--7
when "10101" => N <= "1000" ;--8
when "10110" => N <= "1001" ;--9
when others => N <= "1111" ;
end case ;
END IF ;
--****************************
IF CLK'EVENT AND CLK = '1' THEN
case Z is
when "11011" => F <= "0100" ;--*_LOCK
when "11110" => F <= "0001" ;--#_UNLOCK
when others => F <= "1000" ;
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 NOT F(1) AND NOT F(0)) OR (NOT F(3) AND NOT F(2)AND NOT F(1) AND F(0) ) ;
--To generate clear signal for ACC
PROCESS (CLK)
BEGIN
IF CLK'EVENT AND CLK = '1' THEN
R1 <= R0 ; R0 <= FF ;
END IF ;
RR2 <= R1 AND NOT R0 ;
CLEAR <= RR2 ;
END PROCESS ;
end block key_decoder ;
--*****************************************************
--KEYIN /// BACK /// ALL CLEAR
KEYIN_PROCESS :BLOCK
SIGNAL RST,D0,D1: STD_LOGIC ;
BEGIN
PROCESS(CLK,FN,RST)
BEGIN
IF RST = '1' THEN
ACC <= "0000000000000000" ; --CLEAR INPUT
NC <= "000" ;
ELSE
IF FN'EVENT AND FN = '1' THEN
IF NC < 4 THEN
ACC <= ACC(11 DOWNTO 0) & N ;
BB <= '0' ;
NC <= NC + 1 ;
ELSE
BB <= '1' ;
END IF ;
END IF ;
END IF ;
END PROCESS ;
RST <= RR2 ;
END BLOCK KEYIN_PROCESS ;
--*********************************************
LOCK_PROCESS : BLOCK
BEGIN
PROCESS(CLK,F)
BEGIN
IF (CLK'EVENT AND CLK = '1') THEN
IF NC = 4 THEN
IF F(2) = '1' THEN --LOCK
REG <= ACC ;
QA <= '1' ; QB <= '0';
ELSIF F(0) = '1' THEN --UNLOCK
IF REG = ACC THEN --CHECK PIN CODE
QB <= '1' ; QA<= '0';
END IF ;
ELSIF ACC = "0010010110000000" THEN
--To set "2580" is the Universal. pin number
QB <= '1' ; QA<= '0';
END IF ;
END IF;
END IF ;
END PROCESS ;
END BLOCK LOCK_PROCESS ;
ENLOCK <= QA AND NOT QB ;
BCD_CODE <= ACC ;
--**********************************************
MULTIPLEXER : BLOCK
BEGIN
DB <= ACC(15 DOWNTO 12) WHEN CLK_DISPLAY = 0 ELSE
ACC(11 DOWNTO 8) WHEN CLK_DISPLAY = 1 ELSE
ACC(7 DOWNTO 4) WHEN CLK_DISPLAY = 2 ELSE
ACC(3 DOWNTO 0) WHEN CLK_DISPLAY = 3 ELSE
"1111" ;
End Block MULTIPLEXER ;
--**********************************************
SEVEN_SEGMENT : Block -- Binary Code -> Segment 7 Code
Begin
--gfedcba
SEG <= "0111111" WHEN DB = 0 ELSE
"0000110" WHEN DB = 1 ELSE
"1011011" WHEN DB = 2 ELSE
"1001111" WHEN DB = 3 ELSE
"1100110" WHEN DB = 4 ELSE
"1101101" WHEN DB = 5 ELSE
"1111101" WHEN DB = 6 ELSE
"0000111" WHEN DB = 7 ELSE
"1111111" WHEN DB = 8 ELSE
"1101111" WHEN DB = 9 ELSE
"1111111";
End Block SEVEN_SEGMENT;
END a;