在NESYS3上实现UART(VHDL)

    最近在做用FPGA读取ADC的数据,但是不知道读取的数据是否正确,想到了在单片机上经常用UART与上位机进行通信,于是便在FPGA上用VHDL写了一个简单的UART的模块,这样就可以在电脑上查看读取到的ADC数据了!

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_UNSIGNED.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;

-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;

entity UART_MODULE is
    Port ( clk : in  STD_LOGIC;
			  send : in boolean;
           data : in  STD_LOGIC_VECTOR (7 downto 0);
           tx : out  STD_LOGIC;
           busy : out  STD_LOGIC);
end UART_MODULE;

architecture Behavioral of UART_MODULE is

TYPE state_type IS(Idel, Rdy, LoadByte, SendBit);
signal state : state_type;
signal BspClk : STD_LOGIC;
signal BspClkReg : INTEGER RANGE 0 TO 511;
signal tx_data : STD_LOGIC_VECTOR(9 DOWNTO 0);
signal tx_byte_count : INTEGER RANGE 0 TO 15;

begin
	PROCESS(clk)
	BEGIN
		if (RISING_EDGE(clk)) then
			BspClkReg <= BspClkReg + 1;
			if(BspClkReg = 434) then
				BspClkReg <= 0;
				BspClk <= NOT BspClk;
			end if;
		end if;
	END PROCESS;
	
	PROCESS(BspClk)
	BEGIN
		if (RISING_EDGE(BspClk)) then
			CASE state IS
				WHEN Idel => tx <= '1';
								 busy <= '0';
								 tx_byte_count <= tx_byte_count + 1;
								 if(NOT send) then
										state <= Rdy;
								 end if;
				WHEN Rdy => tx_byte_count <= 0;
								tx <= '1';
								busy <= '1';
								state <= LoadByte;
				WHEN LoadByte => tx_data <= '1' & data & '0';
									  tx <= '1';
									  busy <= '1';
									  state <= SendBit;
				WHEN SendBit => tx <= tx_data(0);
									 busy <= '1';
									 tx_data <= TO_STDLOGICVECTOR(TO_BITVECTOR(tx_data) SRL 1);
									 tx_byte_count <= tx_byte_count + 1;
									 if(tx_byte_count = 9) then
										state <= Idel;
									 else
										state <= SendBit;
									 end if;
				WHEN OTHERS => NULL;
			END CASE;			 
							
		end if;
	END PROCESS;


end Behavioral;


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值