- 4
- 0
- 约4.17千字
- 约 6页
- 2017-09-06 发布于重庆
- 举报
1、基本知识:1、-- 异步串行通信数据帧格式如下:起始位为 ‘0’,停止位为1-2位‘1’;奇偶校验位为可选 ,数据为5-8位可选。2、波特率指的是每秒钟发送的字符(一位数据位)的个数,1B=1b/s。如波特率为9600B,那么发送一个数据位所需要的时间为1/9600 s; 发送/接收时钟的频率与波特率的关系为: 发送/接收时钟的频率 = n x发送/接收波特率。其中n = 1 、16、64;我们在这里取n = 16 ;即对每一位数据都采样16次。2、功能实现-- 本模块的功能是验证实现和PC机进行基本的串口通信的功能。需要在PC机上安装一个串口调试工具来验证程序的功能。程序实现了一个收发一帧10个bit(即无奇偶校验位)的串口控制器,10个bit是1位起始位,8个数据位,1个结束位。程序分为三个模块:时钟分频模块,接收数据模块,发送数据模块。具体程序代码为:library IEEE;use IEEE.STD_LOGIC_1164.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 is port( CLK50M : in std_logic; reset : in std_logic; uart_tx : out std_logic; uart_rx : in std_logic );end uart;architecture Behavioral of uart is signal div_clk50M :std_logic; ----分频后的时钟信号signal rx_data : std_logic_vector(7 downto 0); --- 接收数据缓存signal tx_data : std_logic_vector(7 downto 0); ----发送数据缓存signal data_buf: std_logic_vector(7 downto 0); --- 接收数据存储type state_rx is (rx_idle,rx_start,rx_sample,rx_stop); ---接收状态机signal rx_state : state_rx := rx_idle; type state_tx is (tx_idle,tx_start,tx_send,tx_stop); --发送状态机signal tx_state : state_tx := tx_idle;signal send_enable : std_logic :=0;begin--- --------CLK_Div module---------------------Bauds = 9600B clk=50M division factor = 50M/(9600x16)=325--------------------------------------------- CLK_DIV: process(CLK50M,reset) variable div_count : integer :=0; -----分频系数变量 begin if(reset=1) then div_clk50M =0; div_count :=0; elsif(rising_edge(CLK50M)) then if div_count=325 then div_clk50M = 0; div_count :=0; elsif div_count=162 then div_count := div_count + 1; div_clk50M = 1; else div_count :=0; div_clk50M = 0;end if; end if; end process CLK_DIV; -----------------------------------------------------------DATA Receive module------------DATA_R : process(div_c
原创力文档

文档评论(0)