📄 rxd.vhd
字号:
-------------------------------------------------------------------------------------
--name:rxd.vhd
--author:Mouyun Liu
--date:2008.12.11
--function:uart模块中数据接收模块,实现数据接收功能
--changing log:
--2009.02.09:修改tmpreg8的长度,由原来的存储停止位改为不存储,最终依然存储
--2009.02.07:修改通信协议,去掉fifo功能,clk_out信号改名为cr(change request),作为
--一字节数据接收完毕信号,取消wr,fifo_full信号
--2009.01.29:修改wr信号赋值方式,添加start标记,表明新的数据接收
--2009.01.28:取消发送部分功能,即取消三个端口txd_busy,txd_cs,rcv_state及相应功能语句
--2008.12.18:Create this unit
-------------------------------------------------------------------------------------
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
use work.common.all;
entity rxd is
--clk_in:时钟输入
--cr:cr上升沿表明一字节数据接收完毕,pul_gen模块可以进行读操作
--data_in:数据输入
--data_out:并行数据输出
port
(
clk_in : in std_logic;
cr : out std_logic;
data_in : in std_logic;
-- data_out : out std_logic_vector(7 downto 0)
data_out : out std_logic_vector(18 downto 0)
);
end rxd;
architecture behav of rxd is
--tmpreg8:tmpreg8中存储接收到的数据,包括一位起始位,奇偶校验位,包括停止位
--tmpreg8(0):存储的是最后接收到的一位奇偶校验位
--tmpreg8(data_length - 1):存储的是最开始接收到的起始位
-- signal tmpreg8 : std_logic_vector(data_length - 1 downto 0);
signal tmpreg8 : std_logic_vector(18 downto 0);
--sig2:接收时钟计数;sig3:接收数据位数;sig4:串并变换时钟
signal sig2 : integer range 0 to 15;
signal sig3 : integer range 0 to 15;
-- signal sig5 : integer range 0 to 15;
--sig4:由0到1的跳变表明读取一位数据
signal sig4 : std_logic;
signal start : std_logic := '0';
begin
process(clk_in)
variable cnt : natural range 0 to 31 := 0;
begin
if clk_in'event and clk_in = '0' then
if start = '0' then --空闲状态
if data_in = '0' then --此if语句用于判断是否起始位,是,则start置为“1”
if sig2 = 7 then --对应于起始位的处理
sig2 <= 0;
start <= '1';
-- sig4 <= '1'; --2009.03.01
sig3 <= 1;
else
sig2 <= sig2 + 1;
-- sig4 <= '0'; --2009.03.01
cr <= '0';
end if;
--2009.03.01
sig4 <= '0';
if cnt > 18 then
cnt := 0;
end if;
--
else
sig2 <= 0;
cr <= '0'; --2009.02.11
end if;
else --接收数据状态
if sig2 = 15 then --对应于数据位的处理
sig2 <= 0;
if sig3 = data_length - 1 then --接收完一帧否?若接收完,则start置‘0’
sig3 <= 0;
start <= '0';
-- sig4 <= '1'; --2009.01.29
--调用奇偶校验函数时,停止位还没被接收,故进行奇偶校验时校验的对象需要注意
-- if uartcheck(uart_check,tmpreg8) = true then
-- cr <= '1';
-- end if;
if cnt > 18 then
cr <= '1';
end if;
else
if (sig3 < 9) and (cnt < 19) then
sig4 <= '1';
cnt := cnt + 1;
else
sig4 <= '0';
end if;
sig3 <= sig3 + 1;
-- cr <= '0';
end if;
else
sig2 <= sig2 + 1;
sig4 <= '0';
end if;
end if;
end if;
end process;
--此过程完成接收数据的串并转换
--数据接收完毕后,tmpreg8(0)对应奇偶校验位,tmpreg8(1)对应数据的最高位,……
process(sig4)
begin
if sig4'event and sig4 = '1' then
for i in tmpreg8'high downto tmpreg8'low + 1 loop
tmpreg8(i) <= tmpreg8(i - 1);
end loop;
tmpreg8(tmpreg8'low) <= data_in;
end if;
end process;
--2009.01.29
process(clk_in)
begin
if clk_in'event and clk_in = '1' then
for i in data_out'high downto data_out'low loop
data_out(i) <= tmpreg8(data_out'high - i); --输出
end loop;
end if;
end process;
end behav;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -