基於verilog狀態機的交通燈演示


設計要求:設計一個簡易的交通燈(系統時鍾1hz共陰極)。南北方向為主干道(L3~L1),綠燈時間為29s;東西方向為次干道(L6~L4),綠燈時間為19s;在一個方向從紅燈轉綠燈前3s,另一個方向黃燈亮3s。

                      S1      S2     S3     S4     S1
  主干道           紅燈    紅燈    綠燈   黃燈   紅燈
 次干道            綠燈    黃燈    紅燈   紅燈   綠燈
                     19S     3S     29S    3S   19S


module traffic(clk,rst_n,led); input clk; input rst_n; output [5:0] led; reg [5:0] led; reg [4:0] time_left; reg [1:0] state; parameter [1:0] S1 = 2'b00,
                S2 = 2'b01,
        S3 = 2'b11,
        S4 = 2'b10;
 clk_div CD( .clk(clk), .clkout(clkout) ); //分頻模塊 
    always @(posedge clk or negedge rst_n) begin
          if(!rst_n) begin state <= S1; time_left <= 5'd18;
          led <= 6'b100_001;
      end
      else
      begin
          
              case(state) S1:begin    
               if(clkout) begin        
                            if(time_left==0) begin state <= S2; time_left <= 5'd2;
                  led <= 6'b100_010; 
                 
                end
                else
                begin state <= S1; time_left <= time_left-1'b1;
                led <= 6'b100_001;
                end
                 end
              end S2:begin
               if(clkout) begin
                            if(time_left==0) begin state <= S3; time_left <= 5'd28;
                  led <= 6'b001_100; 
                end
               
                else
                begin state <= S2; time_left <= time_left-1'b1;
                led <= 6'b100_010;
                end
                 end
              end S3:begin
              if(clkout) begin
                            if(time_left==0) begin state <= S4; time_left <= 5'd2;
                  led <= 6'b010_100; 
                end
              
                else
                begin state <= S3; time_left <= time_left-1'b1;
                led <= 6'b001_100;
                end
                  end
              end S4:begin
               if(clkout) begin
                            if(time_left==0) begin state <= S1; time_left <= 5'd18;
                  led <= 6'b100_001; 
                end
              
                else
                begin state <= S4; time_left <= time_left-1'b1;
                led <= 6'b010_100;
                end
                  end
              end
          endcase
      end
      
    end
    endmodule
`timescale 1ns / 1ps //////////////////////////////////////////////////////////////////////////////////
// Company: // Engineer: // 
// Create Date: 2017/05/10 14:42:14 // Design Name: // Module Name: clk_div // Project Name: // Target Devices: // Tool Versions: // Description: // 
// Dependencies: // 
// Revision: // Revision 0.01 - File Created // Additional Comments: // 
//////////////////////////////////////////////////////////////////////////////////


module clk_div( clk, clkout ); //分頻模塊
input clk; output clkout; reg clkout; reg [31:0] cnt; always @(posedge clk) begin //板子時鍾為100MHZ,
if(cnt == 32'd9999_9999)
begin clkout <= 1'b1; 
cnt <= 32'b0;
end  
else 
begin clkout <= 1'b0;
cnt <= cnt + 32'd1; 
end 
end 


endmodule

而且這里采用全局時鍾方式,分頻后的時鍾作為使能端控制數據傳輸。經在xilinx的xc7a100板子上驗證與預想結果完全相同,設計成功。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2026 CODEPRJ.COM