⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi.vhd

📁 VHDL 实现的SPI接口
💻 VHD
字号:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
package dpack is
	type LEDarray is array (5 downto 0) of std_logic_vector(9 downto 0); 	
	subtype integer6 is integer range 5 downto 0;
	function CONV_integer6(threebits:std_logic_vector(2 downto 0)) return integer6;
end dpack;

package body dpack is
	function CONV_integer6(threebits:std_logic_vector(2 downto 0)) return integer6 is
	-------------this function is used to convert a 3 bit vector to a integer between 0 and 7
	variable result:integer6;
	variable tmp:integer;
	begin
		tmp:=CONV_integer(threebits);
		if tmp>5 then
			result:=0;
		else
			result:=tmp;
		end if;
		return result;
	end CONV_integer6;
end dpack;


library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
use work.dpack.all;

entity disp is
port(
	------------------------SPI interface
	MISO:out std_logic;
	MOSI:in std_logic;
	SPSCK:in std_logic;
	SS:in std_logic;
	IRQ:out std_logic;
	-----------------------SPI interface
	KB:in std_logic_vector(4 downto 0);
	LEDID:out std_logic_vector(4 downto 0);
	LED:out LEDarray);
end disp;

architecture struc of disp is
signal rx,tx:std_logic_vector(15 downto 0);
signal CodeDog:std_logic_vector(2 downto 0);
begin
	SPItx:process(SS,SPSCK)
	begin
		if SS='1' then
			tx(15 downto 13)<=CodeDog(2 downto 0);
			tx(12 downto 8)<=KB;
			tx(7 downto 5)<=CodeDog(2 downto 0);
			tx(4 downto 0)<=KB;
			MISO<='Z';
		elsif SPSCK'event and SPSCK='0' then
			MISO<=tx(0);
			tx(14 downto 0)<=tx(15 downto 1);
		end if;
	end process;
	
	SPIrx:process(SPSCK)
	begin
		if SS='0' then
			if SPSCK'event and SPSCK='1' then
				rx(0)<=MOSI;
				rx(15 downto 1)<=rx(14 downto 0);
			end if;	
		end if;
	end process;
	
	disp:process(SS)
	begin
		if SS'event and SS='1' then
			case rx(13 downto 11) is
				when "001"=>
				LED(0)(9 downto 7)<=rx(10 downto 8);
				LED(0)(6 downto 0)<=rx(6 downto 0);
				when "010"=>
				LED(1)(9 downto 7)<=rx(10 downto 8);
				LED(1)(6 downto 0)<=rx(6 downto 0);
				when "100"=>
				LED(2)(9 downto 7)<=rx(10 downto 8);
				LED(2)(6 downto 0)<=rx(6 downto 0);
				when "101"=>
				LED(3)(9 downto 7)<=rx(10 downto 8);
				LED(3)(6 downto 0)<=rx(6 downto 0);
				when "110"=>
				LED(4)(9 downto 7)<=rx(10 downto 8);
				LED(4)(6 downto 0)<=rx(6 downto 0);
				when "111"=>
				LED(5)(9 downto 7)<=rx(10 downto 8);
				LED(5)(6 downto 0)<=rx(6 downto 0);
				when "000"=>
				LEDID<=rx(4 downto 0);
				when "011"=>
				CodeDog(2)<=(rx(7) xor rx(6)) xor rx(5);
				CodeDog(1)<=(rx(4) xor rx(3)) xor rx(2);
				CodeDog(0)<=rx(1) xor rx(0);
				when others=>
				null;
			end case;
		end if;
	end process;
end struc;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -