串口通信--VHDL

fds




 ----------------UART的异步串口通信协议的VHDL语言实现 ----------------
 --异步串行通信的采用的波特率为9600b/s,外配晶体振荡器的频率为50MHz,故还要采取分频电路
 
 
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity uart is
    port(clk : in  std_logic;                        --系统时钟
       rst_n: in std_logic;                        --复位信号 
   rs232_rx: in std_logic;                     --RS232接收数据信号;
   rs232_tx: out std_logic                     --RS232发送数据信号;
   );
end uart;

architecture behav of uart is

 
   component uart_rx port(clk : in  std_logic;                   --系统时钟
       rst_n: in std_logic;                        --复位信号 
       rs232_rx: in std_logic;                     --RS232接收数据信号
       clk_bps: in std_logic;                      --此时clk_bps的高电平为接收数据的采样点
       bps_start:out std_logic;  --接收到数据后,波特率时钟启动置位
       rx_data: out std_logic_vector(7 downto 0);  --接收数据寄存器,保存直至下一个数据来到
       rx_int: out std_logic                       --接收数据中断信号,接收数据期间时钟为高电平,传送给串口发送
               );
 end component;
 
 component speed_select port(clk : in  std_logic;               --系统时钟
        rst_n: in std_logic;                      --复位信号
        clk_bps: out std_logic;                   --此时clk_bps的高电平为接收或者发送数据位的中间采样点
        bps_start:in std_logic   --接收数据后,波特率时钟启动信号置位
          );
 end component;
 
 component uart_tx port(clk : in  std_logic;                    --系统时钟
       rst_n: in std_logic;                         --复位信号
       rs232_tx: out std_logic;                     --RS232接收数据信号
       clk_bps: in std_logic;                       --此时clk_bps的高电平为接收数据的采样点
       bps_start:out std_logic;   --接收到数据后,波特率时钟启动置位
       rx_data: in std_logic_vector(7 downto 0);    --接收数据寄存器,保存直至下一个数据来到
       rx_int: in std_logic                         --接收数据中断信号,接收数据期间时钟为高电平,传送给串口发送
                        --模块,使得串口正在进行接收数据的时候,发送模块不工作,避免
                        --了一个完整的数据(1位起始位、8位数据位、1位停止位)还没有
                        --接收完全时,发送模块就已经将不正确的数据传输出去
       );
 end component;

 signal bps_start_1:std_logic;
 signal bps_start_2:std_logic;
 signal clk_bps_1:std_logic;
 signal clk_bps_2:std_logic;
 signal rx_data:std_logic_vector(7 downto 0);
 signal rx_int:std_logic;
 
 
 begin
 RX_TOP: uart_rx port map(clk=>clk,
        rst_n=>rst_n,
        rs232_rx=>rs232_rx,
        clk_bps=>clk_bps_1,
        bps_start=>bps_start_1,
        rx_data=>rx_data,
        rx_int=>rx_int
        );
       
   SPEED_TOP_RX: speed_select port map(clk=>clk,
            rst_n=>rst_n,
            clk_bps=>clk_bps_1,
            bps_start=>bps_start_1
            );
           
 TX_TOP:uart_tx port map(clk=>clk,                             --系统时钟
       rst_n=>rst_n,                               --复位信号 
       rs232_tx=>rs232_tx,                         --RS232发送数据信号
       clk_bps=>clk_bps_2,                         --此时clk_bps的高电平为发送数据的采样点
       bps_start=>bps_start_2,     --接收到数据后,波特率时钟启动置位
       rx_data=>rx_data,                           --接收数据寄存器,保存直至下一个数据来到
       rx_int=>rx_int                              --接收数据中断信号,接收数据期间时钟为高电平,传送给串口发送
                       --模块,使得串口正在进行接收数据的时候,发送模块不工作,避免
       
         );
   SPEED_TOP_TX: speed_select port map(clk=>clk,
            rst_n=>rst_n,
            clk_bps=>clk_bps_2,
            bps_start=>bps_start_2
            );
           
end behav;


--------------------------------------------------------------------------------------
---------------------------------3个子模块---------------------------------------------







