按键消抖

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module key_debounce
#(parameter KEY_WIDTH = 4)
(
    input                   clk,
    input                   rst_n,
    input   [KEY_WIDTH:1]   i_key,
    output reg [KEY_WIDTH:1] key_out
);
 
reg [KEY_WIDTH:1] key_sample1,key_samp1_locked;
 
always @ (posedge clk,negedge rst_n)
begin
    if(!rst_n)
        key_sample1 <= {KEY_WIDTH{1'b1}};
    else
        key_sample1 <= i_key;
end
 
always @ (posedge clk,negedge rst_n)
begin
    if(!rst_n)
        key_samp1_locked <= {KEY_WIDTH{1'b1}};
    else
        key_samp1_locked <= key_sample1;
end
 
wire [KEY_WIDTH:1] key_changed1 = key_samp1_locked &(~key_sample1);
 
reg [19:0] cnt;
 
always @ (posedge clk,negedge rst_n)
begin
    if(!rst_n)
        cnt <= 20'b0;
    else if(key_changed1)
        cnt <= 20'b0;
    else
        cnt <= cnt + 1'b1;
end
 
reg [KEY_WIDTH:1] key_samp2,key_samp2_locked;
 
always @ (posedge clk,negedge rst_n)
begin
    if(!rst_n)
        key_samp2 <= {KEY_WIDTH{1'b1}};
    else if(cnt == 20'hF_FFFF)
        key_samp2 <= i_key;
end
 
always @ (posedge clk,negedge rst_n)
begin
    if(!rst_n)
        key_samp2_locked <= {KEY_WIDTH{1'b1}};
    else
        key_samp2_locked <= key_samp2;
end
 
wire [KEY_WIDTH:1] key_changed2 =key_samp2_locked &(~key_samp2);
 
always @ (posedge clk,negedge rst_n)
begin
    if(!rst_n)
        key_out <= {KEY_WIDTH{1'b1}};
    else
        key_out <= ~key_changed2;
end
 
endmodule

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
module key_debounce
(
    input clk,
    input rst_n,
    input data_in,
    output keyctr
);
 
reg [19:0] cnt;
 
always @ (posedge clk,negedge rst_n)
  if(!rst_n)
    cnt<=20'd0;
  else
    cnt<=cnt+1'b1;
     
reg low_sw;
 
always @ (posedge clk,negedge rst_n)
    if(!rst_n)
        low_sw<=1'b1;
    else if(cnt==20'hfffff)
        low_sw<=data_in;
         
reg low_sw_r;
 
always @(posedge clk,negedge rst_n)
    if(!rst_n)
        low_sw_r<=1'b1;
    else
        low_sw_r<=low_sw;
         
wire keyctr_r = low_sw_r & (~low_sw);
 
assign keyctr = keyctr_r;
 
endmodule

 

原理图模块消抖电路,利用已经优化好了的LPM库,节省资源,建模速度快。

 

posted on   齐威王  阅读(888)  评论(0编辑  收藏  举报

编辑推荐:
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· SQL Server 内存占用高分析
· .NET Core GC计划阶段(plan_phase)底层原理浅谈
· .NET开发智能桌面机器人:用.NET IoT库编写驱动控制两个屏幕
· 用纯.NET开发并制作一个智能桌面机器人:从.NET IoT入门开始
阅读排行:
· 20250116 支付宝出现重大事故 有感
· 一个基于 Roslyn 和 AvalonEdit 的跨平台 C# 编辑器
· 推荐一款非常好用的在线 SSH 管理工具
· 聊一聊 操作系统蓝屏 c0000102 的故障分析
· .NET周刊【1月第1期 2025-01-05】
< 2010年12月 >
28 29 30 1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31 1
2 3 4 5 6 7 8

导航

统计

点击右上角即可分享
微信分享提示