15 ABC基于状态机的按键消抖原理与状态转移图

1. 基于状态机的按键消抖

1.1 什么是按键?

从按键结构图10-1可知,按键按下时,接点(端子)与导线接通,松开时,由于弹簧的反作用力,接点(端子)与导线断开。

从原理图10-2可知,按键按下时为低电平,未按下为高电平

1.2 为什么要消抖?

1.3 基于按键消抖的状态转移图

2. 写设计代码,仿真代码并仿真(未使用随机函数的测试)

1. 设计代码

module key_filter(clk,rstn,key,
//    key_p_flag,
//    key_r_flag,key_flag,key_state
);input clk;input rstn;input key;
//   output reg key_p_flag;
//   output reg key_r_flag;output reg key_flag;output reg key_state;//边沿检测reg [1:0] r_key;always@(posedge clk)r_key <= {r_key[0], key}; 
//    reg [1:0] r_key;    
//    always@(posedge clk)begin
//        r_key[0] <= key;
//        r_key[1] <= r_key[0]; 
//    endwire nedge_key;wire pedge_key;assign nedge_key = (r_key == 2'b10);assign pedge_key = (r_key == 2'b01);reg [1:0]state;reg [19:0] cnt;always@(posedge clk or negedge rstn)if(!rstn)beginstate <= 0;cnt <= 0;
//        key_p_flag <= 0;
//        key_r_flag <= 0;key_flag <= 0;key_state <= 1;endelsecase(state)0: begin
//            key_r_flag <= 0;key_flag <= 0;if(nedge_key == 1) beginstate <= 1;endelsestate <= 0;end 1:beginif((pedge_key == 1) && (cnt < 1000000 - 1))beginstate <= 0;cnt <= 0;endelse if((pedge_key == 0) && (cnt >= 1000000 - 1))beginstate <= 2;
//               key_p_flag <= 1'd1;key_flag <= 1'd1;key_state <= 0;cnt <= 0;endelsecnt <= cnt + 1'd1;end2:begin
//           key_p_flag <= 0;key_flag <= 0;if(pedge_key == 1)state <= 3;elsestate <= 2;end3:beginif((nedge_key == 1) && (cnt < 1000000 - 1))beginstate <= 2;cnt <= 0;endelse if((nedge_key == 0) && (cnt >= 1000000 - 1))beginstate <= 0;
//                key_r_flag <= 1;key_flag <= 1'd1;key_state <= 1;cnt <= 0;endelsecnt <= cnt + 1'd1;endendcaseendmodule

2. 仿真代码

`timescale 1ns / 1psmodule key_filter_tb();reg clk;reg rstn;reg key;
//    wire key_p_flag;
//    wire key_r_flag;wire key_flag;wire key_state;key_filter key_filter_inst(.clk(clk),.rstn(rstn),.key(key),
//        .key_p_flag(key_p_flag),
//       .key_r_flag(key_r_flag),.key_flag(key_flag),.key_state(key_state));initial clk = 1;always #10 clk = ~clk;initial beginrstn = 0;key = 1;#201;rstn = 1;#200;key = 1;#50000000;key = 0;#30000;key = 1;#30000;key = 0;#30000;key = 1;#30000;key = 0;#50000000;key = 1;#30000;key = 0;#30000;key = 1;#30000;key = 0;#30000;key = 1;#50000000;$stop;endendmodule

3. 仿真波形

 3. 基于verilog系统函数random的随机测试下的按键抖动(tb编写语法)

通过系统函数random产生一个随机的延迟值,来模拟真实情况下的延迟。

3.1 系统函数random的两个例子:

1. 产生一个[-(b+1): (b-1)]的随机数:$random% b;

2.产生一个[0: b-1]的随机数:{$random}% b;;

修改后的仿真代码:

`timescale 1ns / 1psmodule key_filter_tb();reg clk;reg rstn;reg key;
//    wire key_p_flag;
//    wire key_r_flag;wire key_flag;wire key_state;key_filter key_filter_inst(.clk(clk),.rstn(rstn),.key(key),
//        .key_p_flag(key_p_flag),
//       .key_r_flag(key_r_flag),.key_flag(key_flag),.key_state(key_state));initial clk = 1;always #10 clk = ~clk;reg [19:0] rand;initial beginrstn = 0;key = 1;#201;rstn = 1;#200;press_key(1);$stop;endtask press_key;input [2:0] seed;beginkey = 1;#20000000; repeat(5) beginrand = {$random(seed)} % 9999999; //产生0到9999999ns的延迟#rand key = ~key;endkey = 0;#40000000;repeat(5) beginrand = {$random(seed)} % 9999999; //产生0到9999999ns的延迟#rand key = ~key;endkey = 1;#40000000;endendtaskendmodule

