由于在复杂数字中设计中,会用到按键进行设置。这里总共使用四个按键,分别对应左移,右移,加(+),减(-)。这四个按键在使用之前都要进行去抖,每个按键去抖后输出毫秒脉冲。还是采用自定向下的设计方法,并采用模块例化的方法使设计代码能够重用。按键去抖的细节参见“按键自适应去抖部分”。代码如下:
module sw_debounce ( input clk, input rst, input ms_p, input sw_in_l, input sw_in_r, input sw_in_p, input sw_in_m, output sw_out_l, output sw_out_r, output sw_out_p, output sw_out_m ); debouncing debouncing_sw_l ( .rst ( rst ), .clk ( clk ), .ms_p ( ms_p ), .sw_in ( sw_in_l ), .sw_out ( sw_out_l ) ); debouncing debouncing_sw_r ( .rst ( rst ), .clk ( clk ), .ms_p ( ms_p ), .sw_in ( sw_in_r ), .sw_out ( sw_out_r ) ); debouncing debouncing_sw_p ( .rst ( rst ), .clk ( clk ), .ms_p ( ms_p ), .sw_in ( sw_in_p ), .sw_out ( sw_out_p ) ); debouncing debouncing_sw_m ( .rst ( rst ), .clk ( clk ), .ms_p ( ms_p ), .sw_in ( sw_in_m ), .sw_out ( sw_out_m ) ); endmodule
单个按键的去抖程序如下:细节参见“按键自适应去抖”
module debouncing
(
input rst,
input clk,
input ms_p,
input sw_in,
output sw_out
);
reg [ 2: 0 ] bn_r;
reg bn_hp_debounce;
reg [ 3: 0 ] deb_count;
reg [ 1: 0 ] deb_st;
assign sw_out = bn_hp_debounce;
always@( posedge clk or posedge rst )
if ( rst )
begin
bn_r <= 0;
deb_st <= 0;
deb_count <= 0;
bn_hp_debounce <= 0;
end
else
begin
bn_r <= { bn_r[ 1: 0 ], sw_in };
case ( deb_st )
0:
begin
deb_count <= 0;
bn_hp_debounce <= 0;
if ( bn_r[ 2 ] )
deb_st <= 1;
end
1:
begin
if ( ~bn_r[ 2 ] )
deb_st <= 0;
else if ( deb_count >= 10 )
begin
bn_hp_debounce <= 1'b1;
deb_st <= 2;
end
else if ( ms_p )
deb_count <= deb_count + 1;
end
2:
begin
deb_count <= 0;
bn_hp_debounce <= 1'b0;
if ( ~bn_r[ 2 ] )
deb_st <= 3;
end
3:
begin
if ( bn_r[ 2 ] )
deb_st <= 2;
else if ( deb_count >= 10 )
begin
deb_st <= 0;
// bn_hp_debounce <= 1'b1;
end
else if ( ms_p )
deb_count <= deb_count + 1;
end
default:
deb_st <= 0;
endcase
end
endmodule
在本系统程序设计时采用信号或输入、输出端口的命名方法如下:
(1)对变量的定义一般采用多个能体现变量含义的单词或缩写,中间采用下划线”_”级联,如:sw_in_l(按键-输入-左),sw_in_r(按键-输入-右)。
(2)对接口设计的输入端一般都采用“in”指示信号的方向为输入。如:sw_in_r。
(3)对接口的输出端一般都采用“out”指示信号的方向为输出。 如:sw_out_r。
(4)对于模块之间的级联的变量,设为中性,即不带“in”或“out”标志。输入、输出方向可以从例化模块的变量名识别。
(5)对于低电平的单变量可以加前缀n, 或后缀_n。如在低电平有效的复位信号前可以用nrst,或rst_n表示。