2013-03-01 29 views
3

我必須在7段LED上顯示消息以及定時器。所以我通過使用一個多路複用器來處理這個問題,並在一個狀態中顯示消息「Hi」,然後在計數器達到7500時它應該停止顯示「Hi」並開始顯示定時器。即使條件爲真,我的代碼也不會移動到下一個狀態

問題是它只顯示「嗨」,並沒有從那裏前進。

localparam [1:0] 
        idle = 2'b00, 
        starting = 2'b01, 
        time_it = 2'b10, 
        done = 2'b11; 

reg state_reg, state_next; 
reg [12:0] count_reg, count_next; //**change for simulation 

always @ (posedge clock or posedge reset) 
begin 
    if(reset) 
     begin 
      state_reg <= idle; 
      count_reg <= 0; 
     end 
    else 
     begin 
      state_reg <= state_next; 
      count_reg <= count_next; 
     end 
end 
always @ (*) 
begin 
state_next = state_reg; //default state stays the same 
count_next = count_reg; 

case(state_reg) 
    idle: 
     begin 
      //DISPLAY HI HERE 
      sel = 2'b00; 
      if(start) 
      begin 
       count_next = random; //get the random number from LFSR module 
       state_next = starting; 
      end 
     end 
    starting: 
     begin 
      if(count_next == 7500) 
      begin       //and starting from 'rand' ensures a random delay 
       outled = 1'b1; //turn on the led 
       state_next = time_it; //go to next state 
      end 

      else 
       count_next = count_reg + 1; 
     end  
    time_it: 
     begin 
       sel = 2'b01; //start the timer 
       state_next = done;     
     end 

    done: 
     begin 
      if(stop) 
       begin 
        sel = 2'b10; //stop the timer 
        outled = 1'b0; 
       end 
     end 

    endcase 

case(sel) 
    2'b00: //hi 
    begin 
     go_start = 0; //make sure timer module is off 
     regd0 = 4'd12; 
     regd1 = 4'd10; 
     regd2 = 4'd11; 
     regd3 = 4'd12; 
    end 

    2'b01: //timer 
    begin 

     go_start = 1'b1; //enable start signal to start timer 
     regd0 = reg_d0; //get values from stopwatch module 
     regd1 = reg_d1; //get values from stopwatch module 
     regd2 = reg_d2; //get values from stopwatch module 
     regd3 = reg_d3; //get values from stopwatch module 
    end 

    2'b10: //stop timer 
    begin 
     go_start = 1'b0; 
    end 

    default: 
    begin 
     regd0 = 4'bx; 
     regd1 = 4'bx; 
     regd2 = 4'bx; 
     regd3 = 4'bx; 
    end 
endcase   
end 

現在,在模擬中,case停留在00,雖然它被告知在time_it狀態,這樣做也不會改變。由於sel不變go_start未啓用,這就是爲什麼計時器從未打開。爲什麼它留在sel=00

回答

4

從1到2位寬更改狀態暫存器:

reg [1:0] state_reg, state_next; 
+0

感謝您指出了這一點。有人會認爲這樣的錯誤應該由編譯器的語法檢查=/ – ipunished 2013-03-01 07:04:32

+0

對於這個問題,我會得到一個**警告**關於LHS和RHS寬度的任務不匹配。 – Morgan 2013-03-01 10:54:19

+1

如果您希望編譯器收集到錯誤,請使用VHDL而不是Verilog :) – 2013-03-01 12:21:11

0

你能弄清楚reg「start」的定義嗎?如果它保持0,那麼肯定現在的狀態永遠不會變成「開始」狀態。

相關問題