---------------------------------异步接收模块-------------------------------------------    

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity uart_rx is
    port(clk : in  std_logic;                        --系统时钟
       rst_n: in std_logic;                        --复位信号 
   rs232_rx: in std_logic;                     --RS232接收数据信号
   clk_bps: in std_logic;                      --此时clk_bps的高电平为接收数据的采样点
   bps_start:out std_logic;  --接收到数据后,波特率时钟启动置位
   rx_data: out std_logic_vector(7 downto 0);  --接收数据寄存器,保存直至下一个数据来到
       rx_int: out std_logic                       --接收数据中断信号,接收数据期间时钟为高电平,传送给串口发送
                                               --模块,使得串口正在进行接收数据的时候,发送模块不工作,避免
                   --了一个完整的数据(1位起始位、8位数据位、1位停止位)还没有
                   --接收完全时,发送模块就已经将不正确的数据传输出去
   );
end uart_rx;
architecture behav of uart_rx is
         signal    rs232_rx0: std_logic;
         signal    rs232_rx1: std_logic;
         signal    rs232_rx2: std_logic;
         signal    rs232_rx3: std_logic;
   signal    neg_rs232_rx:std_logic;
         signal    bps_start_r:std_logic;
   signal    num:integer;
         signal    rx_data_r:std_logic_vector(7 downto 0);  --串口接收数据寄存器,保存直至下一个数据到来
begin
 
 process(clk,rst_n)
     begin
         if (rst_n='0')then
    rs232_rx0<='0';
    rs232_rx1<='0';
    rs232_rx2<='0';
    rs232_rx3<='0';
   else
    if (rising_edge(clk)) then
     rs232_rx0<=rs232_rx;
     rs232_rx1<=rs232_rx0;
     rs232_rx2<=rs232_rx1;
     rs232_rx3<=rs232_rx2;
    end if;
   end if;
   neg_rs232_rx <=rs232_rx3 and rs232_rx2 and not(rs232_rx1)and not(rs232_rx0);
   end process;
 

   process(clk,rst_n)
     begin
         if (rst_n='0')then
    bps_start_r<='0';
    rx_int<='0';
   else
    if (rising_edge(clk)) then
         if(neg_rs232_rx='1') then    --接收到串口数据线rs232_rx的下降沿标志信号
     bps_start_r<='1';        --启动串口准备数据接收
     rx_int<='1';    --接收数据中断信号使能
     else if((num= 15) and (clk_bps='1')) then --接收完有用数据信息
         bps_start_r<='0';  --数据接收完毕,释放波特率启动信号
       rx_int<='0'; --接收数据中断信号关闭
       end if;
     end if;
       end if;
    end if;
  bps_start<=bps_start_r;
   end process;
 
 
   process(clk,rst_n)
     begin
         if (rst_n='0')then
    rx_data_r<="00000000";
    rx_data<="00000000";
    num<=0;
   else
    if (rising_edge(clk)) then
     if(clk_bps='1')then
       num<=num+1;
       case num is
        when  1=>rx_data_r(0)<=rs232_rx;--锁存第0bit
        when  2=>rx_data_r(1)<=rs232_rx;--锁存第0bit
        when  3=>rx_data_r(2)<=rs232_rx;--锁存第0bit
        when  4=>rx_data_r(3)<=rs232_rx;--锁存第0bit
        when  5=>rx_data_r(4)<=rs232_rx;--锁存第0bit
        when  6=>rx_data_r(5)<=rs232_rx;--锁存第0bit
        when  7=>rx_data_r(6)<=rs232_rx;--锁存第0bit
        when  8=>rx_data_r(7)<=rs232_rx;--锁存第0bit
        when  10=>rx_data<=rx_data_r;
        when  11=>num<=15;
        when  others=>null;
       end case;
       if(num=15) then
        num<=0;
       end if;
      end if;
     end if;
   end if;
  end process;
 
 end behav;
   

   
   
   
---------------------------------波特率控制模块-----------------------------------------  

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity speed_select is
    port(clk : in  std_logic;                        --系统时钟
       rst_n: in std_logic;                        --复位信号 
   clk_bps: out std_logic;                     --此时clk_bps的高电平为接收或者发送数据位的中间采样点
   bps_start:in std_logic     --接收数据后,波特率时钟启动信号置位
                   --或者开始发送数据时,波特率时钟启动信号置位
   );
end speed_select;
architecture behav of speed_select is

