首页 > 代码库 > Verilog之串口通信

Verilog之串口通信

0:起始位,低电平;1~8:数据位;9:校验位,高电平;10:停止位,高电平。

采集1~8位,忽略0、9、10位。

串口传输数据,从最低位开始,到最高位结束。

串口发送:

 

串口接受

module rx_control_module                        (                            CLK, RSTn,                             H2L_Sig, RX_Pin_In, BPS_CLK, RX_En_Sig,                        Count_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 )                             if( !RSTn )                                  begin                                   i <= 4d0;                                     rData <= 8d0;                         isCount <= 1b0;                         isDone <= 1b0;                     end                  else if( RX_En_Sig )                              case ( i )                                                           4d0 :                                 if( H2L_Sig ) begin i <= i + 1b1; isCount <= 1b1; end         /*进入第0位,同时驱动bps_module开始计数*/                                              4d1 :                          if( BPS_CLK ) begin i <= i + 1b1; end                          /*第0位中部,BPS_CLK发出第一个脉冲,忽略第0位*/                                              4d2, 4d3, 4d4, 4d5, 4d6, 4d7, 4d8, 4d9 :                         if( BPS_CLK ) begin i <= i + 1b1; rData[ i - 2 ] <= RX_Pin_In; end                                                  4d10 :                         if( BPS_CLK ) begin i <= i + 1b1; end                                                  4d11 :                         if( BPS_CLK ) begin i <= i + 1b1; end                                                  4d12 :                         begin i <= i + 1b1; isDone <= 1b1; isCount <= 1b0; end                                                  4d13 :                         begin i <= 4d0; isDone <= 1b0; end                                             endcase                                    /********************************************************/                                                      assign Count_Sig = isCount;                         assign RX_Data =http://www.mamicode.com/ rData;                         assign RX_Done_Sig = isDone;                                                                           /*********************************************************/                                             endmodule                        

 

module detect_module                     (                        CLK, 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 )                         if( !RSTn )                              begin                                H2L_F1 <= 1b1;                         H2L_F2 <= 1b1;                end              else                          begin                                H2L_F1 <= RX_Pin_In;                         H2L_F2 <= H2L_F1;                end                            /***************************************/                                        assign H2L_Sig = H2L_F2 & !H2L_F1;                                        /***************************************/                                    endmodule                    
module rx_bps_module            (                CLK, 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 )                if( !RSTn )                     Count_BPS <= 13d0;             else if( Count_BPS == 13d5207 )                 Count_BPS <= 13d0;             else if( Count_Sig )                 Count_BPS <= Count_BPS + 1b1;             else                 Count_BPS <= 13d0;                       /********************************/                        assign BPS_CLK = ( Count_BPS == 12d2604 ) ? 1b1 : 1b0; //在周期中间采集数据                            /*********************************/                        endmodul

Verilog之串口通信