library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
use std.textio.all;
entity RS485serialInterface is
port(
Clk : in std_logic;
reset : in std_logic;
-- Rclk : in std_logic;
-- Tclk : in std_logic;
RS485RO : in std_logic;
receiverData : out std_logic_vector(7 downto 0);
RS485DI : out std_logic;
transferData : in std_logic_vector(7 downto 0);
busy : out std_logic;
CTTPfifoRdreq : in std_logic
);
end RS485serialInterface;
architecture Art_RS485serialInterface of RS485serialInterface is
signal receiverReg : std_logic_vector(7 downto 0);
signal transmitterReg : std_logic_vector(9 downto 0);
signal counter : std_logic_vector(7 downto 0);
signal cont : std_logic_vector(7 downto 0);
type State1 is (WaitStartBit,GetStartBit,GetDataBit0,GetDataBit1,GetDataBit2,GetDataBit3,GetDataBit4,GetDataBit5,GetDataBit6,GetDataBit7,GetStopBit,GetData);
signal receiverState : State1;
type State2 is (Idle,transStartBit,transDataBit0,transDataBit1,transDataBit2,transDataBit3,transDataBit4,transDataBit5,transDataBit6,transDataBit7,transStopBit);
signal transferState : State2;
begin
-----------------RS485 receiver-----------
process(Clk,reset)
begin
if reset='1' then
receiverReg<=(others=>'0');
receiverData<=(others=>'0');
counter<=(others=>'0');
receiverState<=WaitStartBit;
elsif Clk'event and Clk='1' then
case receiverState is
when WaitStartBit=>
receiverReg<=(others=>'0');
if RS485RO='0' then
counter<=counter+1;
receiverState<=GetStartBit;
else
counter<=(others=>'0');
receiverState<=WaitStartBit;
end if;
when GetStartBit=>
if counter="110010" then
counter<=(others=>'0');
receiverState<=GetDataBit0;
elsif RS485RO='0' then
counter<=counter+1;
receiverState<=GetStartBit;
else
receiverState<=WaitStartBit;
end if;
when GetDataBit0=>
if counter="1100100" then
receiverReg(0)<=RS485RO;
counter<=(others=>'0');
receiverState<=GetDataBit1;
else
counter<=counter+1;
receiverState<=GetDataBit0;
end if;
when GetDataBit1=>
if counter="1100100" then
receiverReg(1)<=RS485RO;
counter<=(others=>'0');
receiverState<=GetDataBit2;
else
counter<=counter+1;
receiverState<=GetDataBit1;
end if;
when GetDataBit2=>
if counter="1100100" then
receiverReg(2)<=RS485RO;
counter<=(others=>'0');
receiverState<=GetDataBit3;
else
counter<=counter+1;
receiverState<=GetDataBit2;
end if;
when GetDataBit3=>
if counter="1100100" then
receiverReg(3)<=RS485RO;
counter<=(others=>'0');
receiverState<=GetDataBit4;
else
counter<=counter+1;
receiverState<=GetDataBit3;
end if;
when GetDataBit4=>
if counter="1100100" then
receiverReg(4)<=RS485RO;
counter<=(others=>'0');
receiverState<=GetDataBit5;
else
counter<=counter+1;
receiverState<=GetDataBit4;
end if;
when GetDataBit5=>
if counter="1100100" then
receiverReg(5)<=RS485RO;
counter<=(others=>'0');
receiverState<=GetDataBit6;
else
counter<=counter+1;
receiverState<=GetDataBit5;
end if;
when GetDataBit6=>
if counter="1100100" then
receiverReg(6)<=RS485RO;
counter<=(others=>'0');
receiverState<=GetDataBit7;
else
counter<=counter+1;
receiverState<=GetDataBit6;
end if;
when GetDataBit7=>
if counter="1100100" then
receiverReg(7)<=RS485RO;
counter<=(others=>'0');
receiverState<=GetStopBit;
else
counter<=counter+1;
receiverState<=GetDataBit7;
end if;
when GetStopBit=>
if counter="1100100" then
counter<=(others=>'0');
receiverState<=GetData;
else
counter<=counter+1;
receiverState<=GetStopBit;
end if;
when GetData=>
receiverData<=receiverReg;
receiverState<=WaitStartBit;
when others=>
receiverReg<=(others=>'0');
receiverData<=(others=>'0');
counter<=(others=>'0');
receiverState<=WaitStartBit;
end case;
end if;
end process;
--------------RS485 transfer---------
process(Clk,reset)
begin
if reset='1' then
RS485DI<='1';
busy<='0';
cont<=(others=>'0');
transferState<=Idle;
elsif Clk'event and Clk='1' then
case transferState is
when Idle=>
if CTTPfifoRdreq='1' then
busy<='1';
transferState<=transStartBit;
else
busy<='0';
transferState<=Idle;
end if;
when transStartBit=>
if(cont="1100100")then
busy<='1';
RS485DI<='0';
cont<=(others=>'0');
transferState<=transDataBit0;
else
cont<=cont+1;
busy<='1';
RS485DI<='0';
transferState<=transStartBit;
end if;
when transDataBit0=>
if(cont="1100100")then
busy<='1';
RS485DI<=transferData(0);
cont<=(others=>'0');
transferState<=transDataBit1;
else
cont<=cont+1;
busy<='1';
RS485DI<=transferData(0);
transferState<=transDataBit0;
end if;
when transDataBit1=>
if(cont="1100100")then
busy<='1';
RS485DI<=transferData(1);
cont<=(others=>'0');
transferState<=transDataBit2;
else
cont<=cont+1;
busy<='1';
RS485DI<=transferData(1);
transferState<=transDataBit1;
end if;
when transDataBit2=>
if(cont="1100100")then
busy<='1';
RS485DI<=transferData(2);
cont<=(others=>'0');
transferState<=transDataBit3;
else
cont<=cont+1;
busy<='1';
RS485DI<=transferData(2);
transferState<=transDataBit2;
end if;
when transDataBit3=>
if(cont="1100100")then
busy<='1';
RS485DI<=transferData(3);
cont<=(others=>'0');
transferState<=transDataBit4;
else
cont<=cont+1;
busy<='1';
RS485DI<=transferData(3);
transferState<=transDataBit3;
end if;
when transDataBit4=>
if(cont="1100100")then
busy<='1';
RS485DI<=transferData(4);
cont<=(others=>'0');
transferState<=transDataBit5;
else
cont<=cont+1;
busy<='1';
RS485DI<=transferData(4);
transferState<=transDataBit4;
end if;
when transDataBit5=>
if(cont="1100100")then
busy<='1';
RS485DI<=transferData(5);
cont<=(others=>'0');
transferState<=transDataBit6;
else
cont<=cont+1;
busy<='1';
RS485DI<=transferData(5);
transferState<=transDataBit5;
end if;
when transDataBit6=>
if(cont="1100100")then
busy<='1';
RS485DI<=transferData(6);
cont<=(others=>'0');
transferState<=transDataBit7;
else
cont<=cont+1;
busy<='1';
RS485DI<=transferData(6);
transferState<=transDataBit6;
end if;
when transDataBit7=>
if(cont="1100100")then
busy<='1';
RS485DI<=transferData(7);
cont<=(others=>'0');
transferState<=transStopBit;
else
cont<=cont+1;
busy<='1';
RS485DI<=transferData(7);
transferState<=transDataBit7;
end if;
when transStopBit=>
if(cont="1100100")then
busy<='1';
RS485DI<='1';
cont<=(others=>'0');
transferState<=Idle;
else
cont<=cont+1;
busy<='1';
RS485DI<='1';
transferState<=transStopBit;
end if;
when others=>
busy<='0';
RS485DI<='0';
transferState<=Idle;
end case;
end if;
end process;
end Art_RS485serialInterface;
阅读(1432) | 评论(0) | 转发(0) |