【嵌入式开发】FPGA/CPLD控制串口(VHDL版)

简介: 作者:gnuhpc 出处:http://www.cnblogs.com/gnuhpc/ 采用自顶向下设计: top_rs232_port.vhd library ieee; use ieee.

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/

采用自顶向下设计:

top_rs232_port.vhd

library ieee;
use ieee.std_logic_1164.all;

entity rs232_port is
    port (    serialinput   : in  std_logic;
              clock         : in  std_logic;
              reset         : in  std_logic;
              readinput     : in  std_logic;
              sendoutput          : in  std_logic;
              serial_in        : in  std_logic_vector(7 downto 0);
              serialoutput  : out std_logic;
              getinput            : out std_logic;
              outputsent          : out std_logic;
              serial_out    : out std_logic_vector(7 downto 0));
     end rs232_port;

architecture behavioral of rs232_port is
signal fastclk    : std_logic;
signal slowclk    : std_logic;

component rs232_in
    port(data_out : out std_logic_vector(7 downto 0);
           reset    : in std_logic;
           clock    : in std_logic;
           getdata  : in std_logic;
           ready    : out std_logic;
           serialinput: in std_logic);
  end component;

component rs232_out
    port(data_in : in std_logic_vector(7 downto 0);
           reset   : in std_logic;
           clock   : in std_logic;
           load    : in std_logic;
           ready   : out std_logic;
           serialoutput : out std_logic);
  end component;

component clkdiv
   Generic (Divisor1: positive :=1311; -- clock for sending
            Divisor2: positive :=82 ); -- clock for receiving
      PORT(fast_clock : IN STD_LOGIC;
           reset      : IN STD_LOGIC;
           slow_clock1: out STD_LOGIC;
           slow_clock2: out STD_LOGIC);
END component;

begin
serin:     rs232_in
                port map(data_out      => serial_out,
                            reset         => reset,
                            clock         => fastclk,
                            getdata    => readinput,
                            ready         => getinput,
                            serialinput=> serialinput);

serout:     rs232_out
                port map(data_in        => serial_in,
                            reset          => reset,
                            clock          => slowclk,
                            load           => sendoutput,
                            ready          => outputsent,
                            serialoutput=> serialoutput);
clocks:        clkdiv
                port map(fast_clock     => clock,
                         reset             => reset,
                         slow_clock1 => slowclk,
                         slow_clock2 => fastclk);

end behavioral;

rs232_input.vhd

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;

entity rs232_in is
    port(data_out: out std_logic_vector(7 downto 0);
           reset   : in std_logic;
           clock   : in std_logic;
           getdata : in std_logic;
           ready   : out std_logic;
           serialinput : in std_logic);
  end rs232_in;

architecture behavioural of rs232_in is
signal   indata   : std_logic_vector(7 downto 0);   
constant countmax : natural := 153;
begin

