資源描述:
《矩陣鍵盤(未消抖).doc》由會(huì)員上傳分享,免費(fèi)在線閱讀,更多相關(guān)內(nèi)容在教育資源-天天文庫(kù)。
1、4*4矩陣鍵盤(未消抖):module?Matrix_keyboard(clk,in_s,out_s,num);//定義模塊端口信息input??????????clk;input???[3:0]??in_s;output??[3:0]??out_s;output??[4:0]??num;//定義輸出信號(hào)類型及局部變量reg?????[4:0]??num;reg?????[1:0]??cnt?=?0;reg?????[1:0]??tmp?=?0;reg?????[3:0]??o_ut_st?=?0;wire????[7:0]??ds
2、ample;//將掃描輸出和輸入信號(hào)級(jí)聯(lián),得到矩陣掃描結(jié)果assign??dsample?=?{o_ut_st,?in_s};assign??out_s???=?o_ut_st;//產(chǎn)生按鈕矩陣的列掃描信號(hào)always??@(posedge?clk)???begin???????cnt?<=?cnt?+?1'b1;???????case?(cnt)??????????2'b00:?????o_ut_st?<=?4'b1000;??????????2'b01:?????o_ut_st?<=?4'b0100;??????????2'b1
3、0:?????o_ut_st?<=?4'b0010;??????????2'b11:?????o_ut_st?<=?4'b0001;????????????????endcase???end//根據(jù)按鈕的列掃描信號(hào)和行輸入信號(hào)判斷按鈕是否被按下always??@(posedge?clk)???begin??????//如果無(wú)按鈕按下,定義num=16為無(wú)效狀態(tài)??????if?(in_s?==?4'b0000)????????begin???????????if?(tmp?==?3)?????????????begin???????
4、?????????num?<=?16;??//無(wú)按鍵輸入,輸出16????????????????tmp?<=?0;?????????????end???????????else?????????????begin????????????????num?<=?num;????????????????tmp?<=?tmp?+?1'b1;??//掃描周期,3個(gè)時(shí)鐘周期?????????????end????????end??????else????????begin???????????tmp?<=?0;???????????case?
5、(dsample)?????????????//第1列掃描結(jié)果?????????????8'b1000_0001:?num?<=?0;?????????????8'b1000_0010:?num?<=?1;?????????????8'b1000_0100:?num?<=?2;?????????????8'b1000_1000:?num?<=?3;??????????????//第2列掃描結(jié)果?????????????8'b0100_0001:?num?<=?4;?????????????8'b0100_0010:?num?<=?5
6、;?????????????8'b0100_0100:?num?<=?6;?????????????8'b0100_1000:?num?<=?7;???????????????//第3列掃描結(jié)果?????????????8'b0010_0001:?num?<=?8;?????????????8'b0010_0010:?num?<=?9;?????????????8'b0010_0100:?num?<=?10;?????????????8'b0010_1000:?num?<=?11;???????????????//第4列掃描結(jié)果??
7、???????????8'b0001_0001:?num?<=?12;?????????????8'b0001_0010:?num?<=?13;?????????????8'b0001_0100:?num?<=?14;?????????????8'b0001_1000:?num?<=?15;?????????????endcase????????end???endendmodule???獨(dú)立按鍵消抖程序:module?Btn_without_shake(Clk_50MHz,PB_UP,PB_Out,count_sel);//定義模塊
8、端口信息input?????Clk_50MHz;??//模塊時(shí)鐘50MHzinput?????PB_UP;??//按鈕輸入output????PB_Out;??//去抖后按鈕輸出output????[1:0]??count_sel;?