数电课设——数字密码锁

发布时间:2026/6/29 18:05:44

数电课设——数字密码锁 题目4.数字密码锁电路一、设计要求设计一个简易的数字密码锁电路该锁应在收到3位与规定码相符的十进制数码时打开使相应指示灯点亮若收到代码与规定不符或者开锁程序有误表示错误的指示灯点亮。二、系统功能描述1系统接通电源后首先按动SETUP键后方投入运行。运行时标志开门的指示灯和警报灯、铃皆不工作系统处于安锁状态。2开锁代码是3位十进制数可按照用户的意愿调定。代码不足3位或超出3位时均不能开锁。3开锁程序由设计者确定用户必须严格执行所规定的程序方可开锁。4开锁代码和程序正确表示数字锁打开的指示灯点亮。5允许用户在开锁过程中有1次错误输入代码错误或开锁程序错误只要出错表示错误的指示灯必定点亮。如果有两次错误则报警器喇叭鸣叫以示情况异常。6开锁程序为I按启动键START启动开锁程序此时系统内部分应处于初始状态。II依次键入3个十进制码。III按开门键OPEN准备开门。若按上述程序执行且拨号正确则开门继电器工作绿灯亮若密码输入错误或未按上述程序执行则按动开门键OPEN后警报装置鸣叫红灯亮。IV开锁事务处理完毕后应将门关上按SETUP键使系统重新进入安锁状态。若在报警按SETUP或START均不起作用应另用一内部的I-SETUP键才能使系统进入安锁状态。V若按错号码可在按OPEN键之前按START键重新启动开锁程序。7号码09、START、OPEN均用按键产生并均有消抖和同步化电路。受限与实验条件我将蜂鸣器发出警告换位了灯泡闪亮密码也换位了固定密码若是想要在实验箱上自由设置密码可以将generic (delay : integer : 10;clock_Hz : integer : 50_000_000;preset_1 : std_logic_vector(3 downto 0) : 0001; -- 密码第一位: 1 ← 新增preset_2 : std_logic_vector(3 downto 0) : 0010; -- 密码第二位: 2 ← 新增preset_3 : std_logic_vector(3 downto 0) : 0011 -- 密码第三位: 3 ← 新增);port (clk : in std_logic;--时钟信号rst_n : in std_logic;--复位低电平有效setup : in std_logic;--使能i_setup : in std_logic;--mis2后的使能start : in std_logic;--输密码前opendoor : in std_logic;--输密码后key : in std_logic_vector(9 downto 0);--数字按键green_led : out std_logic;--rightred_led : out std_logic;--misalarm : out std_logic;--mis2relay : out std_logic--right结果);部分换为generic (delay : integer : 10;clock_Hz : integer : 50_000_000);port (clk : in std_logic;--时钟信号rst_n : in std_logic;--复位低电平有效preset_1 : std_logic_vector(3 downto 0) ; -- 密码第一位: 1preset_2 : std_logic_vector(3 downto 0) ; -- 密码第二位: 2preset_3 : std_logic_vector(3 downto 0) ; -- 密码第三位: 3setup : in std_logic;--使能i_setup : in std_logic;--mis2后的使能start : in std_logic;--输密码前opendoor : in std_logic;--输密码后key : in std_logic_vector(9 downto 0);--数字按键green_led : out std_logic;--rightred_led : out std_logic;--misalarm : out std_logic;--mis2relay : out std_logic--right结果);代码代码均由ai生成此文章只做分享参考library ieee;use ieee.std_logic_1164.all;use ieee.numeric_std.all;entity digital_lock isgeneric (delay : integer : 10;clock_Hz : integer : 50_000_000;preset_1 : std_logic_vector(3 downto 0) : 0001; -- 密码第一位: 1 ← 新增preset_2 : std_logic_vector(3 downto 0) : 0010; -- 密码第二位: 2 ← 新增preset_3 : std_logic_vector(3 downto 0) : 0011 -- 密码第三位: 3 ← 新增);port (clk : in std_logic;--时钟信号rst_n : in std_logic;--复位低电平有效setup : in std_logic;--使能i_setup : in std_logic;--mis2后的使能start : in std_logic;--输密码前opendoor : in std_logic;--输密码后key : in std_logic_vector(9 downto 0);--数字按键green_led : out std_logic;--rightred_led : out std_logic;--misalarm : out std_logic;--mis2relay : out std_logic--right结果);end entity;architecture behavioral of digital_lock is--延迟上限constant DBNC_MAX : integer : delay * clock_Hz / 1000;--状态type state_t is (power_off, wait_state,--start后num1, num2, num3, wait_opendoor,--opendoor前right, mis1, mis2);signal current_state : state_t;--当前消抖后signal setup_clean : std_logic;signal i_setup_clean : std_logic;signal start_clean : std_logic;signal opendoor_clean : std_logic;signal key_clean : std_logic_vector(9 downto 0);--前一刻消抖后signal setup_last : std_logic;signal i_setup_last : std_logic;signal start_last : std_logic;signal opendoor_last : std_logic;signal key_last : std_logic_vector(9 downto 0);--判断长短按signal setup_press : std_logic;signal i_setup_press : std_logic;signal start_press : std_logic;signal opendoor_press : std_logic;signal key_press : std_logic_vector(9 downto 0);--有数字按键按下 数字键数值signal digit_valid : std_logic;signal digit_num : std_logic_vector(3 downto 0);--用户输入三位密码signal input_1 : std_logic_vector(3 downto 0);signal input_2 : std_logic_vector(3 downto 0);signal input_3 : std_logic_vector(3 downto 0);--错误次数signal error_count : integer range 0 to 2;begin--按键消抖db_setup : entity work.debouncegeneric map (DBNC_MAX) port map (clk, rst_n, setup, setup_clean);db_i_setup : entity work.debouncegeneric map (DBNC_MAX) port map (clk, rst_n, i_setup, i_setup_clean);db_start : entity work.debouncegeneric map (DBNC_MAX) port map (clk, rst_n, start, start_clean);db_opendoor : entity work.debouncegeneric map (DBNC_MAX) port map (clk, rst_n, opendoor, opendoor_clean);gen_db : for i in 0 to 9 generatedb_key : entity work.debouncegeneric map (DBNC_MAX) port map (clk, rst_n, key(i), key_clean(i));end generate;-- 识别按键的按下瞬间process (clk, rst_n)beginif rst_n 0 thensetup_last 0;i_setup_last 0;start_last 0;opendoor_last 0;key_last (others 0);elsif rising_edge(clk) then--上升沿信号传递setup_last setup_clean;i_setup_last i_setup_clean;start_last start_clean;opendoor_last opendoor_clean;key_last key_clean;end if;end process;-- 高电平产生原因setup_press 1 when setup_clean 1 and setup_last 0 else 0;i_setup_press 1 when i_setup_clean 1 and i_setup_last 0 else 0;start_press 1 when start_clean 1 and start_last 0 else 0;opendoor_press 1 when opendoor_clean 1 and opendoor_last 0 else 0;gen_press : for i in 0 to 9 generatekey_press(i) 1 when key_clean(i) 1 and key_last(i) 0 else 0;end generate;-- 按键是那个数process (key_press)begin--默认无效值digit_valid 0;digit_num 1111;--优先级排序if key_press(0) 1 then digit_num 0000; digit_valid 1;--if key_press(0)1 then数字键的值为0000有数字键按下elsif key_press(1) 1 then digit_num 0001; digit_valid 1;elsif key_press(2) 1 then digit_num 0010; digit_valid 1;elsif key_press(3) 1 then digit_num 0011; digit_valid 1;elsif key_press(4) 1 then digit_num 0100; digit_valid 1;elsif key_press(5) 1 then digit_num 0101; digit_valid 1;elsif key_press(6) 1 then digit_num 0110; digit_valid 1;elsif key_press(7) 1 then digit_num 0111; digit_valid 1;elsif key_press(8) 1 then digit_num 1000; digit_valid 1;elsif key_press(9) 1 then digit_num 1001; digit_valid 1;end if;end process;process (clk, rst_n)begin--复位键发挥作用if rst_n 0 thencurrent_state power_off;--复位到关机input_1 0000;input_2 0000;input_3 0000;error_count 0;elsif rising_edge(clk) thencase current_state iswhen power_off if setup_press 1 thencurrent_state wait_state;error_count 0;end if;when wait_state if start_press 1 thencurrent_state num1;input_1 0000; input_2 0000; input_3 0000;end if;when num1 --输入第一位if digit_valid 1 theninput_1 digit_num;current_state num2;elsif start_press 1 theninput_1 0000; input_2 0000; input_3 0000;elsif opendoor_press 1 then--未输入当前位num1if error_count 0 thenerror_count 1; current_state mis1;elseerror_count 2; current_state mis2;end if;end if;when num2 if digit_valid 1 theninput_2 digit_num;current_state num3;elsif start_press 1 theninput_1 0000; input_2 0000; input_3 0000;current_state num1;elsif opendoor_press 1 thenif error_count 0 thenerror_count 1; current_state mis1;elseerror_count 2; current_state mis2;end if;end if;when num3 if digit_valid 1 theninput_3 digit_num;current_state wait_opendoor;elsif start_press 1 theninput_1 0000; input_2 0000; input_3 0000;current_state num1;elsif opendoor_press 1 thenif error_count 0 thenerror_count 1; current_state mis1;elseerror_count 2; current_state mis2;end if;end if;when wait_opendoor if opendoor_press 1 thenif (input_1 preset_1) and(input_2 preset_2) and(input_3 preset_3) thencurrent_state right;elseif error_count 0 thenerror_count 1; current_state mis1;elseerror_count 2; current_state mis2;end if;end if;elsif start_press 1 theninput_1 0000; input_2 0000; input_3 0000;current_state num1;end if;when right if setup_press 1 thencurrent_state wait_state;error_count 0;input_1 0000; input_2 0000; input_3 0000;end if;when mis1 if start_press 1 thencurrent_state num1;input_1 0000; input_2 0000; input_3 0000;elsif setup_press 1 thencurrent_state wait_state;error_count 0;input_1 0000; input_2 0000; input_3 0000;end if;when mis2 if i_setup_press 1 thencurrent_state wait_state;error_count 0;input_1 0000; input_2 0000; input_3 0000;end if;when others current_state power_off;end case;end if;end process;-- 输出状态green_led 1 when current_state right else 0;--如果正确green 是1反之是0relay 1 when current_state right else 0;red_led 1 when (current_state mis1) or (current_state mis2) else 0;alarm 1 when current_state mis2 else 0;end architecture;-- 消抖library ieee;use ieee.std_logic_1164.all;entity debounce isgeneric (LIMIT : integer : 50);port (clk : in std_logic;rst_n : in std_logic; -- 强制模块回到初始状态key_in : in std_logic; -- 原始按键输入key_out : out std_logic -- 消抖后输出);end entity;--消抖architecture behavioral of debounce isbegin-- 临时短路消抖信号直通方便在短仿真时间内验证逻辑key_out key_in;end architecture;仿真时间有限所以消抖这一部分只能换位这种仿真成果这是一次错误一次正确这是两次错误一次正确再次声明代码部分是均为ai生成的注释部分是我为了应对老师检查自己乱写的有错是肯定会存在的。以上所有内容只做分享不会也禁止一切商业性行为。侵权私信包删

相关新闻