MyFPGA Forum

 找回密码
 注册
搜索
查看: 7042|回复: 4
打印 上一主题 下一主题

rs232串口问题

[复制链接]
跳转到指定楼层
1#
发表于 2010-5-22 10:47:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
// RS-232 TX module
// (c) fpga4fun.com KNJN LLC - 2003, 2004, 2005, 2006

//`define DEBUG   // in DEBUG mode, we output one bit per clock cycle (useful for faster simulations)

module async_transmitter(clk, TxD_start, TxD_data, TxD, TxD_busy);
input clk, TxD_start;
input [7:0] TxD_data;
output TxD, TxD_busy;

parameter ClkFrequency = 25000000;        // 25MHz
parameter Baud = 9600;//115200;
parameter RegisterInputData = 1;        // in RegisterInputData mode, the input doesn't have to stay valid while the character is been transmitted

// Baud generator
parameter BaudGeneratorAccWidth = 16;
reg [BaudGeneratorAccWidth:0] BaudGeneratorAcc;
`ifdef DEBUG
wire [BaudGeneratorAccWidth:0] BaudGeneratorInc = 17'h10000;
`else
wire [BaudGeneratorAccWidth:0] BaudGeneratorInc = ((Baud<<(BaudGeneratorAccWidth-4))+(ClkFrequency>>5))/(ClkFrequency>>4);
`endif

wire BaudTick = BaudGeneratorAcc[BaudGeneratorAccWidth];
wire TxD_busy;
always @(posedge clk) if(TxD_busy) BaudGeneratorAcc <= BaudGeneratorAcc[BaudGeneratorAccWidth-1:0] + BaudGeneratorInc;

// Transmitter state machine
reg [3:0] state;
wire TxD_ready = (state==0);
assign TxD_busy = ~TxD_ready;

reg [7:0] TxD_dataReg;
always @(posedge clk) if(TxD_ready & TxD_start) TxD_dataReg <= TxD_data;
wire [7:0] TxD_dataD = RegisterInputData ? TxD_dataReg : TxD_data;

always @(posedge clk)
case(state)
        4'b0000: if(TxD_start)state <= 4'b0001;
        4'b0001: if(BaudTick) state <= 4'b0100;
        4'b0100: if(BaudTick) state <= 4'b1000;  // start
        4'b1000: if(BaudTick) state <= 4'b1001;  // bit 0
        4'b1001: if(BaudTick) state <= 4'b1010;  // bit 1
        4'b1010: if(BaudTick) state <= 4'b1011;  // bit 2
        4'b1011: if(BaudTick) state <= 4'b1100;  // bit 3
        4'b1100: if(BaudTick) state <= 4'b1101;  // bit 4
        4'b1101: if(BaudTick) state <= 4'b1110;  // bit 5
        4'b1110: if(BaudTick) state <= 4'b1111;  // bit 6
        4'b1111: if(BaudTick) state <= 4'b0010;  // bit 7
        4'b0010: if(BaudTick) state <= 4'b0011;  // stop1
        4'b0011: if(BaudTick) state <= 4'b0000;  // stop2
        default: if(BaudTick) state <= 4'b0000;
endcase

// Output mux
reg muxbit;
always @( * )
case(state[2:0])
        3'd0: muxbit <= TxD_dataD[0];
        3'd1: muxbit <= TxD_dataD[1];
        3'd2: muxbit <= TxD_dataD[2];
        3'd3: muxbit <= TxD_dataD[3];
        3'd4: muxbit <= TxD_dataD[4];
        3'd5: muxbit <= TxD_dataD[5];
        3'd6: muxbit <= TxD_dataD[6];
        3'd7: muxbit <= TxD_dataD[7];
endcase

// Put together the start, data and stop bits
reg TxD;
always @(posedge clk) TxD <= (state<4) | (state[3] & muxbit);  // register the output to make it glitch free

endmodule

其中的
TxD <= (state<4) | (state[3] & muxbit);
是什么意思?它能实现发送起始位0和两位停止位吗?
2#
发表于 2010-5-24 11:21:38 | 只看该作者
在state<4时候,刚好是发送stop1 stop2所处状态,既然其条件为真,则发送的为高电平;当state[3]==1时候,刚好发送八位数据,而数据内容muxbit,故发送的就是八位数据,其实这个无非是把要发送的数据用clk时钟锁存后再送到TxD,以期去除毛刺。
3#
 楼主| 发表于 2010-5-25 15:37:33 | 只看该作者
多谢
4#
发表于 2010-8-11 09:27:45 | 只看该作者
回复 2# hdhuang


// Baud generator
parameter BaudGeneratorAccWidth = 16;
reg [BaudGeneratorAccWidth:0] BaudGeneratorAcc;
`ifdef DEBUG
wire [BaudGeneratorAccWidth:0] BaudGeneratorInc = 17'h10000;
`else
wire [BaudGeneratorAccWidth:0] BaudGeneratorInc = ((Baud<<(BaudGeneratorAccWidth-4))+(ClkFrequency>>5))/(ClkFrequency>>4);
`endif
里面的波特率是怎么产生的啊,请教一下。
5#
发表于 2010-8-12 11:09:56 | 只看该作者
回复 1# zlsopc


这个模块我有点看不懂啊,可以不可以加点注释啊。谢了。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|MyFPGA

GMT+8, 2024-5-3 06:01 , Processed in 0.035078 second(s), 14 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表