实现FPGA与PC机的串口通信功能

源代码在线查看: uart_tx.v

软件大小: 40 K
上传用户: foresnake
关键词: FPGA PC机 串口 通信功能
下载地址: 免注册下载 普通下载 VIP

相关代码

				/******************************************************************************				* File:    uart_tb.v				* Version: V0.0				* Author:  minjingguo 				* Date:    20070814				* Company: SHHIC Co., Ltd.				******************************************************************************				* Description:				* 				*				******************************************************************************				* Version: V0.1				* Modifier: name 				* Date:				* Description:				******************************************************************************/												// *************************    				// MODULE DEFINTION				//**************************								module  uart_tx				    (				    //INPUT				    rst     ,				    clk16x  ,				    din     ,				    wr      ,				    parity_def,           				    				    //OUTPUT				    tbre    ,       //tbre=0 if tx is empty				    sdo     				    );				    				// *************************				// INPUTS				// *************************				input           rst         ;				input           clk16x      ;				input   [7:0]   din         ;				input           wr          ;				input           parity_def  ;				// *************************				// OUTPUTS 				// *************************				output          tbre        ;				output          sdo         ;								// *************************				// INTERNAL SIGNALS				// *************************				reg     [3:0]   cnt_clk     ;				reg     [3:0]   cnt_byte    ;				reg             tbre        ;				reg     [7:0]   tbr         ;				reg             sdo         ;				reg     [7:0]   tsr         ;				reg		parity	;				// *************************				// CODE				// *************************								/////////////////////////////////////////////////////////////////////////////////////				//process for generate the clock numbers for transmit one byres data + verify data //				//note: one cycle for transmit needs 16 x 12 = 192 clocks                          //				///////////////////////////////////////////////////////////////////////////////////// 				always @(posedge clk16x or negedge rst)				begin				    if (rst== 1'h0)				    begin				        cnt_clk     				        cnt_byte    				    end				    else				    begin				        if (cnt_byte==4'hf)				        begin				            if (wr)				            begin				                cnt_clk     				                cnt_byte    				            end				        end				        else				        begin				            cnt_clk     				            if (cnt_clk==4'hf)				            begin				                if (cnt_byte==4'hb)				                    cnt_byte    				                else				                    cnt_byte    				            end				        end				    end				end								/////////////////////////////////////////////////////////////////////////////////////////				//process for generate the signal tbre                                                 //				//the signal tbre =1 during the process of transmitting data, and tbre=0 for complit   //				/////////////////////////////////////////////////////////////////////////////////////////				always @(posedge clk16x or negedge rst)				begin				    if (rst== 1'h0)				    begin				        tbre    				        tbr     				    end				    else				    begin				        if (wr)				        begin				            tbre    				            tbr     				        end				        else if (cnt_clk==4'hf && cnt_byte == 4'hb)				        begin				            tbre    				        end				    end				end								//////////////////////////////////////////////////////////////////////////////////////////				//process for generate transmit the data for serial bit                                 //				//note: the first one bit for start=0 bit, before the start bit wait for 8 clocks,      //				//      after 16 clocks for start bit, transmit 8bits data with transmit the msb firstly//				//      then parity bit, last transmit for stop bit                                     //				//////////////////////////////////////////////////////////////////////////////////////////				always @(posedge clk16x or negedge rst)				begin				    if (rst== 1'h0)				    begin				        sdo     				        parity 				        tsr     				    end				    else if (cnt_clk==4'h7)//wait for 8 clocks				    begin				        if (cnt_byte == 4'h0)				        begin				            tsr     				            sdo     				            parity  				        end				        else if ((cnt_byte >= 4'h1) && (cnt_byte 				        begin				            tsr     				            sdo     				            parity  				        end				        else if (cnt_byte == 4'h9)//wait for 9 cycles				        begin				            sdo  				        end				        else if (cnt_byte == 4'ha)				        begin				            sdo  				        end				        else if (cnt_byte == 4'hb)				        begin				            sdo  				            parity  				        end				    end				end								endmodule								 							

相关资源