4. 调试(产生多余38ns的原因)

 

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/680114.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【开源】SpringBoot框架开发天沐瑜伽馆管理系统

目录 一、摘要1.1 项目介绍1.2 项目录屏 二、功能模块2.1 数据中心模块2.2 瑜伽课程模块2.3 课程预约模块2.4 系统公告模块2.5 课程评价模块2.6 瑜伽器械模块 三、系统设计3.1 实体类设计3.1.1 瑜伽课程3.1.2 瑜伽课程预约3.1.3 系统公告3.1.4 瑜伽课程评价 3.2 数据库设计3.2.…

牛客周赛 Round 32 F.小红的矩阵修改【三进制状态压缩dp】

原题链接&#xff1a;https://ac.nowcoder.com/acm/contest/75174/F 时间限制&#xff1a;C/C 1秒&#xff0c;其他语言2秒 空间限制&#xff1a;C/C 262144K&#xff0c;其他语言524288K 64bit IO Format: %lld 题目描述 小红拿到了一个字符矩阵&#xff0c;矩阵中仅包含&q…

java 执行方式和类加载过程

java默认属于混合执行&#xff1a; 编译和解释并存 java先进行解释执行&#xff0c;遇到多次重复的代码会把它编程成可执行文件&#xff0c;方便下次直接执行。 可以通过VM参数来修改执行方式。 类加载过程

Nacos、Eureka、Zookeeper、Consul对比

开发中&#xff0c;经常需要对微服务进行管理&#xff0c;所以需要引入一些服务治理的中间件&#xff0c;用于注册、发现服务&#xff0c;常见的服务治理中间件为 服务治理中间件 【1】Nacos 【2】Eureka 【3】Zookeeper 【4】Consul&#xff08;Consul 所在的 HashiCorp 公司…

从完成[flutter竖向显示文字]到对实现方式[Rich Text和Text Span]的一些整理

前言 完成的需求是竖向显示文字&#xff0c;而已有的RotatedBox虽然可以让文字内部控件进行指定角度的旋转&#xff0c;但是不能保持文字仍正常显示&#xff08;它会因为旋转横着&#xff09;&#xff0c;遂尝试Rich Text和Text Span的方式&#xff0c;这两个我曾在android有略…

红队笔记Day2 -->上线不出网机器

今天就来讲一下在企业攻防中如何上线不出网的机器&#xff01;&#xff01; 1.基本网络拓扑 基本的网络拓扑就是这样 以下是对应得的P信息&#xff0c;其中的52网段充当一个内网的网段&#xff0c;而111充当公网网段 先ping一下&#xff0c;确保外网ping不通内网&#xff0c;内…

文档类图像的智能识别,百度、阿里、华为腾讯开放接口

文档类图像的智能识别是指利用人工智能技术对文档图像进行自动识别和信息提取。在我国&#xff0c;百度、阿里、华为和腾讯等科技巨头都提供了相应的开放接口&#xff0c;方便开发者集成和使用文档类图像识别功能。以下是这些公司提供的相关开放接口&#xff1a; 1. 百…

微信小程序(四十一)wechat-http的使用

注释很详细&#xff0c;直接上代码 上一篇 新增内容&#xff1a; 1.模块下载 2.模块的使用 在终端输入npm install wechat-http 没有安装成功vue的先看之前的一篇 微信小程序&#xff08;二十&#xff09;Vant组件库的配置- 如果按以上的成功配置出现如下报错先输入以下语句 …

leetcode 24

24. 两两链表交换链表中的节点 已经给出了链表节点结构类&#xff1a; public class ListNode {int val;ListNode next;ListNode() {}ListNode(int val) { this.val val; }ListNode(int val, ListNode next) { this.val val; this.next next; }} 简而言之&#xff0c;我们…

