📄 ps2.vhd
字号:
------------------------------------------------------------------------------------ Company: -- Engineer: -- -- Create Date: 21:39:44 12/03/2007 -- Design Name: -- Module Name: PS2 - Behavioral -- Project Name: -- Target Devices: -- Tool versions: -- Description: ---- Dependencies: ---- Revision: -- Revision 0.01 - File Created-- Additional Comments: ------------------------------------------------------------------------------------library IEEE;use IEEE.STD_LOGIC_1164.ALL;use IEEE.STD_LOGIC_ARITH.ALL;use IEEE.STD_LOGIC_UNSIGNED.ALL;---- Uncomment the following library declaration if instantiating---- any Xilinx primitives in this code.--library UNISIM;--use UNISIM.VComponents.all;entity PS2 is Port ( dclk : in STD_LOGIC; datain : in STD_LOGIC; lcd_e : out STD_LOGIC; lcd_rw : out STD_LOGIC; lcd_rs : out STD_LOGIC; sf_d : out STD_LOGIC_VECTOR (3 downto 0); clk : in STD_LOGIC);end PS2;architecture Behavioral of PS2 issignal count : integer range 0 to 10 :=0;signal lword1,lword2 : std_logic_vector(3 downto 0);signal hword1,hword2 : std_logic_vector(3 downto 0);signal state : integer range 0 to 7 := 0;Type data is array (0 to 3, 0 to 1) of std_logic_vector(3 downto 0);constant cmd : data := (("0010","1000"),--Function set ("0000","0110"),--Entry mode set ("0000","1100"),--Display on/off ("1000","0000")--Set DDram address );signal index : integer range 0 to 4 :=0;signal count0 : integer range 0 to 750000 := 0;signal sflag : std_logic := '0';signal flag : integer range 0 to 15 := 0;signal twochar: std_logic :='0';beginprocess(clk)beginif rising_edge(clk) then if(state = 0) then --initializtion if(flag = 0) then if count0 = 750000 then flag <= 1; sf_d <= "0011"; lcd_e <= '1'; count0 <= 0; else count0 <= count0+1; end if; elsif flag = 1 then if count0 = 12 then flag <= 2; lcd_e <= '0'; count0 <= 0; else count0 <= count0+1; end if; elsif flag = 2 then if count0 = 205000 then flag <= 3; sf_d <= "0011"; lcd_e <= '1'; count0 <= 0; else count0 <= count0+1; end if; elsif flag = 3 then if count0 = 12 then flag <= 4; lcd_e <= '0'; count0 <= 0; else count0 <= count0+1; end if; elsif flag = 4 then if count0 = 5000 then flag <= 5; sf_d <= "0011"; lcd_e <= '1'; count0 <= 0; else count0 <= count0+1; end if; elsif flag = 5 then if count0 = 12 then flag <= 6; lcd_e <= '0'; count0 <= 0; else count0 <= count0+1; end if; elsif flag = 6 then if count0 = 2000 then flag <= 7; sf_d <= "0010"; lcd_e <= '1'; count0 <= 0; else count0 <= count0+1; end if; elsif flag = 7 then if count0 = 12 then flag <= 8; lcd_e <= '0'; count0 <= 0; else count0 <= count0+1; end if; else if count0 = 2000 then flag <= 0; count0 <= 0; state <= 1; lcd_rs <= '0'; lcd_rw <= '0'; sf_d <= cmd(index,0);--the high 4bits else count0 <= count0+1; end if; end if; elsif state = 1 then --send command if flag = 0 then --delay 40ns if count0 = 2 then flag <= 1; count0 <= 0; lcd_e <= '1'; else count0 <= count0+1; end if; elsif flag = 1 then --delay 230ns if count0 = 12 then flag <= 2; lcd_e <= '0'; count0 <= 0; else count0 <= count0+1; end if; elsif flag = 2 then --delay 20ns if count0 = 1 then if sflag = '0' then --the word is not finished flag <= 3; else flag <= 4; end if; lcd_rw <= '1'; count0 <= 0; else count0 <= count0+1; end if; elsif flag = 3 then --delay 1us,low 4bits if count0 = 50 then sflag <= '1'; flag <= 0; lcd_rw <= '0'; count0 <= 0; sf_d <= cmd(index,1); index <= index+1; else count0 <= count0+1; end if; elsif flag = 4 then --delay 40us, the word is finished if count0 = 2000 then sflag <= '0'; flag <= 0; lcd_rw <= '0'; count0 <= 0; if index = 4 then lcd_rs <= '1'; state <= 2; else sf_d <= cmd(index,0); end if; else count0 <= count0+1; end if; end if; elsif state = 2 then --read data if dclk = '0' then --transfer start if twochar = '0' then --character if count=9 then --parity check case hword1&lword1 is when "00011100"=>hword1<="0110";lword1<="0001";--a
when "00110010"=>hword1<="0110";lword1<="0010";--b
when "00100001"=>hword1<="0110";lword1<="0011";--c
when "00100011"=>hword1<="0110";lword1<="0100";--d
when "00100100"=>hword1<="0110";lword1<="0101";--e
when "00101011"=>hword1<="0110";lword1<="0110";--f
when "00110100"=>hword1<="0110";lword1<="0111";--g when others=>hword1<="1111";lword1<="0000"; end case;
state <= 3; elsif count=11 then --transfer finished sf_d <= hword1; twochar <= '1'; count <= 0;
state <= 3; elsif (count>0 and count<5) then--low 4bits lword1(count-1) <= datain;
state <= 3; elsif (count>4 and count<9) then --high 4bits hword1(count-5) <= datain;
state <= 3;
else
state <= 3; end if; else if count=11 then if ( lword2="0000" and hword2="1111") then twochar <= '0'; state <= 4;
else
state <= 3; end if; count <= 0; elsif (count>0 and count<5) then lword2(count-1) <= datain;
state <= 3; elsif (count>4 and count<9) then --high 4bits hword2(count-5) <= datain;
state <= 3;
else
state <= 3; end if; end if; end if; elsif state = 3 then --read next bit if count0 = 4000 then --delay 80us count <= count+1; state <= 2; count0 <= 0; else count0 <= count0+1; end if; elsif state = 4 then --send characters if flag = 0 then --delay 40ns if count0 = 2 then flag <= 1; count0 <= 0; lcd_e <= '1'; else count0 <= count0+1; end if; elsif flag = 1 then --delay 230ns if count0 = 12 then flag <= 2; lcd_e <= '0'; count0 <= 0; else count0 <= count0+1; end if; elsif flag = 2 then --delay 20ns if count0 = 1 then if sflag = '0' then --the word is not finished flag <= 3; else flag <= 4; end if; lcd_rw <= '1'; count0 <= 0; else count0 <= count0+1; end if; elsif flag = 3 then --delay 1us,low 4bits if count0 = 50 then sflag <= '1'; flag <= 0; lcd_rw <= '0'; count0 <= 0; sf_d <= lword1 ; else count0 <= count0+1; end if; elsif flag = 4 then --delay 40us if count0 = 2000 then sflag <= '0'; flag <= 0; lcd_rw <= '0'; state <= 2; count0 <= 0; else count0 <= count0+1; end if; end if; end if; end if;end process;end Behavioral;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -