当前位置:网站首页>【按键消抖】基于FPGA的按键消抖模块开发
【按键消抖】基于FPGA的按键消抖模块开发
2022-07-06 03:45:00 【fpga和matlab】
1.软件版本
QUARTUSII8.1
Modelsim6.5d
2.系统源码
module tops(
i_clk, //100M
i_rst, //系统复位功能,高电平复位,如果不使用这个角,那么一直接低电平即可
i_input1, //按键输入1
i_input2, //按键输入2
o_output1,//脉冲输出1
o_output2,//脉冲输出2
test_cnt1,//测试计数器1
test_cnt2,//测试计数器2
test_enable1,//测试使能信号
test_enable2 //测试使能信号
);
//100M等于10ns,所以1s中就是10_000_000个100M时钟周期
//仿真的时候,为了方便看到仿真效果,所以将10_000_000改为一个较小的值10_000
//parameter NUM = 32'd10000000; //实际用
parameter NUM = 32'd100; //仿真用
input i_clk;//100M为10ns
input i_rst;
input i_input1;
input i_input2;
output o_output1;
output o_output2;
output[31:0]test_cnt1;
output[31:0]test_cnt2;
output test_enable1;
output test_enable2;
reg o_output1 = 1'b0;
reg o_output2 = 1'b0;
reg test_enable1 = 1'b0;
reg test_enable2 = 1'b0;
reg[31:0]cnt1 = 32'd0;
reg[31:0]cnt2 = 32'd0;
reg flag1 = 1'b1;
reg flag2 = 1'b1;
always @(posedge i_clk or posedge i_rst)//处理主要进程
begin
if(i_rst)//系统复位
begin
test_enable1 <= 1'b0;//定义使能信号
test_enable2 <= 1'b0;//定义使能信号
cnt1 <= 32'd0;
cnt2 <= 32'd0;
flag1 <= 1'b1;
flag2 <= 1'b1;
end
else begin
if(i_input1 == 1'b0 & i_input2 == 1'b1 & flag1 == 1'b1)//按下按键1,不按按键2
begin
//1s钟内使能维持10_000_000个时钟周期
cnt2 <= 32'd0;
if(cnt1 < NUM)//不到1s
begin
cnt1 <= cnt1 + 32'd1;
test_enable1 <= 1'b1;//输出1个脉冲,//按下按钮后,持续1s钟
test_enable2 <= 1'b0;
flag1 <= 1'b1;//用来屏蔽第二个按钮
flag2 <= 1'b0;//用来屏蔽第二个按钮
end
if(cnt1 == NUM)//到1s,停止输出
begin
cnt1 <= cnt1;
test_enable1 <= 1'b0;
test_enable2 <= 1'b0;
flag1 <= 1'b1;
flag2 <= 1'b1;
end
end
if(i_input1 == 1'b1 & i_input2 == 1'b0 & flag2 == 1'b1)//按下按键2,不按按键1
begin
//50s钟内使能维持
cnt1 <= 32'd0;
if(cnt2 < 50*NUM)//不到50s
begin
cnt2 <= cnt2 + 32'd1;
test_enable1 <= 1'b0;//输出1个脉冲,//按下按钮后,持续1s钟
test_enable2 <= 1'b1;
flag1 <= 1'b0;//用来屏蔽第1个按钮
flag2 <= 1'b1;//用来屏蔽第1个按钮
end
if(cnt2 == 50*NUM)//到1s,停止输出
begin
cnt2 <= cnt2;
test_enable1 <= 1'b0;
test_enable2 <= 1'b0;
flag1 <= 1'b1;
flag2 <= 1'b1;
end
end
if(i_input1 == 1'b1 & i_input2 == 1'b1)//不发生按键操作
begin
cnt1 <= 32'd0;
cnt2 <= 32'd0;
end
end
end
assign test_cnt1 = cnt1;
assign test_cnt2 = cnt2;
//下面根据是能信号,输出脉冲
//定义两个脉冲计数器
reg[31:0]pcnt1 = 32'd0;
reg[31:0]pcnt2 = 32'd0;
always @(posedge i_clk or posedge i_rst)//处理主要进程
begin
if(i_rst)//系统复位
begin
pcnt1 <= 32'd0;
pcnt2 <= 32'd0;
o_output1 <= 1'b0;
o_output2 <= 1'b0;
end
else begin
if(test_enable1 == 1'b1)//1s内一个100ns的脉冲,即1s内发生一个10M的脉冲信号
begin
pcnt1 <= pcnt1 + 32'd1;
if(pcnt1 < 32'd10)
begin
o_output1 <= 1'b1;//产生100ns的信号
end
else begin
o_output1 <= 1'b0;
end
end
else begin
pcnt1 <= 32'd0;
o_output1 <= 1'b0;
end
if(test_enable2 == 1'b1)//50s内50个100ns的脉冲,即1s内发生一个10M的脉冲信号
begin
if(pcnt2 == NUM-1)
begin
pcnt2 <= 32'd0;
end
else begin
pcnt2 <= pcnt2 + 32'd1;
end
if(pcnt2 < 32'd10)
begin
o_output2 <= 1'b1;//产生100ns的信号
end
else begin
o_output2 <= 1'b0;
end
end
else begin
pcnt2 <= 32'd0;
o_output2 <= 1'b0;
end
end
end
endmodule 3.仿真结论
QII自带仿真说明:

我们将波形进行局部放大:

第一部分,可以看到input1为高,input2为低,说明2按下了,所以output2产生连续的50个脉冲。

第二部分,input1为低,第一个按键按下了,所以只产生一个高电平信号

第三部分,还是第一个按钮被按下了,所以只产生一个脉冲。
Modelsim仿真说明:

我们将仿真进行局部放大:

按键一按下:

按键二被按下;

还是按键二被按下。
A35-09
边栏推荐
- 施努卡:什么是视觉定位系统 视觉系统如何定位
- Schnuka: 3D vision detection application industry machine vision 3D detection
- 有条件地 [JsonIgnore]
- WPF效果第一百九十一篇之框选ListBox
- C language judgment, ternary operation and switch statement usage
- Containerization Foundation
- Blue Bridge Cup - day of week
- 2. GPIO related operations
- [slam] lidar camera external parameter calibration (Hong Kong University marslab) does not need a QR code calibration board
- 2.1 rtthread pin设备详解
猜你喜欢

JS Vanke banner rotation chart JS special effect

Cubemx 移植正点原子LCD显示例程
![[risc-v] external interrupt](/img/9d/eb1c27e14045d9f1f690f4a7f5c596.jpg)
[risc-v] external interrupt

How to standardize the deployment of automated testing?

Blue Bridge Cup - day of week

Pointer written test questions ~ approaching Dachang

Recommended papers on remote sensing image super-resolution

Blue style mall website footer code

数据分析——seaborn可视化(笔记自用)

Align items and align content in flex layout
随机推荐
Serial port-rs232-rs485-ttl
Microkernel structure understanding
Flask learning and project practice 9: WTF form verification
Introduction to data types in MySQL
How do we make money in agriculture, rural areas and farmers? 100% for reference
Quick sort function in C language -- qsort
3分钟带你了解微信小程序开发
Codeforces Global Round 19
EDCircles: A real-time circle detector with a false detection control 翻译
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
SAP ALV颜色代码对应颜色(整理)
Recommended papers on remote sensing image super-resolution
Yyds dry inventory what is test driven development
2.13 weekly report
3.1 detailed explanation of rtthread serial port device (V1)
多项目编程极简用例
[meisai] meisai thesis reference template
暑期刷题-Day3
Suggestions for new engineer team members
施努卡:3d视觉检测应用行业 机器视觉3d检测