signal cnt:std_logic_vector(12 downto 0);
signal clk_bps_r:std_logic;
constant BPS_PARA:integer:=5207;
constant BPS_PARA_2:integer:=2603;

begin

   process(clk,rst_n)
     begin
         if (rst_n='0')then
    cnt<="0000000000000";
   else
    if (rising_edge(clk)) then
     if((cnt=BPS_PARA)or(bps_start='0')) then
       cnt<="0000000000000";   --波特率计数器清零
     else
      cnt<=cnt+'1'; --波特率时钟计数启动
     end if;
    end if;
   end if;
 end process;
 
 process(clk,rst_n)
     begin
         if (rst_n='0')then
    clk_bps_r<='0';
   else  
    if (rising_edge(clk)) then
     if(cnt=BPS_PARA_2) then
        clk_bps_r<='1';   --clk_bps_r高电平为接收数据位的中间采样点,同时也作为发送数据的数据改变点
     else
      clk_bps_r<='0';   --波特率计数器清零
     end if;
    end if;
   end if;
  clk_bps<=clk_bps_r;
 end process;
end behav;




---------------------------------异步发送模块  -------------------------------------------  
 

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity uart_tx is
    port(clk : in  std_logic;                        --系统时钟
       rst_n: in std_logic;                        --复位信号 
   rs232_tx: out std_logic;                     --RS232接收数据信号
   clk_bps: in std_logic;                      --此时clk_bps的高电平为接收数据的采样点
   bps_start:out std_logic;  --接收到数据后,波特率时钟启动置位
   rx_data: in std_logic_vector(7 downto 0);  --接收数据寄存器,保存直至下一个数据来到
       rx_int: in std_logic                       --接收数据中断信号,接收数据期间时钟为高电平,传送给串口发送
                                               --模块,使得串口正在进行接收数据的时候,发送模块不工作,避免
                   --了一个完整的数据(1位起始位、8位数据位、1位停止位)还没有
                   --接收完全时,发送模块就已经将不正确的数据传输出去
   );
end uart_tx;
architecture behav of uart_tx is
         signal    rx_int0: std_logic;
         signal    rx_int1: std_logic;
         signal    rx_int2: std_logic;
   signal    neg_rx_int:std_logic;
         signal    bps_start_r:std_logic;
   signal    num:integer;
         signal    tx_data:std_logic_vector(7 downto 0);  --串口接收数据寄存器,保存直至下一个数据到来
begin
 
 process(clk,rst_n)
     begin
         if (rst_n='0')then
    rx_int0<='0';
    rx_int1<='0';
    rx_int2<='0';
   else
    if (rising_edge(clk)) then
     rx_int0<=rx_int;
     rx_int1<=rx_int0;
     rx_int2<=rx_int1;
    end if;
   end if;
   neg_rx_int <=not(rx_int1)and (rx_int2);
   end process;
 

   process(clk,rst_n)
     begin
         if (rst_n='0')then
    bps_start_r<='0';
    tx_data<="00000000";
   else
    if (rising_edge(clk)) then
         if(neg_rx_int='1') then    --接收到串口数据线rs232_rx的下降沿标志信号
     bps_start_r<='1';        --启动串口准备数据接收
     tx_data<=rx_data;    --接收数据中断信号使能
     else if((num= 15) and (clk_bps='1')) then --接收完有用数据信息
         bps_start_r<='0';  --数据接收完毕,释放波特率启动信号
       end if;
     end if;
       end if;
    end if;
  bps_start<=bps_start_r;
   end process;
 
 
   process(clk,rst_n)
     begin
         if (rst_n='0')then
    rs232_tx<='1';
    num<=0;
   else
    if (rising_edge(clk)) then
     if(clk_bps='1')then
       num<=num+1;
       case num is
        when  1=>rs232_tx<='0';
        when  2=>rs232_tx<=tx_data(0);--发送第1bit
        when  3=>rs232_tx<=tx_data(1);--发送第2bit
        when  4=>rs232_tx<=tx_data(2);--发送第3bit
        when  5=>rs232_tx<=tx_data(3);--发送第4bit
        when  6=>rs232_tx<=tx_data(4);--发送第5bit
        when  7=>rs232_tx<=tx_data(5);--发送第6bit
        when  8=>rs232_tx<=tx_data(6);--发送第7bit
        when  9=>rs232_tx<=tx_data(7);--发送第8bit
        when  10=>rs232_tx<='1';
        when  11=>num<=15;
        when  others=>null;
       end case;
       if(num=15) then
        num<=0;
       end if;
      end if;
     end if;
   end if;
  end process;
 
 end behav; 



 ----------------UART的异步串口通信协议的VHDL语言实现 ----------------
 --异步串行通信的采用的波特率为9600b/s,外配晶体振荡器的频率为50MHz,故还要采取分频电路
 
 
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity uart is
    port(clk : in  std_logic;                        --系统时钟
       rst_n: in std_logic;                        --复位信号 
   rs232_rx: in std_logic;                     --RS232接收数据信号;
   rs232_tx: out std_logic                     --RS232发送数据信号;
   );
