当前位置:网站首页>【按键消抖】基于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
边栏推荐
- 自动化测试怎么规范部署?
- Exchange bottles (graph theory + thinking)
- After five years of testing in byte, I was ruthlessly dismissed in July, hoping to wake up my brother who was paddling
- Esbuild & SWC: a new generation of construction tools
- 遥感图像超分辨重建综述
- 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
- [meisai] meisai thesis reference template
- MADDPG的pythorch实现——(1)OpenAI MADDPG环境配置
- 施努卡:3d视觉检测应用行业 机器视觉3d检测
- WPF效果第一百九十一篇之框选ListBox
猜你喜欢
mysql从一个连续时间段的表中读取缺少数据
JS music online playback plug-in vsplayaudio js
如何修改表中的字段约束条件(类型,default, null等)
Python implementation of maddpg - (1) openai maddpg environment configuration
LTE CSFB test analysis
3.1 rtthread 串口设备(V1)详解
深入刨析的指针(题解)
Esbuild & SWC: a new generation of construction tools
2.2 STM32 GPIO操作
JS Vanke banner rotation chart JS special effect
随机推荐
Image super-resolution using deep convolutional networks(SRCNN)解读与实现
1.16 - 校验码
Blue Bridge Cup - day of week
2.2 STM32 GPIO操作
Flask learning and project practice 9: WTF form verification
JS音乐在线播放插件vsPlayAudio.js
MySQL reads missing data from a table in a continuous period of time
RT-Thread--Lwip之FTP(2)
Brush questions in summer -day3
Mapping between QoE and KQI
给新人工程师组员的建议
The solution of permission denied (750 permissions should be used with caution)
BUAA计算器(表达式计算-表达式树实现)
2.2 fonctionnement stm32 GPIO
登录mysql输入密码时报错,ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: NO/YES
Image super resolution using deep revolutionary networks (srcnn) interpretation and Implementation
[matlab] - draw a five-star red flag
Mathematical modeling regression analysis relationship between variables
C#(二十八)之C#鼠标事件、键盘事件
UDP reliable transport protocol (quic)