Introduction
Previous [Notes]. An independent Keyboard shake-off using the Tilde language. [Tilde] is written for four buttons. Today, I changed it to parameterization. It has been verified and is very easy to use.
Code Key_debounce.v
Module key_debounce # (parameter key_width = 4) (input I _clk, input I _rst_n, input [key_width: 1] I _key, // press 0 to loosen to 1 Output Reg [key_width: 1] o_key_val // key value ); // ++ reg [key_width: 1] key_samp1, key_samp1_locked; // collect I _key to key_samp1always @ (posedge I _clk, negedge I _rst_n) if (! I _rst_n) key_samp1 <= {key_width {1 'b1 }}; else key_samp1 <= I _key; // lock key_samp1 to notify @ (posedge I _clk, negedge I _rst_n) if (! I _rst_n) key_samp1_locked <= {key_width {1 'b1 }}; else key_samp1_locked <= key_samp1; // ------------------------------------ // ++ ++ wire [key_width: 1] key_changed1; // when some bits of key_samp1 are changed from 1 to 0 // some bits of key_changed1 are changed from 0 to 1 and only maintain one clock cycle assign key_changed1 = key_samp1_locked &(~ Key_samp1 ); // ------------------------------------ // ++ ++ Reg [19: 0] CNT; // once a button is pressed, CNT is immediately cleared always @ (posedge I _clk, negedge I _rst_n) if (! I _rst_n) CNT <= 20 'h0; else if (key_changed1) CNT <= 20 'h0; else CNT <= CNT + 1 'b1; // ------------------------------------ // ++ ++ Reg [key_width: 1] key_samp2, key_samp2_locked; // The I _key is collected to key_samp2always @ (posedge I _clk, negedge I _rst_n) only when the key does not change (no jitter) and remains above 20 ms) if (! I _rst_n) key_samp2 <= {key_width {1 'b1 }}; else if (CNT = 20 'hf _ FFFF) // 0 xfffff/50 m = 20.9715 Ms key_samp2 <= I _key; // lock key_samp2 to key_samp2_lockedalways @ (posedge I _clk, negedge I _rst_n) if (! I _rst_n) key_samp2_locked <= {key_width {1 'b1 }}; else key_samp2_locked <= key_samp2; // ------------------------------------ // ++ ++ wire [key_width: 1] key_changed2; // when some bits of key_samp2 are changed from 1 to 0 // some bits of key_changed2 are changed from 0 to 1 and only maintain one clock cycle assign key_changed2 = key_samp2_locked &(~ Key_samp2 ); // ------------------------------------ // ++ ++ // after each key is stable, output key value // press 0 and release to 1 always @ (posedge I _clk, negedge I _rst_n) if (! I _rst_n) o_key_val <= {key_width {1 'b1 }}; else o_key_val <= ~ Key_changed2; // ------------------------------------------ endmodule
Analysis
The key is how to correctly use global parameters. Line 3 declares some parameters between the module name and the moudule I/O. Like the moudule I/O declaration, multiple parameters are declared and separated by commas. Note that there is no semicolon at the end of the parentheses. The first line is similar to the following.Key_samp1 <= 4 'hfHow can I write it now? Copy key_width to 1 'b1:Key_samp1_locked <= {key_width {1 'b1}}.
Reference
1 Quartus II->EDit-> insert teMPlate ..