end uart;

architecture behav of uart is

 
   component uart_rx port(clk : in  std_logic;                   --系统时钟
       rst_n: in std_logic;                        --复位信号 
       rs232_rx: in std_logic;                     --RS232接收数据信号
       clk_bps: in std_logic;                      --此时clk_bps的高电平为接收数据的采样点
       bps_start:out std_logic;  --接收到数据后,波特率时钟启动置位
       rx_data: out std_logic_vector(7 downto 0);  --接收数据寄存器,保存直至下一个数据来到
       rx_int: out std_logic                       --接收数据中断信号,接收数据期间时钟为高电平,传送给串口发送
               );
 end component;
 
 component speed_select port(clk : in  std_logic;               --系统时钟
        rst_n: in std_logic;                      --复位信号
        clk_bps: out std_logic;                   --此时clk_bps的高电平为接收或者发送数据位的中间采样点
        bps_start:in std_logic   --接收数据后,波特率时钟启动信号置位
          );
 end component;
 
 component uart_tx port(clk : in  std_logic;                    --系统时钟
       rst_n: in std_logic;                         --复位信号
       rs232_tx: out std_logic;                     --RS232接收数据信号
       clk_bps: in std_logic;                       --此时clk_bps的高电平为接收数据的采样点
       bps_start:out std_logic;   --接收到数据后,波特率时钟启动置位
       rx_data: in std_logic_vector(7 downto 0);    --接收数据寄存器,保存直至下一个数据来到
       rx_int: in std_logic                         --接收数据中断信号,接收数据期间时钟为高电平,传送给串口发送
                        --模块,使得串口正在进行接收数据的时候,发送模块不工作,避免
                        --了一个完整的数据(1位起始位、8位数据位、1位停止位)还没有
                        --接收完全时,发送模块就已经将不正确的数据传输出去
       );
 end component;

 signal bps_start_1:std_logic;
 signal bps_start_2:std_logic;
 signal clk_bps_1:std_logic;
 signal clk_bps_2:std_logic;
 signal rx_data:std_logic_vector(7 downto 0);
 signal rx_int:std_logic;
 
 
 begin
 RX_TOP: uart_rx port map(clk=>clk,
        rst_n=>rst_n,
        rs232_rx=>rs232_rx,
        clk_bps=>clk_bps_1,
        bps_start=>bps_start_1,
        rx_data=>rx_data,
        rx_int=>rx_int
        );
       
   SPEED_TOP_RX: speed_select port map(clk=>clk,
            rst_n=>rst_n,
            clk_bps=>clk_bps_1,
            bps_start=>bps_start_1
            );
           
 TX_TOP:uart_tx port map(clk=>clk,                             --系统时钟
       rst_n=>rst_n,                               --复位信号 
       rs232_tx=>rs232_tx,                         --RS232发送数据信号
       clk_bps=>clk_bps_2,                         --此时clk_bps的高电平为发送数据的采样点
       bps_start=>bps_start_2,     --接收到数据后,波特率时钟启动置位
       rx_data=>rx_data,                           --接收数据寄存器,保存直至下一个数据来到
       rx_int=>rx_int                              --接收数据中断信号,接收数据期间时钟为高电平,传送给串口发送
                       --模块,使得串口正在进行接收数据的时候,发送模块不工作,避免
       
         );
   SPEED_TOP_TX: speed_select port map(clk=>clk,
            rst_n=>rst_n,
            clk_bps=>clk_bps_2,
            bps_start=>bps_start_2
            );
           
