--功能:实现cpld与pc机通讯
--原理:模拟232的发送时序每次发送10bits,一个开始位,八个数据位,一个停止位.
--波特率:9600bps
--环境:cpld器件:epm7128stc100-10,外部1m 时钟,pc机端:串口调试助手.
--操作:从串口调试助手中发送一个byte.cpld接收到信号再返回到串口调试助手.
--本程序仅供学习,不得用与商业.
--作者:sagestar
LIBRARY IEEE;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
--USE IEEE.STD_LOGIC_ARITH.ALL;
ENTITY cpld232 IS
PORT(
clk: in std_logic;
rx: in std_logic;
tx: out std_logic;
rtx_control: in std_logic
);
END cpld232;
ARCHITECTURE cpld OF cpld232 IS
signal Rx_Buffer: std_logic_vector(7 downto 0);
signal Tx_Buffer: std_logic_vector(7 downto 0);
BEGIN
--receive subprogrammer
PROCESS(clk,rx)
variable Baud_Number1:integer range 0 to 100;
variable Rx_start: std_logic_vector(7 downto 0);
variable Rx_Over:std_logic;
variable Rx_Complete:std_logic;
BEGIN
if clk'event and clk='0' then
if rx='0'and Rx_Start="00000000"and Rx_Over='0'and Rx_Complete='0' then
Baud_Number1:=Baud_Number1+1;
if Baud_Number1=100 then
--Rx_Buffer(0) <= rx;
Baud_Number1:=0;
Rx_Start(0):='1';
end if;
elsif Rx_Start(0)='1' then
Baud_Number1:=Baud_Number1+1;
if Baud_Number1=100 then
Rx_Buffer(0) <= rx;
Baud_Number1:=0;
Rx_Start(0):='0';
Rx_Start(1):='1';
end if;
elsif Rx_Start(1)='1' then
Baud_Number1:=Baud_Number1+1;
if Baud_Number1=100 then
Rx_Buffer(1) <= rx;
Baud_Number1:=0;
Rx_Start(1):='0';
Rx_Start(2):='1';
end if;
elsif Rx_Start(2)='1' then
Baud_Number1:=Baud_Number1+1;
if Baud_Number1=100 then
Rx_Buffer(2) <= rx;
Baud_Number1:=0;
Rx_Start(2):='0';
Rx_Start(3):='1';
end if;
elsif Rx_Start(3)='1' then
Baud_Number1:=Baud_Number1+1;
if Baud_Number1=100 then
Rx_Buffer(3) <= rx;
Baud_Number1:=0;
Rx_Start(3):='0';
Rx_Start(4):='1';
end if;
elsif Rx_Start(4)='1' then
Baud_Number1:=Baud_Number1+1;
if Baud_Number1=100 then
Rx_Buffer(4) <= rx;
Baud_Number1:=0;
Rx_Start(4):='0';
Rx_Start(5):='1';
end if;
elsif Rx_Start(5)='1' then
Baud_Number1:=Baud_Number1+1;
if Baud_Number1=100 then
Rx_Buffer(5) <= rx;
Baud_Number1:=0;
Rx_Start(5):='0';
Rx_Start(6):='1';
end if;
elsif Rx_Start(6)='1' then
Baud_Number1:=Baud_Number1+1;
if Baud_Number1=100 then
Rx_Buffer(6) <= rx;
Baud_Number1:=0;
Rx_Start(6):='0';
Rx_Start(7):='1';
end if;
elsif Rx_Start(7)='1' then
Baud_Number1:=Baud_Number1+1;
if Baud_Number1=100 then
Rx_Buffer(7) <= rx;
Baud_Number1:=0;
Rx_Start(7):='0';
Rx_Over:='1';
end if;
elsif Rx_Over='1' then
Baud_Number1:=Baud_Number1+1;
if Baud_Number1=100 then
if rx='1'then
Baud_Number1:=0;
Rx_Over:='0';
Rx_Complete:='1';
end if;
end if;
elsif Rx_Complete='1' then
Tx_Buffer<=Rx_Buffer;
Rx_Complete:='0';
else
Rx_Start:="00000000";
Rx_Over:='0';
Rx_Complete:='0';
end if;
end if;
END PROCESS;
--send subprogrammer
PROCESS(clk)
variable Baud_Number:integer range 0 to 200000;
variable Tx_start: std_logic_vector(7 downto 0);
variable Tx_Over:std_logic;
variable Tx_Complete:std_logic;
BEGIN
if clk'event and clk='0' then
if rtx_control='1' then
if Tx_Start="00000000"and Tx_Over='0'and Tx_Complete='0'then
--Baud_Number:=Baud_Number+1;
--if Baud_Number=100 then
tx <= '0';
--Baud_Number:=0;
Tx_Start(0):='1';
--end if;
elsif Tx_Start(0)='1' then
Baud_Number:=Baud_Number+1;
if Baud_Number=104 then
tx<=Tx_Buffer(0);
-- tx<='1';
Baud_Number:=0;
Tx_Start(0):='0';
Tx_Start(1):='1';
end if;
elsif Tx_Start(1)='1' then
Baud_Number:=Baud_Number+1;
if Baud_Number=104 then
tx<=Tx_Buffer(1);
--tx<='1';
Baud_Number:=0;
Tx_Start(1):='0';
Tx_Start(2):='1';
end if;
elsif Tx_Start(2)='1' then
Baud_Number:=Baud_Number+1;
if Baud_Number=104 then
tx<=Tx_Buffer(2);
--tx<='0';
Baud_Number:=0;
Tx_Start(2):='0';
Tx_Start(3):='1';
end if;
elsif Tx_Start(3)='1' then
Baud_Number:=Baud_Number+1;
if Baud_Number=104 then
tx<=Tx_Buffer(3);
--tx<='1';
Baud_Number:=0;
Tx_Start(3):='0';
Tx_Start(4):='1';
end if;
elsif Tx_Start(4)='1' then
Baud_Number:=Baud_Number+1;
if Baud_Number=104 then
tx<=Tx_Buffer(4);
--tx<='1';
Baud_Number:=0;
Tx_Start(4):='0';
Tx_Start(5):='1';
end if;
elsif Tx_Start(5)='1' then
Baud_Number:=Baud_Number+1;
if Baud_Number=104 then
tx<=Tx_Buffer(5);
--tx<='0';
Baud_Number:=0;
Tx_Start(5):='0';
Tx_Start(6):='1';
end if;
elsif Tx_Start(6)='1' then
Baud_Number:=Baud_Number+1;
if Baud_Number=104 then
tx<=Tx_Buffer(6);
--tx<='0';
Baud_Number:=0;
Tx_Start(6):='0';
Tx_Start(7):='1';
end if;
elsif Tx_Start(7)='1' then
Baud_Number:=Baud_Number+1;
if Baud_Number=104 then
tx<=Tx_Buffer(7);
--tx<='1';
Baud_Number:=0;
Tx_Start(7):='0';
Tx_Over:='1';
end if;
elsif Tx_Over='1' then
Baud_Number:=Baud_Number+1;
if Baud_Number=104 then
tx<='1';
Baud_Number:=0;
Tx_Over:='0';
Tx_Complete:='1';
end if;
elsif Tx_Complete='1' then
Baud_Number:=Baud_Number+1;
if Baud_Number=200000 then
Baud_Number:=0;
Tx_Complete:='0';
end if;
else
Tx_Start:="00000000";
Tx_Over:='0';
Tx_Complete:='0';
end if;
end if;
end if;
END PROCESS;
END cpld;
文章评论(0条评论)
登录后参与讨论