[Verilog]为什么我的计数器的输出延迟了一个时钟?

时间:2014-12-01 16:59:22

标签: counter

我的部分程序如下,它是一个处理交通灯的FSM,以及寄存器"次" (从4到1)是计算交通信号灯应该亮的时间,但每当"次"计数到" 1"," times = 1"比其他"更长的时间"大约一个时钟。

例如:当时间= 4~2时,每个时钟都会计数,但是当时间= 1时,它将需要两个时钟 成为时代= 4。

有人能告诉我这个问题是怎么发生的吗?

always@(posedge clk or negedge rst  )
                if(!rst)
                    begin
                        s1<=state1;
                        A<=3'b0;
                        B<=3'b0;
                        count1<=3'd4;
                        count2<=3'd2;
                        count3<=3'd3;
                        count4<=3'd2;
                        temp<=1'b1;
                    end
                else
                    begin
                        if(temp==1)
                            begin
                                temp<=1'b0;
                                case(s1)
                                    state1:
                                        begin
                                            times<=count1;
                                            A<=3'b001;
                                            B<=3'b100;
                                            s1<=state2;
                                        end
                                    state2:
                                        begin
                                            times<=count2;
                                            A<=3'b010;
                                            B<=3'b100;
                                            s1<=state3;             
                                        end
                                    state3:
                                        begin
                                            times<=count3;
                                            A<=3'b100;
                                            B<=3'b001;
                                            s1<=state4;

                                        end
                                    state4:
                                        begin
                                            times<=count4;
                                            A<=3'b100;
                                            B<=3'b010;
                                            s1<=state1;
                                        end
                                    default:
                                        begin
                                            A[0]<=3'b000;
                                            B[0]<=3'b000;
                                        end
                                    endcase
                            end 
                        else
                            begin
                                if(times>1)
                                    times<=times-1;
                                else if(times==1)
                                    begin
                                        temp<=1'b1;//can't count averagely

                                    end
                            end
                    end

0 个答案:

没有答案