end behav;


--------------------------------------------------------------------------------------
---------------------------------3个子模块---------------------------------------------







---------------------------------异步接收模块-------------------------------------------    

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity uart_rx is
    port(clk : in  std_logic;                        --系统时钟
       rst_n: in std_logic;                        --复位信号 
   rs232_rx: in std_logic;                     --RS232接收数据信号
   clk_bps: in std_logic;                      --此时clk_bps的高电平为接收数据的采样点
   bps_start:out std_logic;  --接收到数据后,波特率时钟启动置位
   rx_data: out std_logic_vector(7 downto 0);  --接收数据寄存器,保存直至下一个数据来到
       rx_int: out std_logic                       --接收数据中断信号,接收数据期间时钟为高电平,传送给串口发送
                                               --模块,使得串口正在进行接收数据的时候,发送模块不工作,避免
                   --了一个完整的数据(1位起始位、8位数据位、1位停止位)还没有
                   --接收完全时,发送模块就已经将不正确的数据传输出去
   );
end uart_rx;
architecture behav of uart_rx is
         signal    rs232_rx0: std_logic;
         signal    rs232_rx1: std_logic;
         signal    rs232_rx2: std_logic;
         signal    rs232_rx3: std_logic;
   signal    neg_rs232_rx:std_logic;
         signal    bps_start_r:std_logic;
   signal    num:integer;
         signal    rx_data_r:std_logic_vector(7 downto 0);  --串口接收数据寄存器,保存直至下一个数据到来
begin
 
 process(clk,rst_n)
     begin
         if (rst_n='0')then
    rs232_rx0<='0';
    rs232_rx1<='0';
    rs232_rx2<='0';
    rs232_rx3<='0';
   else
    if (rising_edge(clk)) then
     rs232_rx0<=rs232_rx;
     rs232_rx1<=rs232_rx0;
     rs232_rx2<=rs232_rx1;
     rs232_rx3<=rs232_rx2;
    end if;
   end if;
   neg_rs232_rx <=rs232_rx3 and rs232_rx2 and not(rs232_rx1)and not(rs232_rx0);
   end process;
 

   process(clk,rst_n)
     begin
         if (rst_n='0')then
    bps_start_r<='0';
    rx_int<='0';
   else
    if (rising_edge(clk)) then
         if(neg_rs232_rx='1') then    --接收到串口数据线rs232_rx的下降沿标志信号
     bps_start_r<='1';        --启动串口准备数据接收
     rx_int<='1';    --接收数据中断信号使能
     else if((num= 15) and (clk_bps='1')) then --接收完有用数据信息
         bps_start_r<='0';  --数据接收完毕,释放波特率启动信号
       rx_int<='0'; --接收数据中断信号关闭
       end if;
     end if;
       end if;
    end if;
  bps_start<=bps_start_r;
   end process;
 
 
   process(clk,rst_n)
     begin
         if (rst_n='0')then
    rx_data_r<="00000000";
    rx_data<="00000000";
    num<=0;
   else
    if (rising_edge(clk)) then
     if(clk_bps='1')then
       num<=num+1;
       case num is
        when  1=>rx_data_r(0)<=rs232_rx;--锁存第0bit
        when  2=>rx_data_r(1)<=rs232_rx;--锁存第0bit
        when  3=>rx_data_r(2)<=rs232_rx;--锁存第0bit
        when  4=>rx_data_r(3)<=rs232_rx;--锁存第0bit
        when  5=>rx_data_r(4)<=rs232_rx;--锁存第0bit
        when  6=>rx_data_r(5)<=rs232_rx;--锁存第0bit
        when  7=>rx_data_r(6)<=rs232_rx;--锁存第0bit
        when  8=>rx_data_r(7)<=rs232_rx;--锁存第0bit
        when  10=>rx_data<=rx_data_r;
        when  11=>num<=15;
        when  others=>null;
       end case;
       if(num=15) then
        num<=0;
       end if;
      end if;
     end if;
   end if;
  end process;
 
 end behav;
   

   
   
   
---------------------------------波特率控制模块-----------------------------------------  

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;
entity speed_select is
    port(clk : in  std_logic;                        --系统时钟
       rst_n: in std_logic;                        --复位信号 
   clk_bps: out std_logic;                     --此时clk_bps的高电平为接收或者发送数据位的中间采样点
   bps_start:in std_logic     --接收数据后,波特率时钟启动信号置位
                   --或者开始发送数据时,波特率时钟启动信号置位
   );
