简易RS232 建模二 (接收)

//clK系统时钟为50MHZ 先发低位后发高位 先接收地位后接收高位
module uart_tx (input clk,rst_n, UART_CTS,
 output reg UART_RTS,
 input  UART_RXD,
 output reg UART_TXD,
 output  [7:0] led);
reg [3:0]state;
reg [30:0] count;
reg [7:0] data;
assign led= rx_data;

reg [7:0] rx_data;
reg a,b;//reg[3:0] state;
always @ (posedge clk)
begin
if(!rst_n) begin rx_data<=8'h05;count<=0;end
else begin
 case (state)
     0:begin a<=UART_RXD;b<=a; if(b && !a) state<=1;else state<=0;end
  1:if(count==2603) begin if(UART_RXD==0)state<=2;else state<=0;count<=0;end //采集开始位 0电平的中点
    else            begin state<=1;count<=count+1;end  
  2:if(count==5208) begin state<=3;count<=0;rx_data[0]<=UART_RXD;end         //计数到数据位的中点
    else            begin state<=2;count<=count+1;end
  3:if(count==5208) begin state<=4;count<=0;rx_data[1]<=UART_RXD;end
    else            begin state<=3;count<=count+1;end 
  4:if(count==5208) begin state<=5;count<=0;rx_data[2]<=UART_RXD;end
    else            begin state<=4;count<=count+1;end 
  5:if(count==5208) begin state<=6;count<=0;rx_data[3]<=UART_RXD;end
    else            begin state<=5;count<=count+1;end  
  6:if(count==5208) begin state<=7;count<=0;rx_data[4]<=UART_RXD;end
    else            begin state<=6;count<=count+1;end
  7:if(count==5208) begin state<=8;count<=0;rx_data[5]<=UART_RXD;end
    else            begin state<=7;count<=count+1;end 
  8:if(count==5208) begin state<=9;count<=0;rx_data[6]<=UART_RXD;end
    else            begin state<=8;count<=count+1;end 
  9:if(count==5208) begin state<=10;count<=0;rx_data[7]<=UART_RXD;end
    else            begin state<=9;count<=count+1;     end 
  10:if(count==5208) begin state<=11;count<=0;end                            //采集结束位
    else            begin state<=10;count<=count+1;     end
  11:state=0;                                                                //回状态一,检测下降沿
  default state=0;
  endcase
end
end
endmodule

经过测试:可以正常工做------------主要技术点---下降沿的检测-----数据中间采样-----分频计数值的计算 

 

---下降沿检测:a为50MHZ采样时钟采样的当前接收值,b为前一个接收值,------即当前一个接收值为1当前为0时为下降沿---即算法为b & !a

---分频系数:50MHZ/9600=5208,也就是说明每计数达到5208时就是9600波特率的上升沿---同理计数到2603为9600波特率的下降沿

---数据中间采样:开始时候计数到2603为开始位的中间点,以后计数到5208时为数据采样的中间点

转载于:https://www.cnblogs.com/TFH-FPGA/archive/2012/08/08/2627673.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值