分享一个SPI发送代码,实测控制频综,效果很好
module SPI_Tx(input wire clk,
input wire dat,
input wire en,//使能,下降沿触发(可以不用,用于延时开始初始化)
output wire spi_clk,//
output wire spi_sdi,
output wire spi_cs
);
reg tFlag = 0;
reg state = 0;
reg cnt = 8'h0;
reg 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;
end
1: oclk<=1;
2: odat<=0;
3: begin
oclk<=0;
shift_reg <= {shift_reg,1'b0};
end
endcase
end
else begin
odat<=0;
oclk<=0;
end
end
endmodule 一般环境里面都有现成的ip核啊,为啥要自己写呢? ArthurBruin 发表于 2018-4-19 08:32
一般环境里面都有现成的ip核啊,为啥要自己写呢?
自己写的 知根知底,厂家提供的IP核用久了 多了 你就会发现会有一些不稳定不可靠的情况,但是人家代码是保密的你自己没办法修改
页:
[1]