⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 keyscan.v

📁 Mars-XC2S50-S-Core-V2.0开发板核心板的说明和设计文档
💻 V
字号:
/*
矩阵键盘实验1:向用户介绍矩阵键盘扫描实现的方法,没有考虑去抖和判断键弹起的问题;把相应的键值显示在数码管上
*/

module keyscan(clk,rst,row,column,dataout,en) ;

input clk,rst;
input[3:0] column;//列线
output[3:0] row;//行线
output[7:0] dataout;//数码管显示数据
reg[7:0] dataout;
output[7:0] en;//数码管显示使能
reg[3:0] row;

reg[3:0] scan_key; //扫描码寄存器
reg[15:0] cnt_scan;//扫描频率计数器

assign en=0;

always@(posedge clk)
begin
	if(!rst) begin
		row<=4'b1110;
		cnt_scan<=0;
	 end
	else begin
		cnt_scan<=cnt_scan+1;
		if(cnt_scan==16'hffff) begin
			row[3:1]<=row[2:0];
			row[0]<=row[3];  //4根行线循环送出低电平
		 end
	 end
end


always@(posedge clk)
begin
	if(!rst) begin
		scan_key<=0;
	 end
	else begin
		case(row)  //该case结果检测何处有键按下
			4'b1110:
				case(column)
					4'b1110: begin
						scan_key<=0;
					 end
					4'b1101: begin
						scan_key<=1;
					 end
					4'b1011: begin
						scan_key<=2;
					 end
					4'b0111: begin
						scan_key<=3;
					 end
				 endcase
			4'b1101:
				case(column)
					4'b1110: begin
						scan_key<=4;
					 end
					4'b1101: begin
						scan_key<=5;
					 end
					4'b1011: begin
						scan_key<=6;
					 end
					4'b0111: begin
						scan_key<=7;
					 end
				 endcase
			4'b1011:
				case(column)
					4'b1110: begin
						scan_key<=8;
					 end
					4'b1101: begin
						scan_key<=9;
					 end
					4'b1011: begin
						scan_key<=10;
					 end
					4'b0111: begin
						scan_key<=11;
					 end
				 endcase
			4'b0111:
				case(column)
					4'b1110: begin
						scan_key<=12;
					 end
					4'b1101: begin
						scan_key<=13;
					 end
					4'b1011: begin
						scan_key<=14;
					 end
					4'b0111: begin
						scan_key<=15;
					 end
				 endcase
			 default:
				scan_key<=15;
		 endcase
	 end
end



always@(scan_key)
begin
	case(scan_key)
		4'b0000:
			dataout=8'b0000_0011;
		4'b0001:
			dataout=8'b1001_1111;
		4'b0010:
			dataout=8'b0010_0101;
		4'b0011:
			dataout=8'b0000_1101;
		4'b0100:
			dataout=8'b1001_1001;
		4'b0101:
			dataout=8'b0100_1001;
		4'b0110:
			dataout=8'b0100_0001;
		4'b0111:
			dataout=8'b0001_1111;
		4'b1000:
			dataout=8'b0000_0001;
		4'b1001:
			dataout=8'b0001_1001;
		4'b1010:
			dataout=8'b0001_0001;
		4'b1011:
			dataout=8'b1100_0001;
		4'b1100:
			dataout=8'b0110_0011;
		4'b1101:
			dataout=8'b1000_0101;
		4'b1110:
			dataout=8'b0110_0001;
		4'b1111:
			dataout=8'b0111_0001;
	 endcase
end	

endmodule		

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -