Verilog之串口(UART)通信
時(shí)間:2025-11-16 22:46:44
手機(jī)看文章
掃描二維碼
隨時(shí)隨地手機(jī)看文章
0:起始位,低電平;1~8:數(shù)據(jù)位;9:校驗(yàn)位,高電平;10:停止位,高電平。
波特率 “9600bps”表示每秒可以傳輸9600位。
波特率定時(shí)計(jì)數(shù)器由時(shí)鐘頻率除以波特率。
采集1~8位,忽略0、9、10位。
發(fā)送“0、8位數(shù)據(jù)、1、1”
串口傳輸數(shù)據(jù),從最低位開始,到最高位結(jié)束。
串口發(fā)送:
module tx_bps_module( RSTn, Count_Sig, BPS_CLK); input CLK; input RSTn; input Count_Sig; output BPS_CLK; reg [12:0]Count_BPS; always @ ( posedge CLK or negedge RSTn ) !RSTn ) Count_BPS <= 13'd0; else if( Count_BPS == 13'd5207 ) Count_BPS <= 13'd0; else if( Count_Sig ) Count_BPS <= Count_BPS + 1'b1; else Count_BPS <= 13'd0; assign BPS_CLK = ( Count_BPS == 13'd2604 ) ? 1'b1 : 1'b0; endmodule
module tx_control_module( CLK, RSTn, TX_En_Sig, TX_Data, BPS_CLK, TX_Done_Sig, TX_Pin_Out ); input CLK; input RSTn; input TX_En_Sig; input [7:0]TX_Data; input BPS_CLK; output TX_Done_Sig; output TX_Pin_Out; /********************************************************/ reg [3:0]i; reg rTX; reg isDone; always @ ( posedge CLK or negedge RSTn ) if( !RSTn ) begin i <= 4'd0; rTX <= 1'b1; isDone <= 1'b0; end else if( TX_En_Sig ) case ( i ) 4'd0 : if( BPS_CLK ) begin i <= i + 1'b1; rTX <= 1'b0; end 4'd1, 4'd2, 4'd3, 4'd4, 4'd5, 4'd6, 4'd7, 4'd8 : if( BPS_CLK ) begin i <= i + 1'b1; rTX <= TX_Data[ i - 1 ]; end 4'd9 : if( BPS_CLK ) begin i <= i + 1'b1; rTX <= 1'b1; end 4'd10 : if( BPS_CLK ) begin i <= i + 1'b1; rTX <= 1'b1; end 4'd11 : if( BPS_CLK ) begin i <= i + 1'b1; isDone <= 1'b1; end 4'd12 : begin i <= 4'd0; isDone <= 1'b0; end endcase /********************************************************/ assign TX_Pin_Out = rTX; assign TX_Done_Sig = isDone; /*********************************************************/ endmodule
module tx_module( CLK, RSTn, TX_Data, TX_En_Sig, TX_Done_Sig, TX_Pin_Out); input CLK; input RSTn; input [7:0]TX_Data; input TX_En_Sig; output TX_Done_Sig; output TX_Pin_Out; /********************************/ wire BPS_CLK; tx_bps_module U1 ( .CLK( CLK ), .RSTn( RSTn ), .Count_Sig( TX_En_Sig ), // input - from U2 .BPS_CLK( BPS_CLK ) // output - to U2 ); /*********************************/ tx_control_module U2 ( .CLK( CLK ), .RSTn( RSTn ), .TX_En_Sig( TX_En_Sig ), // input - from top .TX_Data( TX_Data ), // input - from top .BPS_CLK( BPS_CLK ), // input - from U2 .TX_Done_Sig( TX_Done_Sig ), // output - to top .TX_Pin_Out( TX_Pin_Out ) // output - to top ); /***********************************/ endmodule
串口接收
module detect_module( RSTn, RX_Pin_In, H2L_Sig); input CLK; input RSTn; input RX_Pin_In; output H2L_Sig; reg H2L_F1; reg H2L_F2; always @ ( posedge CLK or negedge RSTn ) !RSTn ) begin H2L_F1 <= 1'b1; H2L_F2 <= 1'b1; end else begin H2L_F1 <= RX_Pin_In; H2L_F2 <= H2L_F1; end assign H2L_Sig = H2L_F2 & !H2L_F1; endmodule
module rx_control_module( RSTn, RX_Pin_In, BPS_CLK, RX_En_Sig, RX_Data, RX_Done_Sig ); input CLK; input RSTn; input H2L_Sig; input RX_En_Sig; input RX_Pin_In; input BPS_CLK; output Count_Sig; output [7:0]RX_Data; output RX_Done_Sig; reg [3:0]i; reg [7:0]rData; reg isCount; reg isDone; always @ ( posedge CLK or negedge RSTn ) !RSTn ) begin i <= 4'd0; rData <= 8'd0; isCount <= 1'b0; isDone <= 1'b0; end else if( RX_En_Sig ) case ( i ) : H2L_Sig ) begin i <= i + 1'b1; isCount <= 1'b1; end /*進(jìn)入第0位,同時(shí)驅(qū)動(dòng)bps_module開始計(jì)數(shù)。又以bps_module驅(qū)動(dòng)狀態(tài)1~11*/ : BPS_CLK ) begin i <= i + 1'b1; end /*第0位中部,BPS_CLK發(fā)出第一個(gè)脈沖,忽略第0位*/ 4'd3, 4'd4, 4'd5, 4'd6, 4'd7, 4'd8, 4'd9 : BPS_CLK ) begin i <= i + 1'b1; rData[ i - 2 ] <= RX_Pin_In; end : BPS_CLK ) begin i <= i + 1'b1; end : BPS_CLK ) begin i <= i + 1'b1; end : begin i <= i + 1'b1; isDone <= 1'b1; isCount <= 1'b0; end : begin i <= 4'd0; isDone <= 1'b0; end endcase assign Count_Sig = isCount; assign RX_Data = rData; assign RX_Done_Sig = isDone; endmodule
module rx_bps_module( RSTn, Count_Sig, BPS_CLK); input CLK; input RSTn; input Count_Sig; output BPS_CLK; reg [12:0]Count_BPS; always @ ( posedge CLK or negedge RSTn ) !RSTn ) Count_BPS <= 13'd0; else if( Count_BPS == 13'd5207 ) Count_BPS <= 13'd0; else if( Count_Sig ) Count_BPS <= Count_BPS + 1'b1; else Count_BPS <= 13'd0; assign BPS_CLK = ( Count_BPS == 12'd2604 ) ? 1'b1 : 1'b0; /*周期中間開始采集數(shù)據(jù)*/ endmodule
module rx_module( CLK, RSTn, RX_Pin_In, RX_En_Sig, RX_Done_Sig, RX_Data); input CLK; input RSTn; input RX_Pin_In; input RX_En_Sig; output [7:0]RX_Data; output RX_Done_Sig; /**********************************/ wire H2L_Sig; detect_module U1 ( .CLK( CLK ), .RSTn( RSTn ), .RX_Pin_In( RX_Pin_In ), // input - from top .H2L_Sig( H2L_Sig ) // output - to U3 ); /**********************************/ wire BPS_CLK; rx_bps_module U2 ( .CLK( CLK ), .RSTn( RSTn ), .Count_Sig( Count_Sig ), // input - from U3 .BPS_CLK( BPS_CLK ) // output - to U3 ); /**********************************/ wire Count_Sig; rx_control_module U3 ( .CLK( CLK ), .RSTn( RSTn ), .H2L_Sig( H2L_Sig ), // input - from U1 .RX_En_Sig( RX_En_Sig ), // input - from top .RX_Pin_In( RX_Pin_In ), // input - from top .BPS_CLK( BPS_CLK ), // input - from U2 .Count_Sig( Count_Sig ), // output - to U2 .RX_Data( RX_Data ), // output - to top .RX_Done_Sig( RX_Done_Sig ) // output - to top ); /************************************/ endmodule





