Timing The Transitions
Parte de la sección Fundamentos del Journey de Verilog de Coddy. Lección 81 de 90.
Desafío
En esta lección, añadirás la lógica del counter que controla cuánto tiempo permanece encendida cada luz. El counter cuenta hacia atrás desde un valor preestablecido hasta cero y después activa el siguiente cambio de state.
Requisitos de timing
| State | Duración | Valor del counter |
|---|---|---|
| Green | 30 segundos | 30 |
| Yellow | 10 segundos | 10 |
| Red | 40 segundos | 40 |
Cómo funciona el counter
- Cuando comienza un state, el counter se carga con el valor de duración
- En cada pulso de reloj, el counter disminuye en 1
- Cuando el counter llega a 0, es momento de cambiar al siguiente state
Tu tarea es añadir la lógica del counter que falta a la máquina de state.
Qué hacer:
- Añade una declaración
reg [5:0] counter - Al hacer reset, establece
counteren 0 - Cuando
counter == 0:- Carga la duración del siguiente state en el counter
- Cambia al siguiente state
- De lo contrario, decrementa el counter en 1 en cada ciclo de reloj
Pruébalo tú mismo
module traffic_light (
input clk,
input reset,
output reg red,
output reg yellow,
output reg green
);
reg [1:0] state;
// TODO: Declarar registro counter (6 bits)
// Asignaciones de salida
always @(*) begin
case (state)
0: begin green = 1; yellow = 0; red = 0; end
1: begin green = 0; yellow = 1; red = 0; end
2: begin green = 0; yellow = 0; red = 1; end
default: begin green = 0; yellow = 0; red = 1; end
endcase
end
// Máquina de estados con temporización
always @(posedge clk or posedge reset) begin
if (reset) begin
state <= 2; // Comienza en Rojo
// TODO: Establecer counter a 0
end else begin
if (counter == 0) begin
case (state)
0: begin
state <= 1;
// TODO: Cargar counter para Amarillo (10 segundos)
end
1: begin
state <= 2;
// TODO: Cargar counter para Rojo (40 segundos)
end
2: begin
state <= 0;
// TODO: Cargar counter para Verde (30 segundos)
end
endcase
end else begin
// TODO: Decrementar counter en 1
end
end
end
endmoduleTodas las lecciones de Fundamentos
1Introducción
Qué es VerilogHardware frente a softwareTu primer móduloComentariosNiveles de abstracción del diseño4Operators Part 1
Arithmetic OperatorsModulo OperatorComparison OperatorsRecap - Simple MathBitwise Operators7Assign And Gates
Continuous AssignmentAssign With OperatorsBuilt In Gate PrimitivesAND OR NOT GatesXOR XNOR GatesRecap - Logic Gate Circuit10Decision Making
If StatementIf - ElseRecap - Simple ComparatorCase StatementCasex And CasezRecap - ALU Design5Operators Part 2
Logical OperatorsReduction OperatorsShift OperatorsConcatenation OperatorConditional OperatorRecap - Operator Challenge3Number System
Binary RepresentationSized NumbersUnsized NumbersNegative NumbersSpecial Values X And ZRecap - Number Formats6Modules
Module StructureInput And Output PortsInout PortsModule InstantiationPort Mapping By NamePort Mapping By OrderRecap - Build A Module9Procedural Blocks
Always BlockInitial BlockSensitivity ListBlocking AssignmentNon Blocking AssignmentRecap - Always vs Initial15Traffic Light Controller
Defining The StatesState Machine LogicPractica por tu cuenta: Compilador de Verilog online