数电课设——数字密码锁
题目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生成的注释部分是我为了应对老师检查自己乱写的有错是肯定会存在的。以上所有内容只做分享不会也禁止一切商业性行为。侵权私信包删

相关新闻

卡梅德生物科普:CD24(分化簇24)的生物学机制与研究前沿

卡梅德生物科普:CD24(分化簇24)的生物学机制与研究前沿

在生物医学研究的广阔领域中,蛋白靶点作为连接细胞生命活动与机体病理状态的关键节点,始终是科研探索的核心。CD24(分化簇24),作为一种广泛分布于细胞表面的功能性膜蛋白,凭借其独特的免疫调控与细胞调节双…

2026/6/29 18:05:43阅读更多 →
企业APP架构选型:D-coding PaaS路径与工程化交付实践

企业APP架构选型:D-coding PaaS路径与工程化交付实践

在上海选择APP开发公司,企业真正关心的往往不是“谁的介绍更好听”,而是项目能不能按业务目标落地,后续能不能持续迭代,数据、接口、运维和成本是否可控。围绕“上海APP开发公司哪家好”“上海APP开发靠谱公司推荐”等问题&#x…

2026/6/29 18:05:43阅读更多 →
Windows风扇控制终极指南:Fan Control如何帮你告别噪音烦恼

Windows风扇控制终极指南:Fan Control如何帮你告别噪音烦恼

Windows风扇控制终极指南:Fan Control如何帮你告别噪音烦恼 【免费下载链接】FanControl.Releases This is the release repository for Fan Control, a highly customizable fan controlling software for Windows. 项目地址: https://gitcode.com/GitHub_Trendi…

2026/6/29 18:00:43阅读更多 →
基于AScript的SQL脚本语言发布啦!

基于AScript的SQL脚本语言发布啦!

一、介绍 支持SqlServer/MySql基础语法和数据类型: 支持SELECT查询语法:FROM/WHERE/LEFT JOIN/RIGHT JOIN/INNER JOIN/GROUP BY/ORDER BY/LIMIT支持INSERT插入语法支持UPDATE修改语法支持DELETE删除语法支持创建存储过程:Sqlserver/MySql语…

2026/6/29 19:16:02阅读更多 →
SpringBoot自动装配和starter

SpringBoot自动装配和starter

自动装配SpringBootApplication 注解中包含了EnableAutoConfiguration注解,这个注解底层又是import 注解,导入自动配置类导入选择器(AutoConfigurationImportSelecor ),这个选择器会去读取约定配置文件,从文…

2026/6/29 19:16:02阅读更多 →
统计学、数据科学、大数据管理,哪个更适合做数据?2026大学生选方向不迷路

统计学、数据科学、大数据管理,哪个更适合做数据?2026大学生选方向不迷路

很多同学想做数据,却卡在专业选择上:统计学偏理论,数据科学偏技术,大数据管理偏业务与治理。到了 2026 年,企业要的早已不是“会做表”的人,而是能把数据变成决策、产品和增长的人。也正因为这样&#xff0…

2026/6/29 19:16:02阅读更多 →
低功耗单片机MCU芯片主流型号盘点

低功耗单片机MCU芯片主流型号盘点

一、低功耗单片机MCU基础概念解析 低功耗单片机本质是集成度极高的微型控制芯片,行业内也常被称作MCU(微控制单元)。不同于普通微处理器,低功耗单片机通过超大规模集成电路技术,将CPU、运行内存RAM、存储ROM、外设接口…

2026/6/29 19:16:02阅读更多 →
AWS VPC 和 ALB 部署规范

AWS VPC 和 ALB 部署规范

覆盖网络架构设计、CloudFormation 模板、安全组规则、健康检查配置、部署检查清单。 一、命名规范 格式: {environment}-{project}-{resource-type}-{description}资源 格式 示例 VPC {project}-vpc govee-vpc 子网 `{project}-subnet-{public private}{n}-{az}` 路由表 `{pr…

2026/6/29 19:16:02阅读更多 →
深度解析GHelper:华硕ROG设备性能优化的专业替代方案

深度解析GHelper:华硕ROG设备性能优化的专业替代方案

深度解析GHelper:华硕ROG设备性能优化的专业替代方案 【免费下载链接】g-helper Lightweight Armoury Crate alternative for Asus laptops with nearly the same functionality. Works with ROG Zephyrus, Flow, TUF, Strix, Scar, ProArt, Vivobook, Zenbook, Exp…

2026/6/29 19:11:02阅读更多 →
AI Coding 六个月真实ROI账本:产品经理的血泪教训,研发的冷静忠告

AI Coding 六个月真实ROI账本:产品经理的血泪教训,研发的冷静忠告

6个月前的2025年12月,Boris Cherny 公开宣布自己卸载了 IDE。一时间,Vibe Coding 成了全行业最热的话题。6个月后,当我们回过头来拉一份真实账本,发现事情远没有"一句话生成一个App"那么浪漫。本文从产品经理和研发两个…

2026/6/29 3:27:55阅读更多 →
审计来了,数据权限全开——审计走了,怎么确保权限全部关掉?

审计来了,数据权限全开——审计走了,怎么确保权限全部关掉?

引言:审计结束三个月了,审计员的权限还没关某城商行每年按照监管要求开展至少一次数据安全审计。审计期间,内审部门需要抽样检查各类业务数据——交易流水、客户信息、员工操作日志、权限配置记录。这些数据分布在不同系统中,审计…

2026/6/29 2:19:08阅读更多 →
如何在3秒内从普通图片生成专业级法线贴图:DeepBump的终极指南

如何在3秒内从普通图片生成专业级法线贴图:DeepBump的终极指南

如何在3秒内从普通图片生成专业级法线贴图:DeepBump的终极指南 【免费下载链接】DeepBump Normal & height maps generation from single pictures 项目地址: https://gitcode.com/gh_mirrors/de/DeepBump 还在为3D建模中的纹理制作而烦恼吗?…

2026/6/29 0:01:47阅读更多 →
OCAuxiliaryTools:终极OpenCore配置工具,让黑苹果安装从未如此简单!

OCAuxiliaryTools:终极OpenCore配置工具,让黑苹果安装从未如此简单!

OCAuxiliaryTools:终极OpenCore配置工具,让黑苹果安装从未如此简单! 【免费下载链接】OCAuxiliaryTools Cross-platform GUI management tools for OpenCore(OCAT) 项目地址: https://gitcode.com/gh_mirrors/oc/OCA…

2026/6/29 0:01:47阅读更多 →
终极Windows 11精简指南:使用tiny11builder快速创建纯净系统镜像

终极Windows 11精简指南:使用tiny11builder快速创建纯净系统镜像

终极Windows 11精简指南:使用tiny11builder快速创建纯净系统镜像 【免费下载链接】tiny11builder Scripts to build a trimmed-down Windows 11 image. 项目地址: https://gitcode.com/GitHub_Trending/ti/tiny11builder 你是否厌倦了Windows 11系统自带的20…

2026/6/29 0:01:47阅读更多 →