⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rx.v

📁 FPGA上实现UART串口原程序
💻 V
字号:
//**********************************************************//
//                 串口接收子程序    V 1.1   				   //
//                                   BooQuai						//
//						2005-6-16											//
// 采用串口通讯接收通道的基本原理,即16倍于波特率的采样时钟	//
//																				//
// clk:50M系统时钟,rxok:一贞数据接收完毕,rx:接收端口Rx   	//
//																				//
// data:数码管格式数据 oridt:原始接收数据							//
//																				//
// 可已通过div_xxxx设置波特率。贞格式:0****_****1				//
//																				//
// 																			//
// *********************************************************//

module rx(clk,rx,rxok,data,tx,oridt);
input clk,rx;
output rxok,tx;
output[7:0] data,oridt;
wire [13:0] baudbuf;

reg rxok,tx;
reg[7:0] data,oridt;
reg[8:0]databuf,databuf2;
reg[4:0] cnt;
reg[2:0] state;
reg[3:0] bitpos;
reg[13:0] count;
reg rxclk;

parameter idle=3'b001,
          rxrun=3'b010,
			 stop=3'b100;

parameter baud_300=10416;				  //div_xxxx=50000000/(baudrate*16)-1
parameter baud_9600=325;
parameter baud_19200=162;
parameter baud_115200=27;


   always @(posedge clk)
   begin
    if(count==baud_9600) begin		
      count<=0;
	   rxclk<=1; 
	 end
    else	 begin
      count<=count+1;
	   rxclk<=0;
    end 
   end 


always @(posedge rxclk)
begin
  case(state)
  idle:begin 
           rxok<=0;						   //无数据
           if(rx==1)  begin				//无数据
			    tx<=1;
             cnt<=0;
	          state<=idle;
            end
            else begin					   //起始位
				  cnt<=cnt+1;
				  if(cnt==7) begin		   //7次采样
				    cnt<=0;
				//	 tx<=0;
					 bitpos<=0;
					 state<=rxrun;				//数据采样,接收
				  end
             end
        end
 rxrun: begin
           if(cnt==15) begin
			    cnt<=0;							
				 databuf[bitpos]<=rx;		//16个T采样一次
			//	 tx<=rx;
				 bitpos<=bitpos+1;			//由低至高
				 if(bitpos==8) begin			//采至停止位(可改为bitpos==7,采至MSB)
				   bitpos<=0;
					state<=stop;				//停止接收
				 end
            end
				else cnt<=cnt+1;
         end
 stop: begin
         databuf2<=databuf;			   //锁存
			oridt<=databuf[7:0];				//锁存
		//	tx<=1;
			rxok<=1;
			if(cnt==7)  begin
			 cnt<=0;
			 state<=idle;  end
         else
			  cnt<=cnt+1;
       end
  default: state<=idle;
  endcase
end					  
 
 
 always @(rxclk)								//译码
 begin
 case(databuf2[7:0])
       8'h30: begin data<=8'b01000000; end  
       8'h31: begin data<=8'b01111001; end
       8'h32: begin data<=8'b00100100; end
       8'h33: begin data<=8'b00110000; end
       8'h34: begin data<=8'b00011001; end
       8'h35: begin data<=8'b00010010; end
       8'h36: begin data<=8'b00000010; end
       8'h37: begin data<=8'b01111000; end
		 8'h38: begin data<=8'b00000000; end
       8'h39: begin data<=8'b00010000; end
    default: data<=8'b00000000;
 endcase
end


endmodule

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -