EmbDev.net

Forum: FPGA, VHDL & Verilog How can I add the status of lights “red-yellow” in Verilog


Author: Michał W. (michalwarmuz)
Posted on:

Rate this post
0 useful
not useful
I wrote a simple school project traffic light and I want to add a new 
state of "red-yellow" lights

!https://ibb.co/vDw4DTN This picture shows how it should look like

I do not know what the "if-else" statement looks like.

Thank you in advance for your help
`timescale 1ns / 1ps
module Traffic
(
  input       reset,
  input       clk,
  input       NS_VEHICLE_DETECT,
  input       EW_VEHICLE_DETECT,
  output wire NS_RED,
  output wire NS_YELLOW,
  output wire NS_GREEN,
  output wire EW_RED,
  output wire EW_YELLOW,
  output wire EW_GREEN
);

reg [5:0] lights; //  {NS_RED, NS_YELLOW, NS_GREEN, EW_RED, EW_YELLOW, EW_GREEN}
reg [4:0] nsCounter;
reg [3:0] ewCounter;
reg [1:0] yellowCounter;


assign {NS_RED, NS_YELLOW, NS_GREEN, EW_RED, EW_YELLOW, EW_GREEN} = lights;


always @(posedge clk)
   if (reset)
      lights <= 6'b001100;
   else
     if (nsCounter == 31 & EW_VEHICLE_DETECT & NS_GREEN)
       lights <= 6'b010100;
     else
       if (ewCounter == 15 & EW_GREEN) 
         lights <= 6'b100010;
       else
         if (yellowCounter == 3 & NS_YELLOW)
           lights <= 6'b100001;
         else
           if (yellowCounter == 3 & EW_YELLOW) 
             lights <= 6'b001100;
//------------------------------------

always @(negedge clk)
  if (reset)
    begin
      nsCounter     <= 5'h0;
      ewCounter     <= 4'h0;
      yellowCounter <= 2'h0;
    end
  else
    begin
      nsCounter     <= nsCounter     + 1'b1;
      ewCounter     <= ewCounter     + 1'b1; 
      yellowCounter <= yellowCounter + 1'b1;
    end
//------------------------------------

endmodule
//====================================

Author: Lothar M. (lkmiller) (Moderator)
Posted on:

Rate this post
0 useful
not useful
Use a FSM with states for the possible light patterns. Cycle through 
those states and assign the according lights value afterwards.

BTW1:
one counter for the delay is eoungh when using a FSM for the states.

BTW2:
A question arises when seeing this:
input       NS_VEHICLE_DETECT,
input       EW_VEHICLE_DETECT,
What is the idle state of those traffic lights?
NS and EW red?
Or is the main street NS green and the lower traffic street EW red?
If the second (which would be more comfortable, but it would not force 
main street drivers to reduce speed..) then you also could ignore the NS 
vehicle detection.

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [vhdl]VHDL code[/vhdl]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig