当前位置:网站首页>【按键消抖】基于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
边栏推荐
- Overview of super-resolution reconstruction of remote sensing images
- [Qt5] QT QWidget immediately appears and disappears
- 登录mysql输入密码时报错,ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES
- 简述C语言中的符号和链接库
- Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art
- C#(二十八)之C#鼠标事件、键盘事件
- EDCircles: A real-time circle detector with a false detection control 翻译
- How to standardize the deployment of automated testing?
- [prediction model] difference method model
- 3.2 detailed explanation of rtthread serial port device (V2)
猜你喜欢
给新人工程师组员的建议
mysql关于自增长增长问题
C language judgment, ternary operation and switch statement usage
1、工程新建
在字节做测试5年,7月无情被辞,想给划水的兄弟提个醒
Schnuka: visual positioning system working principle of visual positioning system
EDCircles: A real-time circle detector with a false detection control 翻译
pytorch加载数据
Force buckle 1189 Maximum number of "balloons"
教你用Pytorch搭建一个自己的简单的BP神经网络( 以iris数据集为例 )
随机推荐
自动化测试怎么规范部署?
[slam] orb-slam3 parsing - track () (3)
Oracle ORA error message
Suggestions for new engineer team members
施努卡:3d视觉检测应用行业 机器视觉3d检测
Pytoch foundation - (2) mathematical operation of tensor
RT thread -- FTP of LwIP (2)
Basic concepts of LTE user experience
Remote Sensing Image Super-resolution and Object Detection: Benchmark and State of the Art
Recommended papers on remote sensing image super-resolution
Prime Protocol宣布在Moonbeam上的跨链互连应用程序
Blue style mall website footer code
Mysqldump data backup
深入刨析的指针(题解)
SAP ALV单元格级别设置颜色
Do you know cookies, sessions, tokens?
Python implementation of maddpg - (1) openai maddpg environment configuration
2.2 fonctionnement stm32 GPIO
Image super-resolution using deep convolutional networks(SRCNN)解读与实现
2.2 STM32 GPIO operation