|
MAXII CPLD 实验板
PS2口通信程序
|
注: |
本实验是针对 MC570/MC240 实验板 设计的,如选用其它开发板实现,部分内容需做调整。 |
|
实现一个PS2接口,接收PC104键盘的扫描码,以BCD码的形式显示在8位LED灯上。 |
1. |
点击 这里 下载示例工程。*.qar 是 QuartusII 的压缩文档,用 QuartusII6.0 打开。 |
|
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
USE IEEE.STD_LOGIC_ARITH.ALL;
ENTITY maxii_ps2 IS
PORT(
clk : IN STD_LOGIC;
ps2clk : IN STD_LOGIC;
ps2dat : IN STD_LOGIC;
leddrv : OUT STD_LOGIC_VECTOR(7 DOWNTO 0)
);
END maxii_ps2;
ARCHITECTURE maxii_ps2 OF maxii_ps2 IS
SIGNAL click : STD_LOGIC :='0';
SIGNAL ps2clkDly, ps2clkDlyLast : STD_LOGIC :='1';
SIGNAL ps2datDly, ps2datDlyLast : STD_LOGIC :='1';
SIGNAL ps2clkDown : STD_LOGIC :='0';
SIGNAL mState : INTEGER RANGE 0 TO 7 :=0;
SIGNAL rShift : STD_LOGIC_VECTOR(7 DOWNTO 0);
SIGNAL cnt : INTEGER RANGE 0 TO 8 :=0;
CONSTANT IDLE : INTEGER :=0;
CONSTANT READY : INTEGER :=1;
CONSTANT COLLECT : INTEGER :=2;
CONSTANT PARITY : INTEGER :=3;
CONSTANT STOP : INTEGER :=4;
BEGIN
PROCESS(clk)
VARIABLE cnt : INTEGER :=0;
BEGIN
IF clk'EVENT AND clk='1' THEN
IF cnt=499 THEN
cnt := 0;
click <= '1';
ELSE
cnt := cnt+1;
click <= '0';
END IF;
END IF;
END PROCESS;
PROCESS(clk)
BEGIN
IF clk'EVENT AND clk='1' THEN
IF click='1' THEN
ps2clkDly <= ps2clk;
ps2datDly <= ps2dat;
ps2clkDlyLast <= ps2clkDly;
ps2datDlyLast <= ps2datDly;
END IF;
END IF;
END PROCESS;
ps2clkDown <= '1' WHEN ps2clkDly='1' AND ps2clkDlyLast='0' ELSE
'0';
PROCESS(clk)
BEGIN
IF clk'EVENT AND clk='1' THEN
IF click='1' THEN
CASE mState IS
WHEN IDLE =>
IF ps2datDlyLast='0' THEN
mState <= READY;
END IF;
WHEN READY =>
IF ps2clkDown='1' THEN
mState <= COLLECT;
END IF;
WHEN COLLECT =>
IF ps2clkDown='1' THEN
IF cnt=7 THEN
mState <= PARITY;
END IF;
END IF;
WHEN PARITY =>
IF ps2clkDown='1' THEN
mState <= STOP;
END IF;
WHEN STOP =>
IF ps2clkDown='1' THEN
mState <= IDLE;
END IF;
WHEN OTHERS => NULL;
END CASE;
END IF;
END IF;
END PROCESS;
PROCESS(clk)
BEGIN
IF clk'EVENT AND clk='1' THEN
IF click='1' THEN
CASE mState IS
WHEN READY =>
IF ps2clkDown='1' THEN
cnt <= 0;
END IF;
WHEN COLLECT =>
IF ps2clkDown='1' THEN
cnt <= cnt+1;
rShift <= ps2datDlyLast & rShift(7 DOWNTO 1);
END IF;
WHEN STOP =>
IF ps2clkDown='1' THEN
leddrv <= rShift;
END IF;
WHEN OTHERS => NULL;
END CASE;
END IF;
END IF;
END PROCESS;
END maxii_ps2;
|
|
|
|