Timing The Transitions
جزء من قسم الأساسيات في رحلة Verilog على Coddy. الدرس 81 من 90.
التحدي
في هذا الدرس، ستضيف منطق counter الذي يتحكم في المدة التي يظل فيها كل ضوء مضاءً. يقوم counter بالعد التنازلي من قيمة محددة مسبقًا إلى الصفر، ثم يُفعّل تغيير state التالي.
متطلبات التوقيت
| State | المدة | قيمة counter |
|---|---|---|
| Green | 30 ثانية | 30 |
| Yellow | 10 ثوانٍ | 10 |
| Red | 40 ثانية | 40 |
كيفية عمل counter
- عندما يبدأ state، يتم تحميل قيمة المدة في counter
- في كل نبضة ساعة، ينخفض counter بمقدار 1
- عندما يصل counter إلى 0، يحين وقت التغيير إلى state التالي
مهمتك هي إضافة منطق counter المفقود إلى آلة state.
ما يجب فعله:
- أضف تعريف
reg [5:0] counter - عند reset، اضبط
counterعلى 0 - عندما يكون
counter == 0:- حمّل مدة state التالي في counter
- غيّر إلى state التالي
- وإلا، أنقص counter بمقدار 1 في كل نبضة ساعة
جرّب بنفسك
module traffic_light (
input clk,
input reset,
output reg red,
output reg yellow,
output reg green
);
reg [1:0] state;
// TODO: تعريف سجل العداد (6 بت)
// تعيينات المخرجات
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
// آلة الحالة مع التوقيت
always @(posedge clk or posedge reset) begin
if (reset) begin
state <= 2; // ابدأ عند الأحمر
// TODO: اضبط العداد إلى 0
end else begin
if (counter == 0) begin
case (state)
0: begin
state <= 1;
// TODO: حمّل العداد للأصفر (10 ثوانٍ)
end
1: begin
state <= 2;
// TODO: حمّل العداد للأحمر (40 ثانية)
end
2: begin
state <= 0;
// TODO: حمّل العداد للأخضر (30 ثانية)
end
endcase
end else begin
// TODO: أنقص العداد بمقدار 1
end
end
end
endmoduleجميع دروس الأساسيات
4Operators 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 Logicتدرّب بنفسك: مترجم Verilog عبر الإنترنت