//==============================================================
//b2s数据发送时序
//==============================================================
reg b2s_dout_r;
reg [3:0] state;
reg [9:0] cnt;
reg [4:0] count; //★与发送数据位宽保持一致(如发送32bit数据时,count宽度为5;发送8bit时,count宽度为4)
always @ (posedge clk)
begin
case(state)
//初始化
0: begin
count<=0;
b2s_dout_r<=1;
if(cnt==19) //b2s_dout_r高电平持续20个时钟
begin
state<=1;
cnt<=0;
end
else
begin
cnt<=cnt+1;
end
end
//开始信号时序
1: begin
b2s_dout_r<=0;
if(cnt==19) //b2s_dout_r低电平持续20个时钟
begin
state<=2;
cnt<=0;
end
else
begin
cnt<=cnt+1;
end
end
2: begin
b2s_dout_r<=1;
if(cnt==19) //b2s_dout_r高电平持续20个时钟
begin
cnt<=0;
state<=3;
end
else
begin
cnt<=cnt+1;
end
end
//待发送数据的逻辑电平判断
3: begin
if(din[count]==1)
state<=4;
else
state<=8;
end
//逻辑1的发送时序
4: begin
b2s_dout_r<=0;
if(cnt==9) //b2s_dout_r低电平持续10个时钟
begin
cnt<=0;
state<=5;
end
else
begin
cnt<=cnt+1;
end
end
5: begin
b2s_dout_r<=1;
if(cnt==29) //b2s_dout_r高电平持续30个时钟
begin
cnt<=0;
state<=6;
end
else
begin
cnt<=cnt+1;
end
end
//逻辑0的发送时序
8: begin
b2s_dout_r<=0;
if(cnt==29) //b2s_dout_r低电平持续30个时钟
begin
cnt<=0;
state<=9;
end
else
begin
cnt<=cnt+1;
end
end
9: begin
b2s_dout_r<=1;
if(cnt==9) //b2s_dout_r高电平持续10个时钟
begin
cnt<=0;
state<=6;
end
else
begin
cnt<=cnt+1;
end
end
//统计已发送数据位数
6: begin
count<=count+1'b1;
state<=7;
end
7: begin
if(count==WIDTH) //当一组数据所有位发送完毕,返回并继续下一次发送
begin
b2s_dout_r<=1;
if(cnt==999) //b2s_dout_r高电平持续1000个时钟
begin
cnt<=0;
state<=0;
end
else
begin
cnt<=cnt+1;
end
end
else //当一组数据未发送完毕,则继续此组下一位数据的发送
state<=3;
end
//default值设定
default: begin
state<=0;
cnt<=0;
count<=0;
end
endcase
end
//==================================================
//b2s_din信号边沿检测
//==================================================
reg [1:0] b2s_din_edge=2'b01;
always @ (posedge clk)
begin
b2s_din_edge[0] <= b2s_din;
b2s_din_edge[1] <= b2s_din_edge[0];
end
//==================================================
//time_cnt -- 存储b2c_din信号下降沿及其最近的下一个上升沿之间的时间
//==================================================
reg [1:0] state0;
reg [5:0] time_cnt_r;
always @ (posedge clk)
begin
case(state0)
0: begin
time_cnt_r<=0;
state0<=1;
end
1: begin
if(b2s_din_edge==2'b10)
state0<=2;
else
state0<=state0;
end
2: begin
if(b2s_din_edge==2'b01)
begin
state0<=0;
end
else
time_cnt_r<=time_cnt_r+1'b1;
end
default: begin
time_cnt_r<=0;
state0<=0;
end
endcase
end
//==================================================
//b2s解码时序
//==================================================
reg [2:0] state;
reg [4:0] count; //★与接收数据位数保持一致(如接收32bit数据时,count宽度为5;接收8bit时,count宽度为4)
reg [WIDTH-1:0] dout_r;
always @ (posedge clk)
begin
case(state)
0: begin
count<=WIDTH;
if((time_cnt>15)&&(time_cnt<25)) //判断起始信号
state<=1;
else
state<=state;
end
1: begin
if((time_cnt>5)&&(time_cnt<15)) //判断接收到的位是否为1
begin
dout_r[WIDTH-1]<=1;
state<=2;
end
else if((time_cnt>25)&&(time_cnt<35)) //判断接收到的位是否为0
begin
dout_r[WIDTH-1]<=0;
state<=2;
end
else
begin
state<=state;
end
end
2: begin
count<=count-1'b1; //每读取一个bit,count计数减1
state<=3;
end
3: if(count==0) //数据读取完毕,返回并继续下一组数据的读取
begin
state<=0;
end
else
state<=4; //数据未读取完毕,则进行移位
4: begin
dout_r<=(dout_r>>1); //数据右移1位
state<=1;
end
default: begin
state<=0;
count<=WIDTH;
end
endcase
end