Skip to main content

traffic light verilog code


Author : Kishore papisetty

module traffic_control(n_lights,s_lights,e_lights,w_lights,clk,rst_a);

   output reg [2:0] n_lights,s_lights,e_lights,w_lights;
   input      clk;
   input      rst_a;

   reg [2:0] state;

   parameter [2:0] north=3'b000;
   parameter [2:0] north_y=3'b001;
   parameter [2:0] south=3'b010;
   parameter [2:0] south_y=3'b011;
   parameter [2:0] east=3'b100;
   parameter [2:0] east_y=3'b101;
   parameter [2:0] west=3'b110;
   parameter [2:0] west_y=3'b111;

   reg [2:0] count;


   always @(posedge clk, posedge rst_a)
     begin
        if (rst_a)
            begin
                state=north;
                count =3'b000;
            end
        else
            begin
                case (state)
                north :
                    begin
                        if (count==3'b111)
                            begin
                            count=3'b000;
                            state=north_y;
                            end
                        else
                            begin
                            count=count+3'b001;
                            state=north;
                            end
                    end

                north_y :
                    begin
                        if (count==3'b011)
                            begin
                            count=3'b000;
                            state=south;
                            end
                        else
                            begin
                            count=count+3'b001;
                            state=north_y;
                        end
                    end

               south :
                    begin
                        if (count==3'b111)
                            begin
                            count=3'b0;
                            state=south_y;
                            end
                        else
                            begin
                            count=count+3'b001;
                            state=south;
                        end
                    end

            south_y :
                begin
                    if (count==3'b011)
                        begin
                        count=3'b0;
                        state=east;
                        end
                    else
                        begin
                        count=count+3'b001;
                        state=south_y;
                        end
                    end

            east :
                begin
                    if (count==3'b111)
                        begin
                        count=3'b0;
                        state=east_y;
                        end
                    else
                        begin
                        count=count+3'b001;
                        state=east;
                        end
                    end

            east_y :
                begin
                    if (count==3'b011)
                        begin
                        count=3'b0;
                        state=west;
                        end
                    else
                        begin
                        count=count+3'b001;
                        state=east_y;
                        end
                    end

            west :
                begin
                    if (count==3'b111)
                        begin
                        state=west_y;
                        count=3'b0;
                        end
                    else
                        begin
                        count=count+3'b001;
                        state=west;
                        end
                    end

            west_y :
                begin
                    if (count==3'b011)
                        begin
                        state=north;
                        count=3'b0;
                        end
                    else
                        begin
                        count=count+3'b001;
                        state=west_y;
                        end
                    end
            endcase // case (state)
        end // always @ (state)
    end


always @(state)
     begin
         case (state)
            north :
                begin
                    n_lights = 3'b001;
                    s_lights = 3'b100;
                    e_lights = 3'b100;
                    w_lights = 3'b100;
                end // case: north

            north_y :
                begin
                    n_lights = 3'b010;
                    s_lights = 3'b100;
                    e_lights = 3'b100;
                    w_lights = 3'b100;
                end // case: north_y

            south :
                begin
                    n_lights = 3'b100;
                    s_lights = 3'b001;
                    e_lights = 3'b100;
                    w_lights = 3'b100;
                end // case: south

            south_y :
                begin
                    n_lights = 3'b100;
                    s_lights = 3'b010;
                    e_lights = 3'b100;
                    w_lights = 3'b100;
                end // case: south_y

            west :
                begin
                    n_lights = 3'b100;
                    s_lights = 3'b100;
                    e_lights = 3'b100;
                    w_lights = 3'b001;
                end // case: west

            west_y :
                begin
                    n_lights = 3'b100;
                    s_lights = 3'b100;
                    e_lights = 3'b100;
                    w_lights = 3'b010;
                end // case: west_y

            east :
                begin
                    n_lights = 3'b100;
                    s_lights = 3'b100;
                    e_lights = 3'b001;
                    w_lights = 3'b100;
                end // case: east

            east_y :
                begin
                    n_lights = 3'b100;
                    s_lights = 3'b100;
                    e_lights = 3'b010;
                    w_lights = 3'b100;
                end // case: east_y
            endcase // case (state)
     end // always @ (state)
endmodule

//test bench//

`timescale 1ns/1ps
module traffic_control_tb;

wire [2:0] n_lights,s_lights,e_lights,w_lights;
reg clk,rst_a;

traffic_control DUT (n_lights,s_lights,e_lights,w_lights,clk,rst_a);

initial
 begin
  clk=1'b1;
  forever #5 clk=~clk;
 end

initial
 begin
  rst_a=1'b1;
  #15;
  rst_a=1'b0;
  #1000;
  $stop;
 end
endmodule



Comments

Popular posts from this blog

FSM 101 in Mealy state

Author : Kishore Papisetty fsm style : mealy fsm verilog code for the sequence detector 101 in mealy state   module fsm_101(clk,rst,x,z); input clk,rst,x; output z; reg [1:0]pstate,nstate; reg z;   always@(x,pstate) case(pstate) 2'd0: if(x==1'd1) begin nstate<=2'd1; z<=1'd0; end else begin nstate<=2'd0; z<=1'd0; end 2'd1: if(x==1'd0) begin nstate<=2'd2; z<=1'd0; end else begin nstate<=2'd1; z<=1'd0; end 2'd2: if(x==1'd1) begin nstate<=2'd1; z<=1'd1; end else begin nstate<=2'd0; z<=1'd0; end endcase   always@(posedge clk) begin if(rst==1'd0) pstate<=2'd0; else pstate<=nstate; end   endmodule //TEST BENCH// `timescale 1ns / 1ps module fsm_tb; reg clk; reg rst; reg x; wire z; fsm_101 uut (

Gray code counter in VHDL

Author : Kishore Papisetty code : VHDL Gray code is the code with only bit transition between adjacent words. The direct description of Gray counter is based on the equation extraction from the truth table. Such solution of n bit counter demands 2^(n-2). product terms. Implementation may be difficult for greater width of counter. The alternative is using of auxiliary bit. The design was based on the Auxiliary bit generation. vhdl code for the gray counter library IEEE; use IEEE.STD_LOGIC_1164.ALL; use IEEE.STD_LOGIC_ARITH.ALL; use IEEE.STD_LOGIC_UNSIGNED.ALL; entity gray_counter is     Port ( clk : in  STD_LOGIC;            rst : in  STD_LOGIC;            gray_code : out  STD_LOGIC_VECTOR (3 DOWNTO 0)); end gray_counter; architecture Behavioral of gray_counter is signal count:STD_LOGIC_VECTOR(3 DOWNTO 0):="0000"; begin process(clk) begin if(rst='1') then count <= "0000"; elsif(rising_edge(clk)) then count<=count+"

VHDL code for a 3-bit binary to thermometer converter

Author : Kishore Papisetty Platform : Xilinx code : VHDL //bit binary to thermometer converter// library IEEE; use IEEE.STD_LOGIC_1164.ALL; use ieee.numeric_std.all; entity bin2therm2bit is                 port (                                 binary_input : in std_logic_vector (1 downto 0);                                 therm_output : out std_logic_vector (6 downto 0)                 ); end bin2therm6bit; architecture Behavioral of bin2therm6bit is begin                 process (binary_input)                 begin                                 label1 : case binary_input is                                                 when "000" => therm_output <= "0000000";                                                 when "001" => therm_output <= "0000001";                                                 when "010" => therm_output <= "0000011";