错误描述

- 状态转换时BitOut会存在未知状态的输出(一个时钟周期长度):
reg [5:0] data_sync = 6'b111111;
always @ (posedge clk) begin
case (state_reg)
0:begin
if(Bit_in_EN_r1==1)begin
if($unsigned(bit_cnt) ==6) begin
state_reg<=1;
end
BitOut <= data_sync[bit_cnt];
bit_cnt = bit_cnt + 1; // 索引增加
end
end
1:begin
BitOut <= 0;
bit_cnt = bit_cnt + 1;
if(bit_cnt==33) begin
bit_cnt = 0;
state_reg<=0;// 发送完成
end
end
endcase
end
- 先判断 bit_cnt == 6 → 成立 → 进入状态1
- 但此时还会执行:BitOut <= data_sync[bit_cnt] → data_sync[6] 导致越界!
解决方案

- 需要注意状态转移判断的时机(状态转移判断置后),这也是常用的写法
reg [5:0] data_sync = 6'b111111;
always @ (posedge clk) begin
case (state_reg)
0:begin
if(Bit_in_EN_r1==1)begin
BitOut <= data_sync[bit_cnt];
bit_cnt = bit_cnt + 1; // 索引增加
if($unsigned(bit_cnt) ==6) begin // 状态转移后判断
state_reg<=1;
end
end
end
1:begin
BitOut <= 0;
bit_cnt = bit_cnt + 1;
if(bit_cnt==33) begin
bit_cnt = 0;
state_reg<=0;// 发送完成
end
end
endcase
end
