|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?注册
x
module tx(clk,din,reset, wr,ce,dbf, txd,state,state0);
input clk,reset ,wr,ce;
//clk=11.0592MHz
//baud=11.0592Mhz/12=921.6kHz
input [7:0]din;
output txd,dbf;output [3:0] state;output [1:0] state0;
reg[7:0] data;
reg[9:0] shift;
reg[4:0] count,t;
reg [3:0] state;
reg [1:0] state0;
reg dbf,tbr; // dbf--data buffer full. tbr--transmit buffer ready.
wire txd;
assign txd=shift[0];
always@(posedge clk)
if (reset==0)
state0<=0;
else
case(state0)
2'b00:begin
if(wr==1 && ce==1)
begin state0<=1;data<=din;end
if(tbr==1) //发送数据准备好
state0<=2;
end
2'b01:if(wr==0 || ce==0)
begin dbf<=1;state0<=0;end
2'b10:begin dbf<=0;state0<=0;end
endcase
always@(posedge clk)
if (reset==0)
begin state<=0; shift[0]<=1;end
else
case(state)
4'b0000:if(t==11 && dbf==1)
begin
tbr<=1; state<=1;t<=0;
shift[9]<=1;
shift[8:1]<=data[7:0];
shift[0]<=0; //bit0:start bit
end
else
if (t<11) t=t+1;
4'b0001: if(t==11) //bit1:d0
begin shift<=shift>>1; shift[9]<=1; state<=2;t<=0; tbr<=0; end
else
t<=t+1;
4'b0010: if(t==11) //bit2:d1
begin shift<=shift>>1; shift[9]<=1; state<=3;t<=0; end
else
t<=t+1;
4'b0011: if(t==11) //bit3:d2
begin shift<=shift>>1; shift[9]<=1; state<=4;t<=0; end
else
t<=t+1;
4'b0100: if(t==11) //bit4:d3
begin shift<=shift>>1; shift[9]<=1; state<=5;t<=0; end
else
t<=t+1;
4'b0101: if(t==11) //bit5:d4
begin shift<=shift>>1; shift[9]<=1; state<=6;t<=0; end
else
t<=t+1;
4'b0110: if(t==11) //bit6:d5
begin shift<=shift>>1; shift[9]<=1; state<=7;t<=0; end
else
t<=t+1;
4'b0111: if(t==11) //bit7:d6
begin shift<=shift>>1; shift[9]<=1; state<=8;t<=0; end
else
t<=t+1;
4'b1000: if(t==11) //bit8:d7
begin shift<=shift>>1; shift[9]<=1; state<=9;t<=0; end
else
t<=t+1;
4'b1001: if(t==11) //bit9:stop
begin shift<=shift>>1; shift[9]<=1; state<=0;t<=0; end
else
t<=t+1;
default
begin t<=0; state<=0;end
endcase
endmodule
|
|