数字信号发送和接收的VHDL源码
2012-10-30
标签:

【数字信号的发送和接收】:设计一个5位数字信号的发送和接收电路,把并行码变为串行码或把串行码变为并行码,串行偶校验检测器可通过异或实现。在数据接收端,只有在代码传送无误后,才把数据代码并行输出。数据传送的格式采用异步串行通信的格式,包含起始位、数据位、校验位、停止位和空闲位。

数据发送模块:将并行数据加上起始位、偶校验位和停止位,以串行方式发送出去。

library ieee;

use ieee.std_logic_1164.all;

use ieee.std_logic_unsigned.all;

entity send is

port(start:in std_logic;

a: in std_logic_vector(4 downto 0);

clk: in std_logic;

b:out std_logic);

end send;

architecture rt1 of send is

begin

process(clk,a)

variable temp:std_logic_vector(6 downto 0);

variable tmp,m:std_logic;

variable tmp1:integer range 0 to 7;

begin

if(clk even and clk='1')then

if(m='0')then--发送空闲位

tmp:='1';

end if;

if(start='0')then--start键有效

m:='1';

temp(5 downto 1):=a(4 downto 0);--数据位

temp(6):=a(4)xor a(3) xor a(2) xor a(1) xor a(0); --偶校验位

temp(0):='0';--起始位

elsif(m='1') then

tmp:=temp(0);--发送数据

temp:='1'&temp(6 downto 1);

if(tmp1<7)then--一次发送8位数据,在最后加上停止位‘1’

temp:=temp1+1;

else

m:='0';

temp:="0000000";

tmp1:=0;

end if;

end if;

end if;

b<=tmp;

end process;

end rt1;

数据接收模块,当检测到起始位时,将数据位和校验位取出,若校验无误,则并行送出,若有误则报警。

library ieee;

use ieee.std_logic_1164.all;

entity rcv is

port(clk,re:in std_logic;

imp:out std_logic_vector(4 downto 0);

alm:out std_logic);

end rcv;

architecture rcv_arc of rcv is

begin

process(clk)

variablea:std_logic;

variable cnt:integer range 0 to 6;

variable shift:std_logic_vector(5 downto 0);

begin

if clk'event and clk='1' then

if a='0' then

if re='0' then--检测起始位

a:='1';

alm<='0';

end if;

else

if cnt<6 then

shift:=shift(4 downto 0)&re; --取出数据位和校验位

cnt:=cnt+1;

else

cnt:=0;

a:='0';

if(shift(0) xor shift(1) xor shift(2) xor shift(3) xor shift(4) xor shift(5)='0' then

imp<=shift(5 downto 1); --若校验无误,则送出

else

alm<='1';--检测到错误则报警

end if;

end if;

end if;

end if;

end process;

end rcv_arc;

可能会用到的工具/仪表
相关文章
推荐文章
热门文章
章节目录
本站简介 | 意见建议 | 免责声明 | 版权声明 | 联系我们
CopyRight@2024-2039 嵌入式资源网
蜀ICP备2021025729号