异步串口的发送与接收

转载 2011年01月12日 22:24:00

module RS_232_r(clk9600,reset,datain,dataout,valid);
input clk9600,datain,reset;
output reg [7:0] dataout;
output reg valid;

reg[7:0] temp;
reg flag;
reg [3:0] state;

parameter IDLE=4'd0,
          S0=4'd1,
          S1=4'd2,
          S2=4'd3,
          S3=4'd4,
          S4=4'd5,
          S5=4'd6,
          S6=4'd7,
          S7=4'd8,
          stop=4'd9;
always @(posedge clk9600 or negedge reset)
begin
 
if(!reset)
   
begin
      state
<=IDLE;
     
//dataout<=8'b11111111;
      flag<=1'b0;
      valid<=1'b0;
    end
 
else
   
begin
     
case(state)
          IDLE:
             
begin
               
if(datain==1'b0)
                  begin
                   state
<=S0;
                   flag
<=1'b1;
                   valid<=1'b0;
                  end
               
else
                  
begin
                   state
<=IDLE;
                   flag
<=1'b0;
                   valid<=1'b0;
                   end
             
end
          S0:
            
begin
             temp[
0]<=datain;
             state
<=S1;
             flag
<=1'b1;
             valid<=1'b0;
             end
          S1:
            
begin
             temp[
1]<=datain;
             state
<=S2;
             flag
<=1'b1;
             valid<=1'b0;
             end
          S2:
            
begin
             temp[
2]<=datain;
             state
<=S3;
             flag
<=1'b1;
             valid<=1'b0;
             end
          S3:
            
begin
             temp[
3]<=datain;
             state
<=S4;
             flag
<=1'b1;
             valid<=1'b0;
             end  
          S4:
            
begin
             temp[
4]<=datain;
             state
<=S5;
             flag
<=1'b1;
             valid<=1'b0;
             end
          S5:
            
begin
             temp[
5]<=datain;
             state
<=S6;
             flag
<=1'b1;
             valid<=1'b0;
             end
          S6:
            
begin
             temp[
6]<=datain;
             state
<=S7;
             flag
<=1'b1;
             valid<=1'b0;
             end
          S7:
            
begin
             temp[
7]<=datain;
             state
<=stop;
             flag
<=1'b0;
             valid<=1'b1;
             end
         stop:
             
begin
                state
<=IDLE;
                dataout
<=temp;
                flag
<=1'b0;
                valid<=1'b0;
              end             
     
endcase
   
end
end
endmodule

 

 

 

 

 

 

 

module rs232_tx
(
   
input clk,
   
input rst_n,
   
input flag,
   
input [7:0] rx,
   
output reg dataout
);

reg[9:0] temp;
integer index;
reg valid;
always @ (posedge clk or negedge rst_n)
begin
   
if(!rst_n)
   
begin
        dataout
<= 1'b1;
        valid <= 1'b0;
        //temp  <= 10'b1111111111;
    end
   
else
   
begin
       
if(flag)
       
begin
        temp
<= {1'b1,rx[7:0],1'b0};
        valid
<=1'b1;
        index <= 0;
        dataout
<=1'b1;
        end
       
else
           
if(valid==1'b1)
            begin
                dataout
<= temp[index];
               
if(index==9)
                    valid
<=1'b0;
                else
                    valid
<=1'b1;
                index <= index +1'b1;
            end
           
else
                dataout
<=1'b1;
    end
end
endmodule

 

 

相关文章推荐

CreateFile系类异步、并行(同一个串口发送接收)

在Windows系统下,串口数据收发CreateFile系类方法对于我来说是最好用的方法,采用异步机制保证了串口读或写入不会使代码停在串口函数内(停在串口函数内原因可能是串口线的原因,或者串口没打开(...

VS2013 Windows API 串口通信编程--多线程异步方式

转载自:blog.csdn.net/mingojiang 1.串口通信基础 提到串口让人想起并口,它们是计算机中两个比较重要的通信方式. 串口:也叫COM口,把字节的二进制位按位列队进行传输,每个字...

TCP/UDP异步发送接收

  • 2015-08-11 16:07
  • 196KB
  • 下载

嵌入式系统上的异步串口通信的实现

在嵌入式系统中,异步串口(UART)使用非常频繁,可以用于与各种外部系统(帧括PC)之间的通信。在硬件上UART通过在每个字节的传输中插入开始位和停止位,保证接收端可以正确地找到字节的开始和结束,同时...
  • ronhu
  • ronhu
  • 2017-04-14 21:09
  • 614

win32串口同步、异步编程

在工业控制中,工控机(一般都基于Windows平台)经常需要与智能仪表通过串口进行通信。串口通信方便易行,应用广泛。 一般情况下,工控机和各智能仪表通过RS485总线进行通信。RS485的通信方式是...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)