|
--ZTQ.VHD
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_ARITH.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
ENTITY ZTQ IS
PORT(DATA_N:IN STD_LOGIC_VECTOR(3 DOWNTO 0);
DATA_F:IN STD_LOGIC_VECTOR(3 DOWNTO 0);
FLAG_N:IN STD_LOGIC;
CLK:IN STD_LOGIC;
CLK_1S:IN STD_LOGIC;
KAI:OUT STD_LOGIC;
WARN:OUT STD_LOGIC;
DATA:OUT STD_LOGIC_VECTOR(15 DOWNTO 0));
END ENTITY ZTQ;
ARCHITECTURE ART OF ZTQ IS
SIGNAL ACC,REE,MIMA:STD_LOGIC_VECTOR(15 DOWNTO 0);
SIGNAL NC,DIR:STD_LOGIC_VECTOR(2 DOWNTO 0);
SIGNAL CUO:STD_LOGIC_VECTOR(1 DOWNTO 0);
SIGNAL TWICE,JINGGAO,CHANG,DUAN,SPEAKER,ANJIAN,KAISUO,ENLOCK:STD_LOGIC;
TYPE STATE_TYPE IS (S0,S1,S2,S3,S4);
---S0输入状态, S1输入后按键判断状态, S2开锁状态, S3输入错误状态,S4密码重新设置状态;
SIGNAL STATE:STATE_TYPE;
BEGIN
PROCESS(DATA_F,CUO,KAISUO,JINGGAO,TWICE,ANJIAN)IS
BEGIN
REE<="0000000000000000"; ----初始化
NC<="000";
TWICE<='0';
CUO<="00";
ENLOCK<='0';
JINGGAO<='0';
CHANG<='0';
DUAN<='0';
SPEAKER<='0';
DIR<="000";
KAISUO<='0';
ANJIAN<='0';
IF DATA_F="1000" THEN
STATE<=S0;
END IF;
CASE STATE IS
WHEN S0=>
IF ANJIAN='1' THEN
IF NC<4 THEN
ACC<=ACC(11 DOWNTO 0)&DATA_N;
NC<=NC+1; ANJIAN<='0';
STATE<=S0;
END IF;
ELSIF
(DATA_F="0100")THEN ---清除;
NC<="000";ACC<="0000000000000000";
STATE<=S0;
ELSIF
(DATA_F="0001")THEN ---确认;
STATE<=S1;
ELSE
STATE<=S0;
END IF;
WHEN S1=>
IF NC<4 THEN
STATE<=S3;
ELSIF (NC=4)THEN
IF ENLOCK='0' THEN
IF ACC=MIMA THEN --密码符合;
STATE<=S2;KAISUO<='1';
ELSIF ACC="0000100100100011" THEN --万用密码;
STATE<=S2;KAISUO<='1';
ELSE
STATE<=S3;
END IF;
ELSE
STATE<=S4;
END IF;
END IF;
WHEN S2=>ENLOCK<='1';CUO<="00";NC<="000";ACC<="0000000000000000";
IF(DATA_F=0010)THEN ---设置密码;
STATE<=S0;
ELSIF(DATA_F="1000")THEN ---关锁;
STATE<=S0;ENLOCK<='0';NC<="000";ACC<="0000000000000000";
END IF;
WHEN S3=>JINGGAO<='1';
IF CUO<2 THEN ---短报警;
CUO<=CUO+1; DUAN<='1';NC<="000";ACC<="0000000000000000";TWICE<='0';
STATE<=S0;
ELSIF CUO=2 THEN ---长报警;
NC<="000";ACC<="0000000000000000";CUO<="00";CHANG<='1';
STATE<=S0;
END IF;
WHEN S4=>
IF (TWICE='0')THEN ---第一次输入密码;
TWICE<='1'; REE<=ACC;
STATE<=S0; NC<="000";ACC<="0000000000000000";
ELSIF(TWICE='1')THEN ---确认密码;
IF(REE=ACC)THEN
MIMA<=REE;STATE<=S2;
ELSE
STATE<=S3;
END IF;
END IF;
END CASE;
END PROCESS;
SHIZHONG:BLOCK IS
SIGNAL DIR:STD_LOGIC_VECTOR(2 DOWNTO 0);
SIGNAL JINGGAO,CHANG,DUAN,SPEAKER,KAISUO:STD_LOGIC;
BEGIN
PROCESS(CLK_1S,KAISUO,JINGGAO,DUAN,CHANG,DIR)IS
BEGIN
IF CLK_1S'EVENT AND CLK_1S='1' THEN --1S时钟;
IF JINGGAO='1'THEN
SPEAKER<='1';DIR<=DIR+1;
IF DUAN='1' THEN --1S到了消除短报警信号
IF(DIR="001") THEN DUAN<='0';SPEAKER<='0';DIR<="000";JINGGAO<='0';
END IF;
ELSIF CHANG='1' THEN ---5S到了消除长报警信号
IF(DIR="101") THEN CHANG<='0';SPEAKER<='0';DIR<="000";JINGGAO<='0';
END IF;
END IF;
ELSIF KAISUO='1' THEN DIR<=DIR+1; --开锁显示
IF(DIR="001")THEN KAISUO<='0';DIR<="000";
END IF;
END IF;
END IF;
END PROCESS;
END BLOCK SHIZHONG;
KEYIN_PROCESS:BLOCK IS
SIGNAL ANJIAN:STD_LOGIC;
BEGIN
PROCESS(FLAG_N)IS
BEGIN
IF FLAG_N'EVENT AND FLAG_N='1' THEN --判断数字按键;
ANJIAN<='1';
ELSE
ANJIAN<='0';
END IF;
END PROCESS;
END BLOCK KEYIN_PROCESS;
DATA<=ACC; --最后数据输出
KAI<=KAISUO;
WARN<=JINGGAO;
END ARCHITECTURE ART; |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|