用CPLD做编码器的解码,用的是状态机写的485通讯,遇到一个很奇怪的问题,写的状态机如下:
case(state)
Idle:
begin
over <= 1'b0;
txd <= 1'b1;
if(start_fg == 1'b1)
begin
state <= Request;
E_c <= 1'b1;
start_fg1 <= 1'b1;
end
end
Request:
begin
if(i == 8'd10)
begin
state <= Wait;
start_fg1 <= 1'b0;
E_c <= 1'b0;
CF_r <= 10'd0;
txd <= 1'b1;
i <= 6'd0;
end
else
begin
txd <= CF[6'd9-i];
i <= i+1'b1;
end
end
Wait:
begin
txd <= 1'b1;
E_b <= 1'b1;
if(CF_r[3:0] == 4'b0010)
begin
state <= Receive;
i <= 6'd0;
CF_r <= (CF_r << 1)|re;
i <= 6'd4;
end
else
begin
CF_r <= (CF_r << 1)|re;
if(i == 4'd14)
begin
state <= Idle;
erro <= 2'd3;
end
else
begin
i <= i+1'b1;
end
end
end
Receive:
begin
if(i < 6'd9)
begin
CF_r <= (CF_r << 1)|re;
i <= i+1'b1;
end
else if(i == 6'd9)
begin
if(CF_r == CF)
begin
CRC_buf <= CRC_buf|{CF_r[8:1],40'd0};
CF_r <= (CF_r << 1)|re;
i <= i+1'b1;
end
else
begin
state <= Idle;
i <= 0;
erro <= 2'd0;
end
end
else if(i < 6'd19)
begin
CF_r <= (CF_r << 1)|re;
i <= i+1'b1;
end
else if(i == 6'd19)
begin
if(CF_r[4] | CF_r[3])
begin
erro <= 2'd2;
i <= i+1'b1;
end
else
begin
CRC_buf <= CRC_buf|{CF_r[8:1],32'd0};
DATA_TEMP <= (DATA_TEMP >> 1)|{re, 29'd0};
i <= i+1'b1;
end
end
else if(i < 6'd49)
begin
DATA_TEMP <= (DATA_TEMP >> 1)|{re, 29'd0};
i <= i+1'b1;
end
else if(i < 6'd59)
begin
CF_r <= (CF_r << 1)|re;
i <= i+1'b1;
end
else
begin
DATA_TEMP[23:0] = {DATA_TEMP[28:21],DATA_TEMP[18:11],DATA_TEMP[8:1]};
for(i = 6'd0; i < 24; i = i+1'd1)
begin
CRC_buf[31-i] = DATA_TEMP
;
end
CRC_buf = CRC_buf|CF_r[8:1];
for(i = 6'd47; i > 6'd7; i = i-1'b1)
begin
CRC_buf[47:40] = {CRC_buf[46:40],(CRC_buf[47]^CRC_buf[39])};
CRC_buf[39:0] = CRC_buf[38:0]<<1; //shift
end
if (!CRC_buf[47:40])
begin
DATA_LAST =DATA; //24'd3;//
DATA =DATA_TEMP[23:0]; //24'h01fffd; //
end
else
begin
CRC_buf[47:40]=8'd0;
DATA=24'hffffff;
end
state <= Idle;
E_b <= 1'd0;
i <= 6'd0;
over <= 1'd1;
end
end
endcase
加了一句如下划线的语句后,状态机不跳了,txd没有输出,看RTL Viewer太复杂了查不过来,有没有哪位有经验的前辈能够指点一下这会是什么原因呢?DATA跟状态机完全没有关系呢。。
[
本帖最后由 mlovew723 于 2013-11-23 09:37 编辑 ]
此帖出自
小平头技术问答
如果你却掉这一句, 如果有txd, 那很明显, 你覆盖了data值, 又没有通过txd之前, 对data的crc进行校验.
可以给DATA赋值一个crc正确的数值, 应该txd就可以了.
一周热门 更多>