您好,欢迎访问三七文档
当前位置:首页 > 电子/通信 > 电子设计/PCB > 可编程器件与硬件描述语言VHDL-PS2键盘
PS2键盘扫描总原理图:说明:总的原理图中共包含4个模块,分别是PS2键盘按键数据读取模块KEY_DATA、数据转换模块CONVERT、数码管显示模块LED、液晶显示模块LCD,以下就各个模块分别作进一步的说明:(1)PS2键盘按键数据读取模块KEY_DATA宏模块图:VHDL代码:libraryIEEE;useIEEE.STD_LOGIC_1164.ALL;useIEEE.STD_LOGIC_ARITH.ALL;useIEEE.STD_LOGIC_UNSIGNED.ALL;entityKEY_DATAisport(sys_clk:inSTD_LOGIC;k_data:inSTD_LOGIC;k_clock:inSTD_LOGIC;reset:inSTD_LOGIC;data:outSTD_LOGIC_VECTOR(7DOWNTO0);request:outSTD_LOGIC);endKEY_DATA;architectureBehavioralofKEY_DATAissignaltmp:STD_LOGIC_VECTOR(11downto0):=000000000000;--记录一帧的信号signalnow_kbclk,pre_kbclk:std_logic;signalscan:STD_LOGIC_VECTOR(10DOWNTO0);beginprocess(reset,k_clock,sys_clk)beginifreset='1'thentmp=000000000000;elsifsys_clk'eventandsys_clk='1'thenpre_kbclk=now_kbclk;now_kbclk=k_clock;if(pre_kbclknow_kbclk)then--检测到ps2键盘时钟的下降沿iftmp(0)='0'thentmp=k_data&01111111111;--开始存入数据,01111111111中的0用于之后判断一帧数据是否读取完毕elsetmp=k_data&tmp(11downto1);--存数据endif;endif;endif;endprocess;process(tmp)beginiftmp(0)='0'then--一帧数据读取完毕scan=tmp(11downto1);data=scan(8downto1);request='1';elsedata=00000000;request='0';endif;endprocess;endBehavioral;管脚说明:reset为复位信号,高电平有效,使输出置0;sys_clk为系统时钟信号(25MHz);k_clock为键盘时钟信号;k_data为键盘数据信号;data(7:0)为输出信号,表示读取的键盘扫描码;request也为输出信号,该管脚为”1”表示一次按键的扫描码已读取完毕,用以通知后续转换电路将扫描码转换为ASCII码。功能说明:依据PS2键盘协议的时序规定读取键盘发送的数据,提供给后续转换电路。(需要说明的是,第一次建立的键盘数据读取宏模块为DATA_IN,采用的是不同的代码,具体如下:宏模块图:VHDL代码:libraryieee;useieee.std_logic_1164.ALL;useieee.numeric_std.ALL;libraryUNISIM;useUNISIM.Vcomponents.ALL;entityDATA_INisport(PS2_CLK:instd_logic;PS2_DATA:instd_logic;RST:instd_logic;REQUEST:outstd_logic;SCAN_DATA:outstd_logic_vector(7downto0));endDATA_IN;architectureBEHAVIORALofDATA_INisSIGNALTEMP:STD_LOGIC_VECTOR(10DOWNTO0);--用于暂存来自键盘的串行数据,共有11位SIGNALDATA_1:STD_LOGIC_VECTOR(7DOWNTO0);SIGNALDATA_2:STD_LOGIC_VECTOR(7DOWNTO0);SIGNALDATA_3:STD_LOGIC_VECTOR(7DOWNTO0);--用于暂存读入的编码SIGNALCOUNT:INTEGERRANGE0TO10;--用于计数beginPROCESS(PS2_CLK,RST)BEGINIFRST='1'THENCOUNT=0;ELSEIFPS2_CLK'EVENTANDPS2_CLK='1'THEN--上升沿时计数值改变IFCOUNT=10THENCOUNT=0;ELSECOUNT=COUNT+1;ENDIF;ENDIF;ENDIF;ENDPROCESS;PROCESS(PS2_CLK,RST)BEGINIFRST='1'THENTEMP=(OTHERS='0');ELSEIFPS2_CLK'EVENTANDPS2_CLK='0'THEN--下降沿时读入数据TEMP(COUNT)=PS2_DATA;ENDIF;ENDIF;ENDPROCESS;PROCESS(PS2_CLK,RST)BEGINIFRST='1'THENDATA_1=(OTHERS='0');DATA_2=(OTHERS='0');DATA_3=(OTHERS='0');ELSEIFPS2_CLK'EVENTANDPS2_CLK='1'THENCASECOUNTISWHEN9=DATA_1=TEMP(8DOWNTO1);WHEN10=IFDATA_2=11110000ANDDATA_1=DATA_3THENSCAN_DATA=DATA_1;DATA_2=(OTHERS='0');DATA_3=(OTHERS='0');REQUEST='1';ELSESCAN_DATA=(OTHERS='0');DATA_3=DATA_2;DATA_2=DATA_1;REQUEST='0';ENDIF;WHENOTHERS=SCAN_DATA=(OTHERS='0');REQUEST='0';ENDCASE;ENDIF;ENDIF;ENDPROCESS;endBEHAVIORAL;下载后虽然数码管与液晶都能正常显示,但是按了几个键后显示都会卡住,此时再按键就不会再有反应了。而且按键的次数是不一定的,有时候按5、6个键就卡住了,有时候按20几个键才卡住。仔细检查后发现后续模块的代码都没有问题,确定是键盘数据读取模块出了问题,上网查询相关资料后得知是由于该模块中没有用到系统时钟,即没有做系统时钟与键盘时钟的同步,两者的不同步导致了数据无法正常读入,于是参照网上的方法重新建立了宏模块KEY_DATA,加入了系统时钟与键盘时钟的同步关联,从而解决了按键卡住的问题。)(2)数据转换模块CONVERT宏模块图:VHDL代码:libraryieee;useieee.std_logic_1164.ALL;useieee.numeric_std.ALL;libraryUNISIM;useUNISIM.Vcomponents.ALL;entityCONVERTisport(PREPARED:instd_logic;RST:instd_logic;SCAN_DATA:instd_logic_vector(7downto0);ASCII:outstd_logic_vector(7downto0));endCONVERT;architectureBEHAVIORALofCONVERTisSIGNALASCII_DATA:STD_LOGIC_VECTOR(7DOWNTO0);beginPROCESS(PREPARED,RST)BEGINIFRST='1'THENASCII_DATA=00000000;--复位ELSEIFPREPARED='1'THEN--PREPARED为”1”时进行转换,PREPARED信号由前级的REQUEST输出信号提供IFSCAN_DATA=01000101THENASCII_DATA=00110000;ELSIFSCAN_DATA=00010110THENASCII_DATA=00110001;ELSIFSCAN_DATA=00011110THENASCII_DATA=00110010;ELSIFSCAN_DATA=00100110THENASCII_DATA=00110011;ELSIFSCAN_DATA=00100101THENASCII_DATA=00110100;ELSIFSCAN_DATA=00101110THENASCII_DATA=00110101;ELSIFSCAN_DATA=00110110THENASCII_DATA=00110110;ELSIFSCAN_DATA=00111101THENASCII_DATA=00110111;ELSIFSCAN_DATA=00111110THENASCII_DATA=00111000;ELSIFSCAN_DATA=01000110THENASCII_DATA=00111001;ELSIFSCAN_DATA=00011100THENASCII_DATA=01000001;ELSIFSCAN_DATA=00110010THENASCII_DATA=01000010;ELSIFSCAN_DATA=00100001THENASCII_DATA=01000011;ELSIFSCAN_DATA=00100011THENASCII_DATA=01000100;ELSIFSCAN_DATA=00100100THENASCII_DATA=01000101;ELSIFSCAN_DATA=00101011THENASCII_DATA=01000110;ELSIFSCAN_DATA=00110100THENASCII_DATA=01000111;ELSIFSCAN_DATA=00110011THENASCII_DATA=01001000;ELSIFSCAN_DATA=01000011THENASCII_DATA=01001001;ELSIFSCAN_DATA=00111011THENASCII_DATA=01001010;ELSIFSCAN_DATA=01000010THENASCII_DATA=01001011;ELSIFSCAN_DATA=01001011THENASCII_DATA=01001100;ELSIFSCAN_DATA=00111010THENASCII_DATA=01001101;ELSIFSCAN_DATA=00110001THENASCII_DATA=01001110;ELSIFSCAN_DATA=01000100THENASCII_DATA=01001111;ELSIFSCAN_DATA=01001101THENASCII_DATA=01010000;ELSIFSCAN_DATA=00010101THENASCII_DATA=01010001;ELSIFSCAN_DATA=00101101THENASCII_DATA=01010010;ELSIFSCAN_DATA=00011011THENASCII_DATA=01010011;ELSIFSCAN_DATA=00101100THENASCII_DATA=01010100;ELSIFSCAN_DATA=00111100THENASCII_DATA=01010101;ELSIFSCAN_DATA=00101010THENASCII_DATA=01010110;ELSIFSCAN_DATA=00011101THENASCII_DATA=01010111;ELSIFSCAN_DATA=00100010THENASCII_DATA=
本文标题:可编程器件与硬件描述语言VHDL-PS2键盘
链接地址:https://www.777doc.com/doc-3865158 .html