• 已解决 73482 个问题
  • 已帮助 5993 位优秀工程师

fpga实现串口接收模块

無唁苡對 2018-10-30 浏览量:809
我用fpga写了一个串口接收模块,但是有时候接收时数据会发生错误,希望大家帮我看看代码,是哪儿的错误??? 
library ieee;
use ieee.std_logic_1164.all;
use ieee.std_logic_unsigned.all;
use ieee.std_logic_arith.all;
entity rxd is
    generic(data_bits:integer:=8);
    port(bclk_inrxd_in:in std_logic;
			rx_done:out std_logic;
         rx_buffer:out std_logic_vector(7 downto 0));
end rxd;
architecture beheavior of rxd is
type states is(r_startr_centerr_waitr_sampler_stop);
signal state:states:=r_start;
signal rxd_sync:std_logic;
signal rxd_fall:std_logic;
begin
    process(bclk_in)
    begin
	 if bclk_in'event and bclk_in='1' then
        rxd_sync<=rxd_in;
		  if(rxd_sync='1' and rxd_in='0')then
			rxd_fall<='1';
		else
			rxd_fall<='0';
		end if;
	 end if;
    end process;
    process(bclk_inrxd_in)
    variable count:std_logic_vector(3 downto 0);
    variable r_cnt:integer:=0;
    variable buf:std_logic_vector(7 downto 0);
    begin
        if bclk_in'event and bclk_in='1' then
            case state is
                when r_start=>
                    if rxd_fall='1' then 
                        state<=r_center;
			rx_done<='0';
			r_cnt:=0;
                    else 
			state<=r_start;
			rx_done<='0';
                    end if;
                when r_center=>
                    if rxd_in='0' then
                        if count="1001"then               --排除噪音的干扰
                            state<=r_wait;
                            count:="0000";
                        else
                            count:=count+'1';
                            state<=r_center;
                        end if;
                    else
                        state<=r_start;
                    end if;
                when r_wait=>
                    if count="1110" then                    --数据位和奇偶校验位将每隔16个bclk周期被采样一次(即每一个波特率时钟被采样一次)
                        if r_cnt=data_bits then
                            state<=r_stop;
                        else
                           state<=r_sample;
                        end if;
                        count:="0000";          --等待计数到15个bclk,在16个bclk进入R_SAMPLE状态进行数据位的采样检测
                    else
                        count:=count+'1';
                        state<=r_wait;
                    end if;
                when r_sample=>
                    buf(r_cnt):=rxd_in;     --接收发送数据
                    r_cnt:=r_cnt+1;
                    state<=r_wait;
                when r_stop=> 
			if rxd_in='1' then 
				rx_buffer<=buf;
				rx_done<='1';  
				state<=r_start;
			else 
				state<=r_start;
			 end if;
                when others=>
                    state<=r_start;
            end case;               
	end if;
    end process;
end beheavior;
0 0 收起

我来回答

上传资料:
选择文件 文件大小不超过15M(格式支持:doc、ppt、xls、pdf、zip、rar、txt)
最佳答案
  • 最好是测量一下在什么情况下会出问题,是数据量大的时候,还是波特率不稳定,在生成的波特率会随时间而变化,这样就要每一段时间对波特率计数进行复位,清零了
    • 发布于 2018-11-27
    • 举报
    • 评论 0
    • 0
    • 0

其他答案 数量:5
  • 你可以先在这个程序烧录好,接一个usb转ttl串口模块,用电脑的调试助手测试一下看看是哪里的问题,再进行排查。
    • 发布于2018-10-31
    • 举报
    • 评论 0
    • 0
    • 0

  • 串口每次只能接受一个再接受需要先关闭串口再打开才有效。
    • 发布于2018-10-31
    • 举报
    • 评论 0
    • 0
    • 0

  • 这个串口的波特率比较高的时候,执行你这里的代码的时间就不能忽略了,积累一定误差时间后有可能会产生一到两个字节的错位
    • 发布于2018-11-09
    • 举报
    • 评论 0
    • 0
    • 0

  • 这种情况跟你的波特率有关系,检查一下你的波特率是不是太高导致不能接收
    • 发布于2018-11-27
    • 举报
    • 评论 0
    • 0
    • 0

  • 一般来说都是你的硬件传输有相关的校验位没有处理导致的
    • 发布于2018-12-10
    • 举报
    • 评论 0
    • 0
    • 0

相关问题

问题达人换一批

fpga实现串口接收模块