process (clock)
variable count:natural range 0 to 170;
variable startbit : integer range 0 to 2 ;
    begin
        if (clock'event and clock='1') then
             if ( reset = '1' or getdata = '1')then
               indata <= (others => '0');
               ready  <= '0';
               count  := 0;
               data_out <= (others => '0');
          startbit := 0 ;
           elsif (serialinput = '0' and startbit = 0) then
                 startbit := 1;   
        elsif (serialinput = '1' and count  >= countmax) then
           startbit := 0;
           data_out <= indata;
                 count:= 0;
                 ready <= '1';
                 indata <= (others => '0');
           end if;      

          if (startbit = 1) then   
             count:= (count +1);   
                        case count is
                           when 24 =>
                               indata(0) <= serialinput;
                           when 40 =>
                               indata(1) <= serialinput;
                           when 56 =>
                               indata(2) <= serialinput;
                           when 72 =>
                               indata(3) <= serialinput;
                           when 88 =>
                               indata(4) <= serialinput;
                           when 104 =>
                               indata(5) <= serialinput;
                           when 120 =>
                               indata(6) <= serialinput;
                           when 136 =>
                               indata(7) <= serialinput;
                           when others =>
                               count:= count;
                         end case;
            end if;
end if;
end process ;
end behavioural;

rs232_output.vhd

library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;

entity rs232_out is
    port(data_in : in std_logic_vector(7 downto 0);
           reset   : in std_logic;
           clock   : in std_logic;
           load    : in std_logic;
           ready   : out std_logic;
           serialoutput : out std_logic);
end rs232_out;

architecture behavioural of rs232_out is
signal frame : std_logic_vector(10 downto 0);
signal internaldata : std_logic_vector(7 downto 0);
signal ready_s : std_logic;   
begin
  frame(0) <= '1';
    frame(1) <= '0';
    frame(9 downto 2) <= internaldata;
    frame(10)<= '1';
    ready    <= ready_s;
process (clock)
variable count:natural range 0 to 10;
begin
    if (clock'event and clock ='1') then
        if (reset = '1') then
            internaldata <= (others => '0');
            ready_s <= '1';
            count   := 0;
            serialoutput <= frame(0);
        elsif load = '1' then
            ready_s <= '0';
            internaldata <= data_in;
            serialoutput <= frame(0);
            count:= 0;
        elsif ready_s = '1' then
            serialoutput <= frame(0);
        elsif count = 10 then
            ready_s <= '1';
            serialoutput <= frame(count);
            count:= 0;
        else
            count:= (count +1);
            serialoutput <= frame(count);
        end if;       
    end if;
end process ;
end behavioural;

clockkdiv.vhd

LIBRARY ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_arith.all;
use ieee.std_logic_unsigned.all;

ENTITY clkdiv IS
   Generic (Divisor1: positive :=1311; -- clock for sending
            Divisor2: positive :=82 ); -- clock for receiving
   PORT(fast_clock : IN  STD_LOGIC;
        reset      : IN  STD_LOGIC;
        slow_clock1: out STD_LOGIC;
        slow_clock2: out STD_LOGIC);
END clkdiv;

ARCHITECTURE behaviour OF clkdiv IS
signal  slow_clock1_s : std_logic;
signal  slow_clock2_s : std_logic;
BEGIN
slow_clock1 <= slow_clock1_s;
slow_clock2 <= slow_clock2_s;
process (fast_clock)
   variable c1:natural range 0 to Divisor1;
   variable c2:natural range 0 to Divisor2;
BEGIN
if( fast_clock'EVENT and fast_clock = '1') then
   if (reset = '1') then
           c1:= 0;
      c2:= 0;
      slow_clock1_s <='0' ; 
         slow_clock2_s <= '0'; 
    else                                
         c1 := (c1 + 1);
      c2 := (c2 + 1);
         IF (c1=Divisor1) THEN
            slow_clock1_s <= not slow_clock1_s;
            c1 := 0;
         elsif (c2 = Divisor2) then
            slow_clock2_s <= not slow_clock2_s;
            c2:= 0;
         END IF;
     end if;  
end if;
END PROCESS ;
END behaviour;

 

作者:gnuhpc
出处:http://www.cnblogs.com/gnuhpc/


               作者:gnuhpc
               出处:http://www.cnblogs.com/gnuhpc/
               除非另有声明,本网站采用知识共享“署名 2.5 中国大陆”许可协议授权。


分享到:

目录
相关文章
|
数据格式 异构计算
FPGA 串口通信
FPGA 串口通信
|
算法 异构计算
基于FPGA的Lorenz混沌系统verilog开发,含testbench和matlab辅助测试程序
基于FPGA的Lorenz混沌系统verilog开发,含testbench和matlab辅助测试程序
|
6月前
|
数据采集 算法 测试技术
【硬件测试】基于FPGA的1024QAM基带通信系统开发与硬件片内测试,包含信道模块,误码统计模块,可设置SNR
本文介绍了基于FPGA的1024QAM基带通信系统的硬件测试版本,包含testbench、高斯信道模块和误码率统计模块。系统新增ila在线数据采集和vio在线SNR设置模块,支持不同SNR条件下的性能测试。1024QAM调制将10比特映射到复平面上的1024个星座点之一,实现高效数据传输。硬件测试结果表明,在SNR=32dB和40dB时,系统表现出良好的性能。Verilog核心程序展示了各模块的连接与功能实现。
145 7
|
6月前
|
算法 数据安全/隐私保护 异构计算
基于FPGA的2ASK+帧同步系统verilog开发,包含testbench,高斯信道,误码统计,可设置SNR
本内容展示了基于Vivado2019.2的算法仿真效果,包括设置不同信噪比(SNR=8db和20db)下的结果及整体波形。同时,详细介绍了2ASK调制解调技术的原理与实现,即通过改变载波振幅传输二进制信号,并提供数学公式支持。此外,还涉及帧同步理论,用于确定数据帧起始位置。最后,给出了Verilog核心程序代码,实现了2ASK解调与帧同步功能,结合DDS模块生成载波信号,完成信号处理流程。
122 0
|
6月前
|
数据采集 算法 数据安全/隐私保护
【硬件测试】基于FPGA的4ASK调制解调通信系统开发与硬件片内测试,包含信道模块,误码统计模块,可设置SNR
本文介绍了基于FPGA的4ASK调制解调系统的硬件测试版本,该系统包括testbench、高斯信道模块和误码率统计模块,并新增了ILA在线数据采集和VIO在线SNR设置功能。通过VIO设置不同SNR(如15dB和25dB),实现了对系统性能的实时监测与调整。4ASK是一种通过改变载波幅度表示数据的数字调制方式,适用于多种通信场景。FPGA平台的高效性和灵活性使其成为构建高性能通信系统的理想选择。
153 17
|
6月前
|
数据采集 算法 数据安全/隐私保护
【硬件测试】基于FPGA的4FSK调制解调通信系统开发与硬件片内测试,包含信道模块,误码统计模块,可设置SNR
本文基于之前的文章《基于FPGA的4FSK调制解调系统》,增加了ILA在线数据采集模块和VIO在线SNR设置模块,实现了硬件测试版本。通过VIO设置不同SNR(如10dB和20dB),并展示了ILA采集的数据结果。四频移键控(4FSK)是一种数字调制方法,利用四个不同频率传输二进制数据,具有较高的频带利用率和抗干扰性能。输入的二进制数据分为两组,每组两个比特,对应四个频率f1、f2、f3、f4,分别代表二进制组合00、01、10、11。调制过程中选择相应频率输出,并进行幅度调制以增强抗干扰能力。接收端通过带通滤波器提取信号并还原为原始二进制数据。
128 7
|
6月前
|
数据采集 算法 数据处理
【硬件测试】基于FPGA的256QAM基带通信系统开发与硬件片内测试,包含信道模块,误码统计模块,可设置SNR
本文介绍了基于FPGA的256QAM基带通信系统的硬件测试版本,包含testbench、高斯信道模块和误码率统计模块。系统新增ila在线数据采集和vio在线SNR设置模块,支持不同信噪比(如30dB和40dB)的仿真测试,并提供配套操作视频。256QAM调制方案每个符号携带8比特信息,通过复数值星座图映射实现高效传输。Verilog代码展示了核心模块设计,包括SNR设置、数据处理和ILA测试分析,确保系统在实际硬件环境中的稳定性和性能。
126 2
|
7月前
|
数据采集 算法 数据安全/隐私保护
【硬件测试】基于FPGA的16QAM基带通信系统开发与硬件片内测试,包含信道模块,误码统计模块,可设置SNR
本文介绍了基于FPGA的16QAM基带通信系统硬件测试版本。该系统在仿真基础上增加了ILA在线数据采集和VIO在线SNR设置模块,支持不同信噪比(如15dB、25dB)的测试。16QAM是一种正交幅度调制方式,通过两路4ASK信号叠加实现,每个符号包含4比特信息。系统采用正交调幅法生成16QAM信号,并通过DAC转换为模拟信号。解调时使用正交相干解调,经低通滤波器恢复电平信号。开发板内完成发射与接收,无需定时同步模块。代码可移植至其他开发板,具体步骤见配套文档。
131 2
|
算法 芯片 异构计算
通过FPGA实现基于RS232串口的指令发送并控制显示器中目标位置
通过FPGA实现基于RS232串口的指令发送并控制显示器中目标位置
|
7月前
|
数据采集 算法 数据安全/隐私保护
【硬件测试】基于FPGA的64QAM基带通信系统开发与硬件片内测试,包含信道模块,误码统计模块,可设置SNR
本文介绍了基于FPGA的64QAM基带通信系统的硬件测试版本,包含testbench、高斯信道模块和误码率统计模块。系统新增ila在线数据采集模块和vio在线SNR设置模块,支持不同SNR条件下的仿真与测试。通过设置SNR为25dB和30dB进行测试,验证了系统的可行性和性能。此外,本文详细阐述了64QAM调制解调的工作原理,包括信号生成、调制、解调及误码率测试等环节,并提供了Verilog核心程序代码。
109 0