DS:单链表实现队列

创作不易&#xff0c;友友们来个三连支持吧&#xff01; 一、队列的概念 队列&#xff1a;是只允许在一端进行插入数据操作&#xff0c;在另一端进行删除数据操作的特殊线性表&#xff0c;队列具有先进先出FIFO(First In First Out)的特点。 入队列&#xff1a;进行插入操作…

leetcode题目记录

文章目录 单调栈[127. 单词接龙](https://leetcode.cn/problems/word-ladder/)[139. 单词拆分](https://leetcode.cn/problems/word-break/)[15. 三数之和](https://leetcode.cn/problems/3sum/)[140. 单词拆分 II](https://leetcode.cn/problems/word-break-ii/)[113. 路径总和…

《数字孪生城市建设指引报告(2023年)》指引智慧城市行动方向

2023年12月27日&#xff0c;中国信息通信研究院&#xff08;简称“中国信通院”&#xff09;产业与规划研究所、中国互联网协会数字孪生技术应用工作委员会和苏州工业园区数字孪生创新坊联合发布《数字孪生城市建设指引报告&#xff08;2023年&#xff09;》。该报告提出了三大…

PostgreSQL的学习心得和知识总结(一百二十八)|构建 PostgreSQL 负载测试器

目录结构 注:提前言明 本文借鉴了以下博主、书籍或网站的内容,其列表如下: 1、参考书籍:《PostgreSQL数据库内核分析》 2、参考书籍:《数据库事务处理的艺术:事务管理与并发控制》 3、PostgreSQL数据库仓库链接,点击前往 4、日本著名PostgreSQL数据库专家 铃木启修 网站…

Linux:docker在线仓库(docker hub 阿里云)基础操作

把镜像放到公网仓库&#xff0c;这样可以方便大家一起使用&#xff0c;当需要时直接在网上拉取镜像&#xff0c;并且你可以随时管理自己的镜像——删除添加或者修改。 1.docker hub仓库 2.阿里云加速 3.阿里云仓库 由于docker hub是国外的网站&#xff0c;国内的对数据的把控…

.NET Core 3 foreach中取索引index

for和foreach 循环是 C# 开发人员工具箱中最有用的构造之一。 在我看来&#xff0c;迭代一个集合比大多数情况下更方便。 它适用于所有集合类型&#xff0c;包括不可索引的集合类型&#xff08;如 &#xff0c;并且不需要通过索引访问当前元素&#xff09;。 但有时&#xf…

Verilog刷题笔记30

题目&#xff1a; You are provided with a BCD one-digit adder named bcd_fadd that adds two BCD digits and carry-in, and produces a sum and carry-out. 解题&#xff1a; module top_module( input [399:0] a, b,input cin,output cout,output [399:0] sum );reg [99…

代码随想录day20--二叉树的应用8

LeetCode669.修剪二叉搜索树 题目描述&#xff1a; 给你二叉搜索树的根节点 root &#xff0c;同时给定最小边界low 和最大边界 high。通过修剪二叉搜索树&#xff0c;使得所有节点的值在[low, high]中。修剪树 不应该 改变保留在树中的元素的相对结构 (即&#xff0c;如果没…

【ES】--Elasticsearch的分词器深度研究

目录 一、问题描述及分析二、analyze分析器原理三、 multi-fields字段支持多场景搜索(如同时简繁体、拼音等)1、ts_match_analyzer配置分词2、ts_match_all_analyzer配置分词3、ts_match_1_analyzer配置分词4、ts_match_2_analyzer配置分词5、ts_match_3_analyzer配置分词6、ts…

ctfshow-文件上传(web151-web161)

目录 web151 web152 web153 web154 web155 web156 web157 web158 web159 web160 web161 web151 提示前台验证不可靠 那限制条件估计就是在前端设置的 上传php小马后 弹出了窗口说不支持的格式 查看源码 这一条很关键 这种不懂直接ai搜 意思就是限制了上传类型 允许…

Ubuntu Desktop - Files Preferences

Ubuntu Desktop - Files Preferences 1. Behavior2. ViewsReferences 1. Behavior Go to file browser’s Menu -> Edit -> Preferences -> Behavior 2. Views Go to file browser’s Menu -> Edit -> Preferences -> Views ​​​ References [1] Yong…