当前位置:网站首页>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级门级电路:

边栏推荐
- Fluorescein-PEG-CLS,胆固醇-聚乙二醇-荧光素科研试剂
- A simple understanding of TCP, learn how to shake hands, wave hands and various states
- OpenCV 图像拼接
- 栈的压入、弹出序列
- BMN: Boundary-Matching Network for Temporal Action Proposal Generation阅读笔记
- Walk the Maze BFS
- 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.
- Zilliz 2023 Fall Campus Recruitment Officially Launched!
- redis持久化方式
- The "interaction design" battle of the smart cockpit
猜你喜欢

Creo 9.0创建几何点

电子邮件安全或面临新威胁!

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

初始 List 接口

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

简单了解下 TCP,学习握手和挥手以及各种状态到底是怎么样的

override learning (parent and child)

The salary of soft testers at each stage, come to Kangkang, how much can you get?

The principle and use of AOSP CameraLatencyHistogram

CAS: 178744-28-0, mPEG-DSPE, DSPE-mPEG, methoxy-polyethylene glycol-phosphatidylethanolamine supply
随机推荐
ros mavros stereo读取rosbag并记录IMU和图片到文件夹
Scala基础【正则表达式、框架式开发原则】
The salary of soft testers at each stage, come to Kangkang, how much can you get?
并查集详解
BMN: Boundary-Matching Network for Temporal Action Proposal Generation Reading Notes
(PC+WAP)织梦模板不锈钢类网站
(PC+WAP)织梦模板螺钉手柄类网站
跨域的学习
使用unbound在RHEL7上搭建DNS服务
【LeetCode】最长回文子序列(动态规划)
3D 语义分割——2DPASS
The Chinese Valentine's Day event is romantically launched, don't let the Internet slow down and miss the dark time
POE交换机全方位解读(上)
1067 Sort with Swap(0, i)
用队列模拟实现栈
响应式织梦模板塑身瑜伽类网站
HNUCM 2022年暑假ACM搜索专项练习
初始 List 接口
Pytest learn-setup/teardown
禾匠编译错误记录