📄 serialout.vhd
字号:
----------------------------------------------------
--芳元电子工作室,版权所有,严禁用于商业用途
--实体名:serialout
--功 能:串口发送控制器
--接 口:clk -为时钟信号,波特率为clk的1/4
-- wr -写使能信号
-- qin -要发送的数据,8bit
-- tdempty-数据发送完毕指示信号,发送完为1,
-- 发送时为0
-- txd -数据串行输出
--作 者:Justin Xu
--日 期:2005-06-21
----------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity serialout is
port
(clk : in std_logic;
wr : in std_logic;
qin : in std_logic_vector(7 downto 0):="01000001";
tdempty: out std_logic;
txd : out std_logic
);
end serialout;
architecture behave of serialout is
constant framelent:integer:=8;
type state_type is (x_idle,x_start,x_wait,x_shift,x_stop);
signal state : state_type:=x_idle;
signal qinbuf:std_logic_vector(7 downto 0);
begin
process(clk,wr,qin)
variable xcnt : std_logic_vector(3 downto 0):="0000";
variable xbitcnt : integer:=0;
variable txds : std_logic;
begin
if clk'event and clk='1' then
case state is
when x_idle=>if wr='1' then --状态1,等待数据发送命令
state<=x_start;
qinbuf<=qin;
else
state<=x_idle;
end if;
txds:='1';
tdempty<='1';
when x_start=>if xcnt="0011" then --状态2,发送启动位信号
state<=x_wait;
xcnt:="0000";
txds:='0';
else
state<=x_start;
xcnt:=xcnt+1;
end if;
tdempty<='0';
when x_wait=>if xcnt="0010" then --状态3,等待
if xbitcnt=framelent then
state<=x_stop;
xbitcnt:=0;
else
state<=x_shift;
end if;
xcnt:="0000";
else
xcnt:=xcnt+1;
state<=x_wait;
end if;
tdempty<='0';
when x_shift=>txds:=qinbuf(xbitcnt);--状态4,将待发送数据进行串并转换
xbitcnt:=xbitcnt+1;
state<=x_wait;
tdempty<='0';
when x_stop=>if xcnt="0011" then --状态5,发送停止位
state<=x_idle;
xcnt:="0000";
else
xcnt:=xcnt+1;
txds:='1';
state<=x_stop;
end if;
tdempty<='0';
when others=>state<=x_idle;
end case;
end if;
txd<=txds;
end process;
end behave;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -