使用Verilog语言编写的5*4按键驱动,clk为扫描时钟,key为按键与FPGA芯片的接口,out为按键模块向微处理器(NIOS)传送结果的接口,scansig为FPGA扫描信号的输出口。
module key1(clk,key,out,scansig);
input clk;
input [4:0] key;
output [4:0] out;
output [3:0] scansig;
reg [4:0] out;
reg [3:0] scansig;
reg [31:0] counter;
reg [4:0] _20clk;
reg [1:0] Q;
always @(posedge clk)
begin
counter <= counter + 1'b1;
if( counter == 1250000)
begin
counter <= 0;
_20clk <= ~_20clk;
end
end
always @( posedge _20clk )
begin
Q <= Q + 1;
//out <= out + 1'b1;
end
always @(Q)
begin
case(Q)
2'b00:begin
scansig = 4'b1110;
case( key )
5'b11110 : out = 5'd1;
5'b11101 : out = 5'd2;
5'b11011 : out = 5'd3;
5'b10111 : out = 5'd4;
5'b01111 : out = 5'd5;
default : out = 5'd0;
endcase
end
2'b01:begin
scansig = 4'b1101;
case( key )
5'b11110 : out = 5'd6;
5'b11101 : out = 5'd7;
5'b11011 : out = 5'd8;
5'b10111 : out = 5'd9;
5'b01111 : out = 5'd10;
default : out = 5'd0;
endcase
end
2'b10:begin
scansig = 4'b1011;
case( key )
5'b11110 : out = 5'd11;
5'b11101 : out = 5'd12;
5'b11011 : out = 5'd13;
5'b10111 : out = 5'd14;
5'b01111 : out = 5'd15;
default : out = 5'd0;
endcase
end
2'b11:begin
scansig = 4'b0111;
case( key )
5'b11110 : out = 5'd16;
5'b11101 : out = 5'd17;
5'b11011 : out = 5'd18;
5'b10111 : out = 5'd19;
5'b01111 : out = 5'd20;
default : out = 5'd0;
endcase
end
endcase
end
endmodule
文章评论(0条评论)
登录后参与讨论