如何使用VHDL中的时钟分频器校正相移?

时间:2018-10-27 01:20:51

标签: vhdl frequency clock uart phase

我想制作一个UART接收器,它读取8个连续的位,并在末尾带有奇偶校验位和一个简单的停止位。我的FPGA的时钟为100Mhz,传输到uart的数据的波特率为56700。分频系数为1736(56700 * 1736≈100Mhz)。这两个输出是uart解码的输入消息和错误信号,指示uart是否已正确读取输入。这就是我所拥有的:

    library ieee;
use ieee.std_logic_1164.ALL;
use ieee.numeric_std.all;

entity uart_receiver is
  generic (
    clksPerBit : integer := 1736     -- Needs to be set correctly
    );
  port (
    clk       : in  std_logic;
    clk_en_uart : in std_logic ;
    reset     : in std_logic;
    uart_rx : in  std_logic;
    error     : out std_logic;
    char   : out std_logic_vector(7 downto 0)
    );
end uart_receiver;


architecture uart_receiver_arch of uart_receiver is

  type etat is (init, start_bit, receiving_bits, parity_bit,
                     stop_bit );
  signal current_state : etat := init ;
  signal error_signal : std_logic := '0';
  signal clk_count : integer range 0 to clksPerBit-1 := 0;
  signal bit_index : integer range 0 to 7 := 0;  -- 8 Bits Total
  signal data_byte   : std_logic_vector(7 downto 0) := (others => '0');


begin

process (clk_en_uart)
  begin
    if rising_edge(clk_en_uart) then

    end if;
  end process;


process (clk,reset)
variable  check_parity : integer range 0 to 7 := 0;
   begin
     if (reset = '1') then 
             current_state <= init;
             error_signal <= '0';
             clk_count <= 0;
             bit_index <= 0;

             data_byte <= (others => '0');
     elsif rising_edge(clk) then
       case current_state  is
         when init =>

           clk_count <= 0;
           Bit_Index <= 0;

           if uart_rx = '0' then       -- Start bit detected
             current_state <= start_bit;
           else
             current_state <= init;
           end if;

          when start_bit =>
            if clk_count = (clksPerBit-1)/2 then
                 if uart_rx = '0' then
                   clk_count <= 0;  -- reset counter since we found the middle
                   current_state   <= receiving_bits;
                 else
                   current_state   <= init;
                 end if;
            else
                 clk_count <= clk_count + 1;
                 current_state <= start_bit;
               end if;               

         when receiving_bits =>
                  if clk_count < clksPerBit-1 then
                    clk_count <= clk_count + 1;
                    current_state   <= receiving_bits;
                  else
                    clk_count <= 0;
                    data_byte(bit_index) <= uart_rx;  
                 if bit_index < 7 then
                  bit_index <= bit_index + 1;
                  current_state   <= receiving_bits ;
                else
                  bit_index <= 0;
                  current_state <= parity_bit;
                end if;
              end if;

          when parity_bit =>
                if clk_count < clksPerBit-1 then
                  clk_count <= clk_count + 1;
                  current_state   <= parity_bit;
                else 
                   for k in 0 to 7 loop
                      if ( data_byte(k) = '1' ) then
                         check_parity := check_parity + 1 ;
                      end if;
                      end loop; 
                      if((uart_rx  = '1' and check_parity mod 2 = 0) or (uart_rx = '0' and check_parity mod 2 = 1)) then 
                            error_signal  <= '1' ;
                      else 
                            error_signal  <= '0';
                      end if ;
                  current_state <= stop_bit;
                end if;

               when stop_bit =>
                    if clk_count < clksPerBit-1 then
                      clk_count <= clk_count + 1;
                      current_state   <= stop_bit ;
                    else
                      clk_count <= 0;
                      current_state  <= init;
                    end if;

                when others => 
                     current_state <= init;
               end case;
         end if; 
     char <= data_byte ;    
     error <= error_signal ;  
     end process;                    
 end uart_receiver_arch;

因此,传输到uart的数据和他的时钟之间存在相移。如果有相移,我不会在正确的时间读取数据。我认为这段代码足以解决此问题。但是,我创建了一个clock_divider,但似乎找不到在此代码中使用它的方法。这是我的时钟分频器:

library IEEE;
use IEEE.STD_LOGIC_1164.ALL;

entity clock_divider is
    generic (divfactor : positive := 1736);
    Port (clk,clk2, reset : in STD_LOGIC ;
          clkdiv, activationsig : out STD_LOGIC );
end clock_divider;

architecture clock_divider_arch of clock_divider is

begin
    process(clk,reset)
    variable  clksigv : std_logic := '0' ;
    variable  activationsigv : std_logic := '0' ;
    variable  count : integer := 0 ;
        begin
            if (reset = '1') then 
              clksigv := '0' ;
              activationsigv := '0' ;
              count := 0 ;
            elsif ( rising_edge(clk) ) then 
                count := count + 2 ;
                if (activationsigv = '1') then
                    activationsigv := '0';
                end if;
                if ( count >= divfactor - 1 ) then 
                    clksigv := not(clksigv) ;
                     if ( clksigv = '1' ) then 
                        activationsigv := '1' ;
                      end if;
                    count := 0 ;
                end if ;

            end if ;
            clkdiv <= clksigv ;
            activationsig <= activationsigv;
        end process ;           
end clock_divider_arch;

此时钟分频器的输出是时钟分频和激活信号,当它为'1'时,我必须读取uart中的数据。因此,两个输出也应该是uart的输入。在uart_recevier中,clk_en_uart实际上是时钟分频的,但是我不使用它,因为我不知道如何操作。

我认为解决方案是在进入start_bit情况时“激活”该分频的时钟,以使我有两个具有相同相位和相同频率的时钟,但我也认为无法为一个钟。

我不确定我是否已经解决了我的问题。如果您在我的代码或解释中看不到某些内容,请随时提出问题。

感谢您的帮助,希望我能找到解决方案。

1 个答案:

答案 0 :(得分:0)

对于此问题,建议的解决方案之类的声音很复杂。

通常的方法是,接收器只寻找起始位的下降沿,然后计数半个时间(在您的情况下为1736/2个周期),然后在此处采样起始位值,然后对每个完整位时间后的数据,奇偶校验和停止位值(在您的情况下为1736个周期)。之后,开始寻找起始位的新下降沿。

于是,发射器和接收器之间的频率差(通常)很小,以至于采样率实际上只在相对较低比特率的11位消息的中间,并且计数器在起始位的下降沿重启,从而确保了消除了长时间频率差异的任何影响。