📄 min.txt
字号:
在网上看了好多关于SPI的VHDL代码,真是不敢苟同。有心情自己写了一个移位寄存器,及其接口并通过仿真:
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity shifter is
generic ( N : integer range 1 to 16 :=6); --设定移位寄存器的宽度为6
port(
din : in std_logic_vector(N-1 downto 0); --要进行并串转换的N比特宽度矢量
load : in std_logic; --将数据打入移位寄存器控制信号,其宽度必须大于一个SCLK周期
sclk : in std_logic; --移位时钟
reset : in std_logic; --复位输入信号
rdy : out std_logic; --准备好输出信号,表示该移位寄存器可以接收下一个数据了
dout : out std_logic); --串行输出信号。
end entity;
architecture behavor of shifter is
signal shifter : std_logic_vector( N-1 downto 0);
signal counter : integer range 0 to N+2;
signal translate : std_logic;
signal pass : std_logic;
signal star_counter : std_logic;
signal stop_counter : std_logic;
begin
process(reset,sclk)
begin
if(reset='0')then
dout<='Z'; --平时输出为高阻状态
counter<=0; --计数器清零
translate<='0'; --表示移位寄存器传输命令
rdy<='1'; --准备好接收数据
pass<='0'; --数据打入命令
stop_counter<='0'; --计数器停止计数
elsif(sclk'event and sclk='1')then
if(star_counter='1')then
if(counter=0)then
pass<='1'; --clock the data into shifter
translate<='1';
counter<=counter+1;
elsif(counter=1)then
pass<='0';
counter<=counter+1;
elsif(counter=N+1) then
stop_counter<='1';
counter<=counter+1;
elsif(counter=N+2)then
counter<=0;
rdy<='1';
translate<='0';
else
counter<=counter+1;
end if;
end if;
end if;
end process;
process(load,sclk,reset)
begin
if(reset='0')then
star_counter<='0';
elsif(sclk'event and sclk='1')then
if(load='0')then
star_counter<='1';
elsif(stop_counter='1')then
star_counter<='0';
end if;
end if;
end process;
process(sclk,translate,pass)
begin
if(pass='1')then --接收到数据打入命令
shifter<=din;
elsif(translate='1')then --接收到传送命令
if(sclk'event and sclk='1')then
dout<=shifter(0);
shift: for i in 0 to (N-2) LOOP
shifter(i)<=shifter(i+1);
end LOOP;
shifter(N-1)<='0';
end if;
else dout<='Z';
end if;
end process;
end behavor;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -