|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2011-12-1 11:23:01
|
显示全部楼层
//////////////////Transmit.v////////////////////////////
module Transmit(
clk, //System Clock 50Mhz
data_in, //8-bits pararel data input
tx_en, //Transmit enable:posedge is effective
tx, //Serial output port
busy, //Busy flag. High is effective
);
input clk;
input [7:0]data_in;
input tx_en;
output busy;
output tx;
reg busy=0;
reg tx=1;
reg [7:0]tx_reg=0;
reg clk_d=0;
reg [9:0]state=0;
reg en_pos=0;
wire tx_cs;
/*************Generate Baud rate************/
parameter BAUD_RATE=9600; //Clock=50Mhz,BD=9600bps
integer count=0;
always @(posedge clk)
begin
count <=count+1;
if(count==(25000000/BAUD_RATE))
begin
clk_d <=~clk_d;
count <=0;
end
end
/***********Generate Control Timing************/
assign tx_cs=en_pos&~tx_en;
always @(posedge clk_d)
begin
en_pos <=tx_en;
end
/**************Send Serial Data***************/
parameter
IDLE =10'b0000000001,
START=10'b0000000010,
BIT_0=10'b0000000100,
BIT_1=10'b0000001000,
BIT_2=10'b0000010000,
BIT_3=10'b0000100000,
BIT_4=10'b0001000000,
BIT_5=10'b0010000000,
BIT_6=10'b0100000000,
BIT_7=10'b1000000000;
always @(posedge clk_d or posedge tx_cs)
begin
if(tx_cs)
begin
state <=START;
tx_reg <=data_in;
busy <=1;
end
else
begin
case(state)
IDLE: begin tx<=1; busy<=0; state<=IDLE ; end
START: begin tx<=0; state<=BIT_0; end
BIT_0: begin tx<=tx_reg[0]; state<=BIT_1; end
BIT_1: begin tx<=tx_reg[1]; state<=BIT_2; end
BIT_2: begin tx<=tx_reg[2]; state<=BIT_3; end
BIT_3: begin tx<=tx_reg[3]; state<=BIT_4; end
BIT_4: begin tx<=tx_reg[4]; state<=BIT_5; end
BIT_5: begin tx<=tx_reg[5]; state<=BIT_6; end
BIT_6: begin tx<=tx_reg[6]; state<=BIT_7; end
BIT_7: begin tx<=tx_reg[7]; state<=IDLE ; end
endcase
end
end
endmodule |
|