当前位置:网站首页>FPGA按键消抖+蜂鸣器
FPGA按键消抖+蜂鸣器
2022-08-03 23:34:00 【三个刺客】
一:按键消抖
解决方法一:

缺点:抖动时间不确定,采集信号不一定稳定
解决方法二:

Verilog HDL编写
key_debounce.v
module key_debounce(
input wire clk,
input wire rst_n,
input wire key,
output reg flag, //判断抖动是否消除的标志信号,0为抖动,1为抖动结束
output reg key_value //消抖后稳定的按键值给到蜂鸣器模块
);
//定义20ms延迟计数器,0.2s,1_000_000次
reg [19:0] delay_cnt;
//寄存依次key的值用来判断按键是否消抖成功
reg key_reg;
//20ms延时计数器
[email protected](posedge clk or negedge rst_n)begin
if(!rst_n)
begin
key_reg <= 1'b1; //复位信号,设置按键无效
delay_cnt <= 1'b0; //计数器设置为0
end
else
begin
key_reg <= key;
if(key_reg ^ key) //当这一次key值和上一次key值不一样,证明正在抖动
delay_cnt <= 20'd1_000_000; //延迟时间20ms
else if(delay_cnt > 0)
delay_cnt <= delay_cnt - 1; //没有抖动,开始20ms倒计时
else
delay_cnt <= 1'b0;
end
end
//根据延时计数器获取按键状态以及按键值
[email protected](posedge clk or negedge rst_n)begin
if(!rst_n)
begin
flag <= 1'b0; //复位信号,设置信号标志为抖动
key_value <= 1'b1; //设置抽样值为1
end
else
begin
if(delay_cnt == 20'd1) //倒计时1_000_000到1
begin
flag <= 1'b1;
key_value <= key; //稳定20ms后将key值给到key_value
end
else
begin
flag <= 1'b0;
key_value <= key_value; //20ms内先不取样
end
end
end
endmodule
说明:
当上一次的key值和这次的值不一样时,证明正在抖动,给计时器赋一个20ms的值,也就是1_000_000次,这个根据时钟频率得出。当key值稳定后,开始倒计时减一,当计时器减到1时,将key值输出。
二:按键消抖+蜂鸣器
beep.v
module beep(
input clk,
input rst_n,
input key_value,
input flag,
output reg beep
);
//蜂鸣器控制模块
[email protected](posedge clk or negedge rst_n)begin
if(!rst_n)
beep <= 1'b0;
else if(flag == 1'b1 && (!key_value))
beep <= ~beep;
else
beep <= beep;
end
endmodule
这里的蜂鸣器和按键消抖分别在两个文件里面,需要设计顶层文件调用者两个模块
key_beep_top.v
module key_beep_top(
input wire clk,
input wire rst_n,
input wire key,
output wire beep
);
wire key_value;
wire flag;
//例化按键消抖模块
key_debounce inst_key_debounce(
.clk (clk ),
.rst_n (rst_n ),
.key (key ),
.flag (flag ),
.key_value (key_value)
);
//例化蜂鸣器模块
beep inst_beep(
.clk (clk ),
.rst_n (rst_n ),
.key_value (key_value),
.flag (flag ),
.beep (beep )
);
endmodule
查看RTL级门级电路:

边栏推荐
- The salary of soft testers at each stage, come to Kangkang, how much can you get?
- 牛客2022 暑期多校3 H Hacker(SAM + 线段树查询区间内部最大子段和)
- [Paper Reading] TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve
- - the skip/skipif Pytest learning
- ML之interpret:基于titanic泰坦尼克是否获救二分类预测数据集利用interpret实现EBC模型可解释性之全局解释/局部解释案例
- Kotlin - extension functions and operator overloading
- 【职场杂谈】售前与销售工作配合探讨
- [RYU] rest_router.py source code analysis
- 2022/8/3 考试总结
- P1449 后缀表达式
猜你喜欢

OpenCV 图像拼接

我的祖国

ML's yellowbrick: A case of interpretability (threshold map) for LoR logistic regression model using yellowbrick based on whether Titanic was rescued or not based on the two-class prediction dataset

CAS: 178744-28-0, mPEG-DSPE, DSPE-mPEG, methoxy-polyethylene glycol-phosphatidylethanolamine supply

软件测试内卷严重,如何提升自己的竞争力呢?

设置工作模式与环境(下):探查和收集信息

走迷宫 BFS

RSS订阅微信公众号初探-feed43

【OpenCV图像处理】 图像拼接技术

Creo9.0 绘制中心线
随机推荐
ts用法大全
IELTS essay writing template
图论-虚拟节点分层建图
OpenCV 图像拼接
2022/8/3 考试总结
What is memoization and what is it good for?
POE交换机全方位解读(上)
电子邮件安全或面临新威胁!
libnet
Walk the Maze BFS
Jmeter-断言
libnet
单例模式使用饿汉式和懒汉式创建一定安全?很多人不知
Scala基础【正则表达式、框架式开发原则】
【论文阅读】TRO 2021: Fail-Safe Motion Planning for Online Verification of Autonomous Vehicles Using Conve
Creo 9.0在草图环境中创建坐标系
LeetCode 0155. 最小栈
跨域的学习
With the rise of concepts such as metaverse and web3.0, many digital forms such as digital people and digital scenes have begun to appear.
七夕?new一个对象