"); //-->
//控制PS2数字小键盘并在数码管显示相应的数字
module ps2scan(clk,ps2k_clk,ps2k_data,ps2_state,led_cs,led_db);
input clk,ps2k_clk,ps2k_data;
output ps2_state;//键盘状态,1时表示有按键按下
output led_cs;//数码管选择
output [7:0] led_db; //数码管显示数据
wire [7:0] led_db; //7段数码管寄存器
reg ps2k_clk_r0,ps2k_clk_r1,ps2k_clk_r2;//ps2_clk时钟信号的寄存器
assign led_cs=1'b1; //led_cs为1时,数码管1选通
//------------------------------------------
wire neg_ps2k_clk;//ps2k_clk下降沿标志位
initial begin
ps2k_clk_r0 = 1'b0; //初始化,因为实验版复位键坏,所以直接初始化赋值
ps2k_clk_r1 = 1'b0;
ps2k_clk_r2 = 1'b0;
end
always @ (posedge clk )
begin //锁存状态,进行滤波
ps2k_clk_r0 <= ps2k_clk;
ps2k_clk_r1 <= ps2k_clk_r0;
ps2k_clk_r2 <= ps2k_clk_r1;
end
assign neg_ps2k_clk = ~ps2k_clk_r1 & ps2k_clk_r2; //下降沿
//------------------------------------------
reg[7:0] ps2_byte_r; //PC接收来自PS2的一个字节数据存储器
reg[7:0] temp_data; //当前接收数据寄存器
reg[3:0] num; //计数寄存器
initial begin temp_data=8'h00;num = 4'd0;end
always @ (posedge clk ) begin
if(neg_ps2k_clk) begin //检测到ps2k_clk的下降沿
case (num)
4'd0: num <= num+1'b1; //接受PS2 key的数据,一共八位
4'd1: begin
num <= num+1'b1;
temp_data[0] <= ps2k_data; //bit0
end
4'd2: begin
num <= num+1'b1;
temp_data[1] <= ps2k_data; //bit1
end
4'd3: begin
num <= num+1'b1;
temp_data[2] <= ps2k_data; //bit2
end
4'd4: begin
num <= num+1'b1;
temp_data[3] <= ps2k_data; //bit3
end
4'd5: begin
num <= num+1'b1;
temp_data[4] <= ps2k_data; //bit4
end
4'd6: begin
num <= num+1'b1;
temp_data[5] <= ps2k_data; //bit5
end
4'd7: begin
num <= num+1'b1;
temp_data[6] <= ps2k_data; //bit6
end
4'd8: begin
num <= num+1'b1;
temp_data[7] <= ps2k_data; //bit7
end
4'd9: begin
num <= num+1'b1; //奇偶校验位,不做处理
end
4'd10: begin
num <= 4'd0; // num清零
end
default: ;
endcase
end
end
//------------------------------------------
reg key_f0; //松键标志位,置1表示接收到数据8'hf0,再接收到下一个数据后清零
reg ps2_state_r; //键盘当前状态,ps2_state_r=1表示有键被按下
initial begin key_f0 = 1'b0;ps2_state_r = 1'b1;end //初始化
always @ (posedge clk) //接收数据的相应处理,这里只对1byte的键值进行处理
begin
if(num==4'd10) begin //刚传送完一个字节数据
if(temp_data== 8'hf0) key_f0 <= 1'b1;
else
begin
if(!key_f0)
begin //说明有键按下
ps2_state_r <= 1'b0;
ps2_byte_r<= temp_data; //锁存当前键值
end
else
begin
ps2_state_r <= 1'b1;
key_f0 <= 1'b0;
end
end
end
end
//------------------------------------------
parameter seg0 = 8'hc0,//数码管显示值
seg1 = 8'hf9,
seg2 = 8'ha4,
seg3 = 8'hb0,
seg4 = 8'h99,
seg5 = 8'h92,
seg6 = 8'h82,
seg7 = 8'hf8,
seg8 = 8'h80,
seg9 = 8'h90;
reg [7:0] led_dbr;//led数据寄存器
always @(ps2_byte_r) //根据不同的键码在数码管输出相应的数字,0~9
begin
case (ps2_byte_r)
8'h70: led_dbr<=seg0;
8'h69: led_dbr<=seg1;
8'h72: led_dbr<=seg2;
8'h7A: led_dbr<=seg3;
8'h6B: led_dbr<=seg4;
8'h73: led_dbr<=seg5;
8'h74: led_dbr<=seg6;
8'h6C: led_dbr<=seg7;
8'h75: led_dbr<=seg8;
8'h7D: led_dbr<=seg9;
default:led_dbr<=0'hff;
endcase
end
/*
//数字小键盘的键码
8'h70 //0
8'h69 //1
8'h72 //2
8'h7A //3
8'h6B //4
8'h73 //5
8'h74 //6
8'h6C //7
8'h75 //8
8'h7D //9
8'h71 //小数点
8'h5A //回车
8'h79 //加
8'h7B //减
8'h7C //乘
8'h4A //除
8'h77 //numlock键
8'h66 //删除键
*/
assign led_db=led_dbr;
endmodule
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。