歡迎訂閱《FPGA學習入門100例教程》、《MATLAB學習入門100例教程》
目錄
一、理論基礎
二、核心程序
三、測試結(jié)果
一、理論基礎
? ? ? ?基于FPGA的4x4矩陣鍵盤控制器是一種使用FPGA(現(xiàn)場可編程門陣列)來實現(xiàn)對4x4矩陣鍵盤進行控制的設備。該控制器能夠有效地降低硬件資源的使用,提高系統(tǒng)的可靠性和穩(wěn)定性,是現(xiàn)代電子產(chǎn)品中常用的一種輸入設備。
? ? ? ?4x4矩陣鍵盤控制器采用矩陣鍵盤作為輸入設備,通過FPGA對鍵盤的掃描和識別,實現(xiàn)對鍵盤輸入信號的采集和處理。其基本原理是將4行4列的16個按鍵排列成一個二維矩陣,其中行線和列線分別連接到FPGA的輸入輸出端口。在FPGA內(nèi)部,通過編寫程序?qū)崿F(xiàn)鍵盤掃描和識別,將采集到的按鍵信號進行處理并輸出到相應的設備中。
? ? ? ?在4x4矩陣鍵盤控制器的實現(xiàn)過程中,需要使用到一些基本的電子電路和數(shù)字電路知識。其中,最核心的公式是行列消元法,即通過逐行掃描、逐列消元的方式,將按鍵信號解碼并識別出來。具體實現(xiàn)過程中,可以采用如下公式:
對于第i行第j列的按鍵:
- 當?shù)趇行與第j列為低電平時,該按鍵未被按下;
- 當?shù)趇行與第j列為高電平時,該按鍵被按下;
- 對于未被按下的按鍵,其行列電平均為低電平;
- 對于被按下的按鍵,其所在的行電平為高電平,而其他行的電平為低電平;
- 對于被按下的按鍵,其所在的列電平為高電平,而其他列的電平為低電平。
通過上述公式的應用,可以在FPGA內(nèi)部編寫程序?qū)崿F(xiàn)鍵盤掃描和識別。
基于FPGA的4x4矩陣鍵盤控制器的實現(xiàn)過程主要包括以下幾個步驟:
- 設計矩陣鍵盤:根據(jù)實際需求,設計4x4的矩陣鍵盤,將16個按鍵按照規(guī)定的行列排列方式連接起來。
- 硬件電路設計:根據(jù)矩陣鍵盤的設計方案,設計相應的硬件電路,包括電源電路、鍵盤接口電路等。
- FPGA程序設計:在FPGA開發(fā)平臺上編寫程序,實現(xiàn)鍵盤掃描和識別。具體實現(xiàn)過程中,可以采用Verilog或VHDL等硬件描述語言。
- 程序調(diào)試與測試:將編寫好的程序下載到FPGA中,對程序進行調(diào)試和測試,確保程序能夠正確地識別按鍵信號并輸出正確的結(jié)果。
- 系統(tǒng)集成:將FPGA與矩陣鍵盤、相關(guān)設備等集成在一起,形成完整的基于FPGA的4x4矩陣鍵盤控制器。
基于FPGA的4x4矩陣鍵盤控制器具有以下優(yōu)點:
- 高可靠性:由于采用FPGA作為核心控制器,具有較強的抗干擾能力和穩(wěn)定性,能夠保證長時間穩(wěn)定運行。
- 高效率:通過矩陣排列的方式,可以有效地降低硬件資源的使用,提高系統(tǒng)的效率。
- 可擴展性:可以根據(jù)實際需求進行擴展和升級,例如增加按鍵數(shù)量、擴展其他外設等。
具體實現(xiàn)過程如下:
? ? ? ? 這里第一部分的主要目標是設計一個鍵盤掃描程序,并讀取4*4鍵盤上的鍵盤,并以0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F(xiàn)在7段數(shù)碼管上顯示出來。這里我們將用到開發(fā)板的鍵盤和數(shù)碼管。本模塊分為鍵盤掃描和數(shù)碼管顯示兩個部分。這兩個部分非常的簡單,這里我們合在一起設計。鍵盤掃描的其基本原理如下所示:
? ? ? 通過不斷的掃描來完成鍵盤值的確認。注意這里四行和四列的交界初就是您實際中的16個按鍵,當您按下其中某個按鍵的時候,列信號y1,y2,y3,y4就會在對應的位置顯示出來,比如你按下了第一列第二個按鈕,那么就會在第二列的位置產(chǎn)生一個信號,又由于行信號x是一直在快速的掃描的,如果當?shù)诙姓脪呙柽^的時候,同時檢測到列信號,這說明這個按鈕按下了。這就是鍵盤掃描的基本原理。
? ? ? ?數(shù)碼管,這里,是7段數(shù)碼管,其基本結(jié)構(gòu)如下所示:
其一共有7個LED來表示一個數(shù)碼管。
二、核心程序
/*
1. Wait buttons and identify key position: according to certain frequency with low level circulation scan lines Y3, Y0 -
Monitor the status, once listed line judge a listed as low means have key is pressed,
Stop scans and maintain the current state of lines, then read column line condition to get the current buttons are key code.
2. Wait buttons pop-up: detection to each column line all become the top flat after restarting the scanning process, waiting for the next buttons
?
*/
module key_scanner(
clk,
rst,
row,
column,
scan_key
);
input clk;
input rst;
input [3:0] column;//Column line
output[3:0] row;//lines
output[3:0] scan_key;
reg[3:0] row;
reg[3:0] scan_key; // scan code registers
reg[31:0] cnt_scan;// scan frequency counter
reg sign; //sign=1:stop scanning
// sign=0:restart scanning*/
reg restart; //
always@(posedge clk or negedge rst)
begin
if(!rst)
begin
row<=4'b1110;
cnt_scan<=0;
end
else begin
if(sign==1'b0)
begin
restart<=0;
cnt_scan<=cnt_scan+1;
//if(cnt_scan==32'h0000ffff)
// begin
row[3:1]<=row[2:0];
row[0]<=row[3]; //4 root lines circulation send out low level
// cnt_scan<=0;
//end
end
else if(sign==1'b1)
begin
row<=row;
if(column==4'b1111)
restart<=1;
//Detect each column is high level
end
end
end
always@(posedge clk or negedge rst)
begin
if(!rst)
begin
scan_key<=0;
end
else begin
if(restart)
begin
sign<=0;
scan_key<=0;
end
else begin
case(row) //This case results testing where key press
4'b1110:
case(column)
4'b1110: begin
sign<=1;
scan_key<=0;
end
4'b1101: begin
sign<=1;
scan_key<=1;
end
4'b1011: begin
sign<=1;
scan_key<=2;
end
4'b0111: begin
sign<=1;
scan_key<=3;
end
endcase
4'b1101:
case(column)
4'b1110: begin
sign<=1;
scan_key<=4;
end
4'b1101: begin
scan_key<=5;
sign<=1;
end
4'b1011: begin
scan_key<=6;
sign<=1;
end
4'b0111: begin
scan_key<=7;
sign<=1;
end
endcase
4'b1011:
case(column)
4'b1110: begin
scan_key<=8;
sign<=1;
end
4'b1101: begin
scan_key<=9;
sign<=1;
end
4'b1011: begin
scan_key<=10;
sign<=1;
end
4'b0111: begin
scan_key<=11;
sign<=1;
end
endcase
4'b0111:
case(column)
4'b1110: begin
scan_key<=12;
sign<=1;
end
4'b1101: begin
scan_key<=13;
sign<=1;
end
4'b1011: begin
scan_key<=14;
sign<=1;
end
4'b0111: begin
scan_key<=15;
sign<=1;
end
endcase
default:
scan_key<=15;
endcase
end
end
end
endmodule
三、測試結(jié)果
其仿真結(jié)果如下所示:
注意col進行掃描的時候,data分別輸出0,1,2,3等鍵盤值。
Clk |
系統(tǒng)時鐘 |
Rst |
系統(tǒng)復位 |
Col |
由鍵盤輸入產(chǎn)生的列掃描信號 |
Data |
鍵盤值 |
LED_display |
7LED數(shù)值顯示 |
Rows |
行掃描信號 |
LED_select |
數(shù)碼管的選擇文章來源:http://www.zghlxwxcb.cn/news/detail-505131.html |
A02-37文章來源地址http://www.zghlxwxcb.cn/news/detail-505131.html
到了這里,關(guān)于基于FPGA的4x4矩陣鍵盤控制器verilog開發(fā)實現(xiàn)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!