LIBRARY IEEE;USEIEEE.STD_LOGIC_1164.ALL;USEIEEE.STD_LOGIC_UNSIGNED.ALL;ENtiTY LOCK ISPORT(CLK,CLK5:INSTD_LOGIC; KEY1,KEY2,KEY3,KEY4,KEY5,KEY6,KEY7,KEY8:INSTD_LOGIC; Q1,Q2,Q3,Q4:OUT STD_LOGIC_VECTOR(3 DOWNTO0); LED_G,LED_R,LED,BEEP:OUT STD_LOGIC);END LOCK;ARCHITECTURE BEHAVOF LOCK IS SIGNAL PASSWORD1:STD_LOGIC_VECTOR(3 DOWNTO0):="0001"; SIGNAL PASSWORD2:STD_LOGIC_VECTOR(3 DOWNTO0):="0001"; SIGNAL PASSWORD3:STD_LOGIC_VECTOR(3 DOWNTO0):="0001"; SIGNAL PASSWORD4:STD_LOGIC_VECTOR(3 DOWNTO0):="0001"; SIGNAL JISHI5:STD_LOGIC_VECTOR(3 DOWNTO0):="0000"; SIGNAL JISHI20:STD_LOGIC_VECTOR(4 DOWNTO0):="00000"; SIGNALZISUO,FLAG,BEEP_FLAG,KAI,LED_TMP:STD_LOGIC:='0'; SIGNALCODE1,CODE2,CODE3,CODE4:STD_LOGIC_VECTOR(3 DOWNTO 0); BEGIN PROCESS(clk,key1,key2,key3,key4,key6)BEGINIF ZISUO='0' ANDKEY6='0' AND KAI='0' THEN IF KEY1'EVENT AND KEY1='1' THEN --用来输入代码,在关锁状况下,并且无开锁和设置密码信号下有效 IF CODE1<9 THENCODE1<=CODE1+1; ELSE CODE1<=(OTHERS=>'0'); END IF; END IF; IF KEY2'EVENT AND KEY2='1' THEN IF CODE2<9 THEN CODE2<=CODE2+1; ELSE CODE2<=(OTHERS=>'0'); END IF; END IF; IF KEY3'EVENT AND KEY3='1' THEN IF CODE3<9 THENCODE3<=CODE3+1; ELSE CODE3<=(OTHERS=>'0'); END IF; END IF; IF KEY4'EVENT AND KEY4='1' THEN IF CODE4<9 THENCODE4<=CODE4+1; ELSE CODE4<=(OTHERS=>'0'); END IF; END IF; ELSIF(ZISUO='0' AND KEY7='1' AND KAI='1')THEN CODE1<="0000";CODE2<="0000";CODE3<="0000";CODE4<="0000";--这一步很重要,重新上锁必须将密码值清零,不然会显示上次输入的密码 IF KEY6='1' AND KAI='1' THEN --显示密码 Q1<=PASSWORD1; Q2<=PASSWORD2; Q3<=PASSWORD3; Q4<=PASSWORD4; END IF; IF KEY6='0' AND KAI='0' THEN --显示要更改的密码 Q1<=CODE1; Q2<=CODE2; Q3<=CODE3; Q4<=CODE4; END IF; IF KEY6='0' AND KAI='1' THEN --开锁后显示零 Q1<="0000"; Q2<="0000"; Q3<="0000"; Q4<="0000"; END IF; ELSIF ZISUO='0' AND KEY6='1' AND KAI='1'THEN --按住KEY6设置密码,此时必须是开锁状态 IF KEY1'EVENT AND KEY1='1' THEN IF PASSWORD1<9 THENPASSWORD1<=PASSWORD1+1; ELSE PASSWORD1<=(OTHERS=>'0'); END IF; END IF; IF KEY2'EVENT AND KEY2='1' THEN IF PASSWORD2<9 THENPASSWORD2<=PASSWORD2+1; ELSE PASSWORD2<=(OTHERS=>'0'); END IF; END IF; IF KEY3'EVENT AND KEY3='1' THEN IF PASSWORD3<9 THENPASSWORD3<=PASSWORD3+1; ELSE PASSWORD3<=(OTHERS=>'0'); END IF; END IF; IF KEY4'EVENT AND KEY4='1' THEN IF PASSWORD4<9 THENPASSWORD4<=PASSWORD4+1; ELSE PASSWORD4<=(OTHERS=>'0'); End IF; END IF; END IF; END PROCESS; PROCESS (CLK,key5,key7) BEGIN IF CLK'EVENT AND CLK='1' THEN IF KEY5='1' AND ZISUO='0' AND KEY6='0'THEN IF PASSWORD1=CODE1 AND PASSWORD2=CODE2 ANDPASSWORD3=CODE3 AND PASSWORD4=CODE4 THEN KAI<='1'; --按下开锁键后,如果密码正确,开锁 ELSE KAI<='0'; END IF; END IF; END IF; IF KAI='1' THEN LED_G<='1';LED_R<='0'; --开锁后红灯灭绿灯亮,否则红灯亮绿灯灭 ELSE LED_G<='0';LED_R<='1'; END IF; IF KEY7='1' AND KEY6='0' THEN --关锁按钮按下后关锁 KAI<='0';LED_G<='0';LED_R<='1'; END IF; END PROCESS; PROCESS(CLK,KEY1,KEY8) BEGIN IF(KEY1'EVENT AND KEY1='1')THEN FLAG<='1'; END IF; IF CLK'EVENT AND CLK='1' THEN --按下第一个按键时 置位标志位FLAG,开始计时 IF(FLAG='1')THEN IF ZISUO='0' AND KEY6='0' ANDKAI='0' THEN IF JISHI5<5 THENJISHI5<=JISHI5+1; ELSE JISHI5<="0000"; END IF; END IF; END IF; IF (JISHI5=5 AND KAI='0') THEN --如果5秒后未开锁则进入自锁状态 ZISUO<='1'; END IF; END IF; IF KAI='1' THEN --5秒内开锁 计时清零,标志位清零 JISHI5<="0000";FLAG<='0'; END IF; IF KEY8='1' AND ZISUO='1' THEN --K8按下后清除自锁 ZISUO<='0';FLAG<='0'; END IF; END PROCESS; PROCESS(CLK,KEY8,CLK5) BEGIN IF CLK'EVENT AND CLK='1' THEN --如果自锁则蜂鸣器持续20秒发声 IF ZISUO='1' THEN IF JISHI20<20 THEN JISHI20<=JISHI20+1; BEEP_FLAG<='1'; --置位蜂鸣器标志位 ELSE BEEP_FLAG<='0'; END IF; LED_TMP<=NOT LED_TMP; --进入自锁后灯闪烁提示 LED<=LED_TMP; END IF; IF ZISUO='0' THEN --自锁解除,清零计时值和标志位 JISHI20<="00000";LED<='0';BEEP_FLAG<='0';LED_TMP<='0';JISHI20<="00000"; END IF; END IF; IFBEEP_FLAG='1' THEN --判断蜂鸣器标志位是否为1 IF CLK5='1' THEN --CLK5给无源蜂鸣器提供1K的时钟信号
一周热门 更多>