end speed_select;
architecture behav of speed_select is

signal cnt:std_logic_vector(12 downto 0);
signal clk_bps_r:std_logic;
constant BPS_PARA:integer:=5207;
constant BPS_PARA_2:integer:=2603;

begin

   process(clk,rst_n)
     begin
         if (rst_n='0')then
    cnt<="0000000000000";
   else
    if (rising_edge(clk)) then
     if((cnt=BPS_PARA)or(bps_start='0')) then
       cnt<="0000000000000";   --波特率计数器清零
     else
      cnt<=cnt+'1'; --波特率时钟计数启动
     end if;
    end if;
   end if;
 end process;
 
 process(clk,rst_n)
     begin
         if (rst_n='0')then
    clk_bps_r<='0';
   else  
    if (rising_edge(clk)) then
     if(cnt=BPS_PARA_2) then
        clk_bps_r<='1';   --clk_bps_r高电平为接收数据位的中间采样点,同时也作为发送数据的数据改变点
     else
      clk_bps_r<='0';   --波特率计数器清零
     end if;
    end if;
   end if;
  clk_bps<=clk_bps_r;
 end process;
end behav;




---------------------------------异步发送模块  -------------------------------------------  
 

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
entity uart_tx is
    port(clk : in  std_logic;                        --系统时钟
       rst_n: in std_logic;                        --复位信号 
   rs232_tx: out std_logic;                     --RS232接收数据信号
   clk_bps: in std_logic;                      --此时clk_bps的高电平为接收数据的采样点
   bps_start:out std_logic;  --接收到数据后,波特率时钟启动置位
   rx_data: in std_logic_vector(7 downto 0);  --接收数据寄存器,保存直至下一个数据来到
       rx_int: in std_logic                       --接收数据中断信号,接收数据期间时钟为高电平,传送给串口发送
                                               --模块,使得串口正在进行接收数据的时候,发送模块不工作,避免
                   --了一个完整的数据(1位起始位、8位数据位、1位停止位)还没有
                   --接收完全时,发送模块就已经将不正确的数据传输出去
   );
end uart_tx;
architecture behav of uart_tx is
         signal    rx_int0: std_logic;
         signal    rx_int1: std_logic;
         signal    rx_int2: std_logic;
   signal    neg_rx_int:std_logic;
         signal    bps_start_r:std_logic;
   signal    num:integer;
         signal    tx_data:std_logic_vector(7 downto 0);  --串口接收数据寄存器,保存直至下一个数据到来
