附录1:键盘扫描程序
module Keypad(row,col,code,clock);
input clock;
input [0:2] row;
output reg [2:0] col;
output reg [3:0] code;
reg [8:0] current_state,next_state;
parameter S_1=9'b000000001,S_2=9'b000000010,S_3=9'b000000100;
parameter S_4=9'b000001000,S_5=9'b000010000,S_6=9'b000100000;
parameter S_7=9'b001000000,S_8=9'b010000000,S_9=9'b100000000;
always@(row,col)
begin
code=code;
if(row!=3'b111)
begin
case({row,col})
6'b011_000:code=0;
6'b011_001:code=1;
6'b011_010:code=2;
6'b011_011:code=3;
6'b011_110:code=4;
6'b011_111:code=5;
6'b101_000:code=6;
6'b101_001:code=7;
6'b101_100:code=8;
6'b101_101:code=9;
endcase
end
end
//以下为键盘扫描状态机
always@(posedge clock)
current_state=next_state;
always@(*)
begin
case(current_state)
S_1:begin
col=3'b000;
if(row!=3'b111) next_state=S_9;else next_state=S_2;
end
S_2:begin
col=3'b001;
if(row!=3'b111) next_state=S_9;else next_state=S_3;
end
S_3:begin
col=3'b010;
if(row!=3'b111) next_state=S_9;else next_state=S_4;
end
S_4:begin
col=3'b011;
if(row!=3'b111) next_state=S_9;else next_state=S_5;
end
S_5:begin
col=3'b100;
if(row!=3'b111) next_state=S_9;else next_state=S_6;
end
S_6:begin
col=3'b101;
if(row!=3'b111) next_state=S_9;else next_state=S_7;
end
S_7:begin
col=3'b110;
if(row!=3'b111) next_state=S_9;else next_state=S_8;
end
S_8:begin
col=3'b111;
if(row!=3'b111) next_state=S_9;else next_state=S_1;
end
S_9:if (row==3'b111) next_state=S_1;
endcase
end
endmodule