|
先从键盘矩阵模块ps2.v做起吧!
新建工程文件夹ps2。
新建ps2.v文件。
module ps2 (clk, rst_n, ps2_sclk, ps2_sda, key_value, flag);
input clk, rst_n;
input ps2_sclk;
input ps2_sda;
output reg [3:0] key_value;
output reg flag;
reg [7:0] ps2_data;
reg valid;
wire neg;
reg ps2_sclk_r;
always @ (posedge clk or negedge rst_n)
begin
if (!rst_n)
begin
ps2_sclk_r <= 0;
end
else
begin
ps2_sclk_r <= ps2_sclk;
end
end
assign neg = ps2_sclk_r && (~ps2_sclk);
reg [3:0] num;
reg [15:0] watchdog; // 1kHz
//////// transfer serial to parallel
always @ (posedge clk or negedge rst_n)
begin
if (!rst_n)
begin
num <= 0;
valid <= 0;
ps2_data <= 0;
watchdog <= 0;
end
else
begin
if (!neg)
begin
if (watchdog < 999)
begin
watchdog <= watchdog + 16'd1;
valid <= 0;
end
else
begin
watchdog <= 0;
num <= 0;
end
end
else
begin
watchdog <= 0;
case (num)
0 : num <= num + 4'd1;
1 : begin
num <= num + 4'd1;
ps2_data[0] <= ps2_sda;
end
2 : begin
num <= num + 4'd1;
ps2_data[1] <= ps2_sda;
end
3 : begin
num <= num + 4'd1;
ps2_data[2] <= ps2_sda;
end
4 : begin
num <= num + 4'd1;
ps2_data[3] <= ps2_sda;
end
5 : begin
num <= num + 4'd1;
ps2_data[4] <= ps2_sda;
end
6 : begin
num <= num + 4'd1;
ps2_data[5] <= ps2_sda;
end
7 : begin
num <= num + 4'd1;
ps2_data[6] <= ps2_sda;
end
8 : begin
num <= num + 4'd1;
ps2_data[7] <= ps2_sda;
end
9 : begin
num <= num + 4'd1;
valid <= 1;
end
10 : begin
num <= 0;
end
default : num <= 0;
endcase
end
end
end
//////////// key value decoder
reg [9:0] key_store;
reg state;
always @ (posedge clk or negedge rst_n)
begin
if (!rst_n)
begin
key_store <= 'b0;
flag <= 0;
key_value <= 4'h0;
state <= 0;
end
else
begin
if (!valid)
flag <= 0;
else
begin
case (state)
0 : begin // key pressed
case (ps2_data)
8'h70 : key_store[0] <= 1'b1; // KP0={70,F0_70}
8'h69 : key_store[1] <= 1'b1; // KP1={69,F0_69}
8'h72 : key_store[2] <= 1'b1; // KP2={72,F0_72}
8'h7A : key_store[3] <= 1'b1; // KP3={7A,F0_7A}
8'h6B : key_store[4] <= 1'b1; // KP4={6B,F0_6B}
8'h73 : key_store[5] <= 1'b1; // KP5={73,F0_73}
8'h74 : key_store[6] <= 1'b1; // KP6={74,F0_74}
8'h6C : key_store[7] <= 1'b1; // KP7={6C,F0_6C}
8'h75 : key_store[8] <= 1'b1; // KP8={75,F0_75}
8'h7D : key_store[9] <= 1'b1; // KP9={7D,F0_7D}
8'hF0 : state <= 1;
default : key_store <= key_store;
endcase
end
1 : begin // key released
case (ps2_data)
8'h70 : begin // KP0={70,F0_70}
key_store[0] <= 1'b0;
flag <= 1;
key_value <= 4'h0;
state <= 0;
end
8'h69 : begin // KP1={69,F0_69}
key_store[1] <= 1'b0;
flag <= 1;
key_value <= 4'h1;
state <= 0;
end
8'h72 : begin // KP2={72,F0_72}
key_store[2] <= 1'b0;
flag <= 1;
key_value <= 4'h2;
state <= 0;
end
8'h7A : begin // KP3={7A,F0_7A}
key_store[3] <= 1'b0;
flag <= 1;
key_value <= 4'h3;
state <= 0;
end
8'h6B : begin // KP4={6B,F0_6B}
key_store[4] <= 1'b0;
flag <= 1;
key_value <= 4'h4;
state <= 0;
end
8'h73 : begin // KP5={73,F0_73}
key_store[5] <= 1'b0;
flag <= 1;
key_value <= 4'h5;
state <= 0;
end
8'h74 : begin // KP6={74,F0_74}
key_store[6] <= 1'b0;
flag <= 1;
key_value <= 4'h6;
state <= 0;
end
8'h6C : begin // KP7={6C,F0_6C}
key_store[7] <= 1'b0;
flag <= 1;
key_value <= 4'h7;
state <= 0;
end
8'h75 : begin // KP8={75,F0_75}
key_store[8] <= 1'b0;
flag <= 1;
key_value <= 4'h8;
state <= 0;
end
8'h7D : begin // KP9={7D,F0_7D}
key_store[9] <= 1'b0;
flag <= 1;
key_value <= 4'h9;
state <= 0;
end
default : state <= 0;
endcase
state <= 0;
end
endcase
end
end
end
endmodule
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?我要注册
x
|