begin
 
 process(clk,rst_n)
     begin
         if (rst_n='0')then
    rx_int0<='0';
    rx_int1<='0';
    rx_int2<='0';
   else
    if (rising_edge(clk)) then
     rx_int0<=rx_int;
     rx_int1<=rx_int0;
     rx_int2<=rx_int1;
    end if;
   end if;
   neg_rx_int <=not(rx_int1)and (rx_int2);
   end process;
 

   process(clk,rst_n)
     begin
         if (rst_n='0')then
    bps_start_r<='0';
    tx_data<="00000000";
   else
    if (rising_edge(clk)) then
         if(neg_rx_int='1') then    --接收到串口数据线rs232_rx的下降沿标志信号
     bps_start_r<='1';        --启动串口准备数据接收
     tx_data<=rx_data;    --接收数据中断信号使能
     else if((num= 15) and (clk_bps='1')) then --接收完有用数据信息
         bps_start_r<='0';  --数据接收完毕,释放波特率启动信号
       end if;
     end if;
       end if;
    end if;
  bps_start<=bps_start_r;
   end process;
 
 
   process(clk,rst_n)
     begin
         if (rst_n='0')then
    rs232_tx<='1';
    num<=0;
   else
    if (rising_edge(clk)) then
     if(clk_bps='1')then
       num<=num+1;
       case num is
        when  1=>rs232_tx<='0';
        when  2=>rs232_tx<=tx_data(0);--发送第1bit
        when  3=>rs232_tx<=tx_data(1);--发送第2bit
        when  4=>rs232_tx<=tx_data(2);--发送第3bit
        when  5=>rs232_tx<=tx_data(3);--发送第4bit
        when  6=>rs232_tx<=tx_data(4);--发送第5bit
        when  7=>rs232_tx<=tx_data(5);--发送第6bit
        when  8=>rs232_tx<=tx_data(6);--发送第7bit
        when  9=>rs232_tx<=tx_data(7);--发送第8bit
        when  10=>rs232_tx<='1';
        when  11=>num<=15;
        when  others=>null;
       end case;
       if(num=15) then
        num<=0;
       end if;
      end if;
     end if;
   end if;
  end process;
 
 end behav; 
  • 7
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
要实现基于VHDL串口收发,首先需要了解串口通信的基本原理。串口通信是通过串行传输数据,即将数据按照位的顺序依次发送或接收。串口通信包含数据位、停止位和校验位等参数,需要在发送和接收端进行设置和匹配。 下面是一个基于VHDL串口收发的简单实现: ``` entity uart is generic( clk_frequency: integer := 50_000_000; --时钟频率 baud_rate: integer := 9600; --波特率 data_bits: integer := 8; --数据位数 parity: string := "none"; --奇偶校验方式 stop_bits: integer := 1 --停止位数 ); port( clk: in std_logic; --时钟 reset: in std_logic; --复位 tx: out std_logic; --发送端口 rx: in std_logic --接收端口 ); end uart; architecture Behavioral of uart is --内部信号 signal baud_tick: integer range 0 to clk_frequency/baud_rate-1 := 0; --波特率计数器 signal bit_count: integer range 0 to data_bits+stop_bits+1 := 0; --位计数器 signal data: std_logic_vector(data_bits-1 downto 0) := (others => '0'); --数据 signal parity_bit: std_logic := '0'; --奇偶校验位 signal stop_bit: std_logic := '1'; --停止位 signal tx_busy: std_logic := '0'; --发送忙标志 begin --波特率计数器 baud_tick <= baud_tick + 1 when baud_tick /= clk_frequency/baud_rate-1 else 0; --发送 tx <= '0' when tx_busy = '1' else stop_bit & data & parity_bit & stop_bit when bit_count = data_bits+stop_bits+1 else '1' when bit_count = data_bits+stop_bits else data(data_bits-1) when bit_count = 1 else parity_bit when bit_count = data_bits+1 else stop_bit when bit_count = data_bits+stop_bits+1 else '1'; --接收 process(clk) begin if rising_edge(clk) then if reset = '1' then bit_count <= 0; data <= (others => '0'); parity_bit <= '0'; stop_bit <= '1'; else if rx = '0' and bit_count = 0 then --接收到起始位 bit_count <= 1; elsif bit_count > 0 and bit_count <= data_bits then --接收数据位 data(bit_count-1) <= rx; bit_count <= bit_count + 1; elsif bit_count = data_bits+1 and parity /= "none" then --接收奇偶校验位 parity_bit <= rx; bit_count <= bit_count + 1; elsif bit_count = data_bits+1 and parity = "none" then --无奇偶校验 parity_bit <= '0'; bit_count <= bit_count + 1; elsif bit_count = data_bits+2 then --接收停止位 stop_bit <= rx; bit_count <= 0; end if; end if; end if; end process; --发送数据 process(clk) begin if rising_edge(clk) then if reset = '1' then tx_busy <= '0'; else if tx_busy = '1' then --发送忙 if bit_count = data_bits+stop_bits+1 then tx_busy <= '0'; end if; else if bit_count > 0 then --开始发送 tx_busy <= '1'; bit_count <= bit_count + 1; end if; end if; end if; end if; end process; end Behavioral; ``` 该实现中包含了发送和接收两个部分。发送部分包括波特率计数器、位计数器、数据、奇偶校验位、停止位和发送忙标志等信号。接收部分包括波特率计数器、位计数器、数据、奇偶校验位和停止位等信号。接收部分通过一个 process 进行处理,发送部分则通过另一个 process 处理。其中,tx_busy 标志表示发送忙,当发送忙时,将不会继续发送,直到发送完成。 需要注意的是,该实现中的波特率计数器是通过计算得到的,因此需要根据时钟频率和波特率设置相应的计数器值。此外,还需要根据实际需求设置数据位数、奇偶校验方式和停止位数等参数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值