您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 电子设计/PCB > 4×4矩阵键盘控制实验
4×4矩阵键盘控制实验一、实验内容摘要设计一个4×4键盘接口控制器,在QuartusII软件上实现基设计,将其与开发板连接,实现电路功能。当按下某一键时,4位LED上显示对应的键值,以二进制代码形式从0至F显示。二、实验源代码LIBRARYieee;USEieee.std_logic_1164.ALL;USEieee.std_logic_unsigned.ALL;ENTITYDEBOUNCINGISPORT(clk,key:INSTD_LOGIC;clr:INSTD_LOGIC;dly_out,dif_out:OUTSTD_LOGIC);ENDDEBOUNCING;ARCHITECTUREaOFDEBOUNCINGISSIGNALsample,dly,diff:STD_LOGIC;BEGINfree_counter:blocksignalQQ:std_logic_vector(4downto0);signald0:std_logic;beginprocess(CLR,clk)beginifclr='0'thend0='0';QQ=(OTHERS='0');ELSifclk'eventandclk='1'thend0=QQ(4);--QQ的最高位同时作为d0信号,即d0的周期为2的5次方个clk.QQ=QQ+1;endif;endprocess;sample=not(QQ(4)and(notd0));--当d0为0,QQ(4)为1时,sample产生采样脉冲,低电平时间为1个clkendblockfree_counter;debunce:blocksignald0,d1,s,r:std_logic;beginprocess(clk,clr)beginifclr='0'thendly='0';elsifrising_edge(clk)thenifsample='1'thend1=d0;d0=key;s=d0andd1;r=notd0andnotd1;ifs='0'andr='0'thendly=dly;elsifs='0'andr='1'thendly='0';elsifs='1'andr='0'thendly='1';elsedly='0';endif;endif;endif;endprocess;dly_out=dly;endblockdebunce;differential:blocksignald1,d0:std_logic;beginprocess(clk,clr)beginifclr='0'thend0='0';d1='0';elsifrising_edge(clk)thend1=d0;d0=dly;endif;diff=d0andnotd1;endprocess;dif_out=diff;endblockdifferential;ENDa;--******************************************************************--*4x4标准键盘板读取并点亮实验箱底板上的L1-L4--*Filename:keyboard4_4--*扫描键盘,译码并点亮实验箱底板上的L1-L4--*已加入去抖程序--******************************************************************libraryieee;useieee.std_logic_1164.all;useieee.std_logic_unsigned.all;entitykeyboard4_4isport(rst:instd_logic;clk_in:instd_logic;keyin:instd_logic_vector(3downto0);scan:outstd_logic_vector(3downto0);leds:outstd_logic_vector(3downto0);state:outstd_logic;M:outstd_logic_vector(3downto0));endkeyboard4_4;architecturekeyboard4_4_archofkeyboard4_4is----*********************************************componentdebouncingport(key:INSTD_LOGIC;clk,clr:INSTD_LOGIC;dly_out:OUTSTD_LOGIC);endcomponent;--*********************************************--signalclkfrq:std_logic;signalcntscn:std_logic_vector(1downto0);signalscnlin:std_logic_vector(3downto0);signalcntfrq:std_logic_vector(14downto0);signallednum:std_logic_vector(7downto0);signalkey_tmp:std_logic_vector(3downto0);signalclk:std_logic;signalcntfrq1:std_logic_vector(5downto0);beginM=0101;--键盘功能选择scan=notscnlin;lednum=scnlin&(notkey_tmp);--key_tmp=keyin;--debounuingcktdebounuing:blockbeginU1:debouncingPORTMAP(KEY=keyin(0),DLY_OUT=key_tmp(0),clr=rst,clk=CLK);U2:debouncingPORTMAP(KEY=keyin(1),dly_out=key_tmp(1),clr=rst,clk=CLK);U3:debouncingPORTMAP(key=keyin(2),dly_out=key_tmp(2),clr=rst,clk=CLK);U4:debouncingPORTMAP(key=keyin(3),dly_out=key_tmp(3),clr=rst,clk=CLK);ENDblockdebounuing;----******************************************************--process(rst,clk_in)--晶振为40MHz,进行40000分频产生去抖时钟(1000Hz)beginifrst='0'thencntfrq=(others='0');elsifrising_edge(clk_in)thenif(cntfrq=100111000011111ornot(key_tmp=1110orkey_tmp=1101orkey_tmp=1011orkey_tmp=0111))then--if(cntfrq=100111000011111orkey_tmp=1111)then--ifcntfrq=1111thencntfrq=(others='0');clk=notclk;--去抖时钟elsecntfrq=cntfrq+1;endif;endif;endprocess;process(rst,clk)--去抖时钟,50分频,形成扫描时钟beginifrst='0'thenclkfrq='0';cntfrq1=(others='0');elsifrising_edge(clk)thenifcntfrq1=11000thencntfrq1=(others='0');clkfrq=notclkfrq;elsecntfrq1=cntfrq1+1;endif;endif;endprocess;process(rst,clkfrq)--根据扫描时钟产生扫描线beginifrst='0'thencntscn=00;elsifrising_edge(clkfrq)thenifcntscn=11thencntscn=00;elsecntscn=cntscn+1;endif;casecntscniswhen00=scnlin=0001;when01=scnlin=0010;when10=scnlin=0100;when11=scnlin=1000;whenothers=null;endcase;endif;endprocess;process(rst,clkfrq)--根据按键点亮相应的ledsbeginif(rst='0')thenleds=0000;elsifclkfrq'eventandclkfrq='0'thencaselednumiswhen10001000=leds=0001;--1when01001000=leds=0010;--2when00101000=leds=0011;--3when00011000=leds=1010;--Awhen10000100=leds=0100;--4when01000100=leds=0101;--5when00100100=leds=0110;--6when00010100=leds=1011;--Bwhen10000010=leds=0111;--7when01000010=leds=1000;--8when00100010=leds=1001;--9when00010010=leds=1100;--Cwhen10000001=leds=1110;--*when01000001=leds=0000;--0when00100001=leds=1111;--#when00010001=leds=1101;--Dwhenothers=null;endcase;endif;endprocess;process(rst,key_tmp)beginif(rst='0')thenstate='1';elsif(key_tmp=1110orkey_tmp=1101orkey_tmp=1011orkey_tmp=0111)thenstate='0';elsif(key_tmp=1111)thenstate='1';endif;endprocess;endkeyboard4_4_arch;三、实验工具软件的选用以及实验过程1、打开QuartusII软件。2、选择File/New,选择DesignFile/VHDLFile。3、输入4×4矩阵键盘控制的代码。4、选择File/Save,保存文件名为DEBOUNCING.vhd。在弹出的窗口Doyouwanttocreatanewprojectwiththisfile?选择“是”。5、在弹出的NewProjecWizard:中,选择Next;Directory,Name,Top-LevelEntity选择Next;AddFiles选择Next;○4Family&DeviceSetting中,Family选择CycloneIV,Packge选择FBGA,PinCount选择256,Speedgrade选择8,Availabledevice选择EP3C25F256C8,在点击Next;○5EDAToolSettings选择Next;○6最后在Summary中选择Finish。6、点击Assignments,选择RemoveAssignments,全选后取消All和DeviceAssigments。7、预编译,选择Process/Start/StartAnalysis&Synthesis,进行综合。8、选择File/New,选择DesignFile/VHDLFile。9、输入4
本文标题:4×4矩阵键盘控制实验
链接地址:https://www.777doc.com/doc-6875935 .html