【黑金教程笔记之004】【建模篇】【Lab 03 消抖模块之一】—笔记

设计思路:

(1)       一旦检测到按键资源按下(从高电平到低电平),“电平检测模块”就会拉高H2L_Sig电平,然后拉低。

(2)       “10ms延迟模块”检测到H2L_Sig高电平,就会利用10ms过滤H2L_Sig,拉高输出。

(3)       当按键被释放,“电平检测模块”会拉高L2H_Sig电平,然后拉低。

(4)       “10ms延迟模块”检测到L2H_Sig就会利用10ms过滤L2H_Sig然后拉低输出。

模块:

 1 /***********************************************************
 2 module name: detect_module.v
 3 function: detect pin's level change
 4 
 5 by yf.x
 6 2014-11-05
 7 
 8 ************************************************************/
 9 
10 module detect_module(
11 CLK,
12 RST_n,
13 Pin_in,
14 H2L_Sig,
15 L2H_Sig
16 );
17 
18 input CLK;
19 input RST_n;
20 input Pin_in;
21 output H2L_Sig;
22 output L2H_Sig;
23 
24 /**************************************************************/
25 //DE2-115 use 50MHz oscillator,50M*0.0001-1=4_999
26 parameter T100us=11'd4999;
27 
28 /**************************************************************/
29 
30 reg [10:0]count1;
31 reg isEn;
32 
33 always @(posedge CLK or negedge RST_n)    //100us timer
34 if(!RST_n)
35   begin
36     count1<=11'd0;
37      isEn<=1'b0;
38   end
39 else if(count1==T100us)
40   isEn<=1'b1;
41 else
42   count1<=count1+1'b1;
43   
44  /***********************************************************/
45 
46 reg H2L_F1;
47 reg H2L_F2;
48 reg L2H_F1;
49 reg L2H_F2;
50 
51 always @(posedge CLK or negedge RST_n)
52 if(!RST_n)
53   begin
54     H2L_F1<=1'b1;
55      H2L_F2<=1'b1;
56     L2H_F1<=1'b0;
57     L2H_F2<=1'b0;
58   end
59 else
60   begin
61     H2L_F1<=Pin_in;
62     H2L_F2<=H2L_F1;
63     L2H_F1<=Pin_in;
64     L2H_F2<=L2H_F1;
65   end
66 
67 /*****************************************************/
68 
69 assign H2L_Sig=isEn?(!H2L_F1&H2L_F2):1'b0;
70 assign L2H_Sig=isEn?(!L2H_F2&L2H_F1):1'b0;
71 
72 /*****************************************************/
73 
74 endmodule
75 
76   
77   
  1 /***************************************************
  2 module name: delay_module.v
  3 function: delay 10ms.
  4 
  5 by yf.x
  6 2014-11-05
  7 
  8 ***************************************************/
  9 
 10 module delay_module(
 11 CLK,
 12 RST_n,
 13 H2L_Sig,
 14 L2H_Sig,
 15 Pin_out
 16 );
 17 
 18 input CLK;
 19 input RST_n;
 20 input H2L_Sig;
 21 input L2H_Sig;
 22 output Pin_out;
 23 
 24 /**************************************************/
 25 //5M*0.001-1=49_999
 26 parameter T1ms=16'd49_999;
 27 
 28 /**************************************************/
 29 
 30 reg [15:0]count1;
 31 
 32 always @(posedge CLK or negedge RST_n)
 33 if(!RST_n)
 34   count1<=16'd0;
 35 else if(iscount && count1==T1ms)
 36   count1<=16'd0;
 37 else if(iscount)
 38   count1<=count1+1'b1;
 39 else if(!iscount)
 40   count1<=16'd0;
 41   
 42 /****************************************************/
 43 
 44 reg [3:0]count_ms;
 45 
 46 always @(posedge CLK or negedge RST_n)
 47 if(!RST_n)
 48   count_ms<=4'd0;
 49 else if(iscount && count1==T1ms)
 50   count_ms<=count_ms+1'b1;
 51 else if(!iscount)
 52   count_ms<=4'd0;
 53 
 54 /*******************************************************/
 55 
 56 reg iscount;
 57 reg rPin_out;
 58 reg [1:0]i;
 59 
 60 always @(posedge CLK or negedge RST_n)
 61 if(!RST_n)
 62   begin
 63     iscount<=1'b0;
 64     rPin_out<=1'b0;
 65     i<=2'd0;
 66   end
 67 else
 68   case(i)
 69     2'd0:
 70     if(H2L_Sig)
 71       i<=2'd1;
 72     else if(L2H_Sig)
 73        i<=2'd2;
 74         
 75      2'd1:
 76      if(count_ms==4'd10)
 77      begin 
 78        iscount<=1'b0;
 79        rPin_out<=1'b1;
 80        i<=2'd0;
 81      end
 82      else
 83        iscount<=1'b1;
 84     
 85     2'd2:
 86      if(count_ms==4'd10)
 87      begin
 88        iscount<=1'b0;
 89        rPin_out<=1'b0;
 90        i<=2'd0;
 91      end
 92      else
 93        iscount<=1'b1;
 94     endcase
 95 
 96 /**************************************************/
 97 
 98 assign Pin_out=rPin_out;
 99 
100 /**************************************************/
101 
102 endmodule
103     
104        
105   
106     
 1 /****************************************
 2 module name: debounce_module.v
 3 function: debounce a key
 4 pin assignments(for DE2-115):
 5 ---------------------------------
 6 CLK----------------------CLOCK_50
 7 RST_n--------------------KEY[0]
 8 Pin_in-------------------KEY[3]
 9 Pin_out------------------LEDG[3]
10 
11 ****************************************/
12 
13 module debounce_module(
14 CLK,
15 RST_n,
16 Pin_in,
17 Pin_out
18 );
19 
20 input CLK;
21 input RST_n;
22 input Pin_in;
23 output Pin_out;
24 
25 /*******************************/
26 
27 wire H2L_Sig;
28 wire L2H_Sig;
29 
30 detect_module u0(
31 .CLK(CLK),
32 .RST_n(RST_n),
33 .Pin_in(Pin_in),    //input from top
34 .H2L_Sig(H2L_Sig),  //output to u1
35 .L2H_Sig(L2H_Sig)   //output to u1
36 );
37 
38 /***************************************/
39 
40 delay_module u1(
41 .CLK(CLK),
42 .RST_n(RST_n),
43 .H2L_Sig(H2L_Sig),  //input from u1
44 .L2H_Sig(L2H_Sig),   //input from u1
45 .Pin_out(Pin_out)   //output to top
46 );
47 
48 /***************************************/
49 
50 endmodule

疑问:

(1)       iscount的作用?

使能2个计数器(count1和count_ms)。

(2)       延迟10ms的目的?

过滤H2L_Sig和L2H_Sig。

detect_module里延迟100us,过滤最初的不稳定状态,至于为何用100us,猜的。

模块框图:

实验三结论:

功能模块对功能模块的组合建模。

原文地址:https://www.cnblogs.com/halflife/p/4078125.html