|
module SPI_Tx(
input wire clk,
input wire [31:0] dat,
input wire en,//使能,下降沿触发(可以不用,用于延时开始初始化)
output wire spi_clk,//
output wire spi_sdi,
output wire spi_cs
);
reg tFlag = 0;
reg [3:0] state = 0;
reg [9:0] cnt = 8'h0;
reg [31:0] shift_reg = 32'h0000_0000;
reg oclk;
reg odat;
reg ocs;
assign spi_clk = oclk;
assign spi_sdi = odat;
assign spi_cs = ocs;
reg lastEn = 0;
always @(posedge clk )
begin
if (tFlag==0) begin
if (lastEn==1'b1 && en == 1'b0) begin
tFlag<=1'b1;
end
end
else begin
if (state==4'h2)
tFlag<=1'b0;
end
lastEn <= en;
end
//状态
always @(posedge clk )
begin
if (tFlag==1'b1) begin
state <= 4'h1;
end
if (state==4'h1) begin
state<=4'h2;
end
else if (cnt>127) begin
state<=4'h0;
end
end
//计数器
always @(posedge clk )
begin
if (state==4'h1) begin
cnt <= 8'h0;
end
else if (state==4'h2) begin
cnt<=cnt+10'h1;
end
else
cnt <= 8'h0;
end
//spi cs输出
always @(posedge clk )
begin
if (state==4'h0)
ocs<=0;
else
ocs<=1;
end
//spi clk,dat输出
always @(posedge clk )
begin
if (state==4'h1) begin
shift_reg<=dat;
end
if (state==4'h2) begin
case (cnt%4)
0: begin
odat<=shift_reg[31];
end
1: oclk<=1;
2: odat<=0;
3: begin
oclk<=0;
shift_reg <= {shift_reg[30:0],1'b0};
end
endcase
end
else begin
odat<=0;
oclk<=0;
end
end
endmodule |
阿莫论坛20周年了!感谢大家的支持与爱护!!
月入3000的是反美的。收入3万是亲美的。收入30万是移民美国的。收入300万是取得绿卡后回国,教唆那些3000来反美的!
|