新手上路
- 积分
- 36
- 金钱
- 36
- 注册时间
- 2022-11-29
- 在线时间
- 9 小时
|
楼主 |
发表于 2023-2-27 10:44:23
|
显示全部楼层
代码在这儿
module serial_rec
(
input wire sys_clk ,
input wire rst_n ,
input wire SDATA ,
output reg out_flag ,
output reg [11:0] data_rec ,
output reg CS
);
reg [8:0]cnt_125k ;
wire sample_flag ;
wire sclk ;
reg aclk ;
(* keep = "true" *)reg [3:0] cnt ;
reg out_flag_reg ;
reg sample_flag_pre ;
reg [4:0]cnt_2m ;
reg [2:0]cnt_aclk ;
always@(posedge sys_clk or negedge rst_n)
begin
if(!rst_n)
cnt_aclk<=3'd0;
else if(cnt_aclk==3'd4)
cnt_aclk<=3'd0;
else
cnt_aclk<=cnt_aclk+1'b1;
end
always@(posedge sys_clk or negedge rst_n)
begin
if(!rst_n)
aclk<=1'b0;
else if(cnt_aclk==3'd4)
aclk<=1'b1;
else
aclk<=1'b0;
end
always@(posedge sys_clk or negedge rst_n)
begin
if(!rst_n)
cnt_125k<=9'd0;
else if(cnt_125k==9'd399)
cnt_125k<=9'd0;
else
cnt_125k<=cnt_125k+1'b1;
end
assign sample_flag=(cnt_125k>199)?1'b1:1'b0;
always@(posedge sys_clk or negedge rst_n)
begin
if(!rst_n)
cnt_2m<=5'd0;
else if(cnt_2m==5'd24)
cnt_2m<=5'd0;
else
cnt_2m<=cnt_2m+1'b1;
end
assign sclk=(cnt_2m>5'd12)?1'b1:1'b0;
always@(negedge sclk or negedge rst_n) //cnt
begin
if(!rst_n)
cnt<=4'd0;
else if(cnt==4'd14)
cnt<=4'd0;
else if(CS==1'b0)
cnt<=cnt+1'b1;
end
always@(negedge sclk or negedge rst_n) //data_rec
begin
if(!rst_n)
data_rec<=12'd0;
else case(cnt)
4'd2 :data_rec[11]<=SDATA;
4'd3 :data_rec[10]<=SDATA;
4'd4 :data_rec[9]<=SDATA;
4'd5 :data_rec[8]<=SDATA;
4'd6 :data_rec[7]<=SDATA;
4'd7 :data_rec[6]<=SDATA;
4'd8 :data_rec[5]<=SDATA;
4'd9 :data_rec[4]<=SDATA;
4'd10:data_rec[3]<=SDATA;
4'd11:data_rec[2]<=SDATA;
4'd12:data_rec[1]<=SDATA;
4'd13:data_rec[0]<=SDATA;
default:;
endcase
end
always@(negedge sclk or negedge rst_n) //打一拍
begin
if(!rst_n)
out_flag_reg<=1'b0;
else if(cnt==4'd13)
out_flag_reg<=1'b1;
else
out_flag_reg<=1'b0;
end
always@(negedge sclk or negedge rst_n)
begin
if(!rst_n)
out_flag<=1'b0;
else
out_flag<=out_flag_reg;
end
always@(posedge aclk or negedge rst_n) //采样上升沿检测
begin
if(!rst_n)
sample_flag_pre<=1'b0;
else
sample_flag_pre<=sample_flag;
end
always@(posedge aclk or negedge rst_n) // CS
begin
if(!rst_n)
CS<=1'b1;
else if(sample_flag&(~sample_flag_pre))
CS<=1'b0;
else if(cnt==4'd14)
CS<=1'b1;
else
CS<=CS;
end
ila_0 ila_0_inst(
|
-
ILA波形
-
仿真波形
|