2. 汽车尾灯控制器
2.1 实验要求:设计一个汽车尾灯控制电路。
(1)功能:汽车左右两侧各有3个尾灯,要求控制尾灯按如下规则亮灭。
- 汽车沿直线行驶时,两侧指示灯全灭。
- 右转弯时,左侧的指示灯全灭,右侧的指示灯按000,100,010,001,000循环;
- 左转弯时,右侧的指示灯全灭,左侧的指示灯同上右侧;
- 若直行时刹车,两侧的指示灯全亮;若转弯时刹车,转弯这一侧的指示灯按上述的循环顺序点亮,另一侧指示灯全亮;
- 故障或紧急状态时,两侧的指示灯闪烁。
2.2 汽车尾灯控制器代码如下:
1 //backlight control circuit 2 //2020-11-03 3 //by YongFengXie 4 module backlight(clk50m,turnl,turnr,brake,fault, 5 lightl,lightr); 6 input clk50m; 7 input turnl; 8 input turnr; 9 input brake; 10 input fault; 11 output [2:0] lightl; 12 output [2:0] lightr; 13 reg [23:0] count; 14 wire clock; 15 reg [2:0] shift=3'b001; 16 reg flash=1'b0; 17 /* 18 always @(posedge clk50m) 19 begin 20 if(count == 12500000) 21 count<=0; 22 else 23 count<=count+1; 24 end 25 26 assign clock=count[23]; 27 */ 28 always @(posedge clk50m) 29 begin 30 shift={shift[1:0],shift[2]}; 31 flash=~flash; 32 end 33 34 assign lightl=turnl?shift:brake?3'b111:fault?{3{flash}}:3'b000; 35 assign lightr=turnr?shift:brake?3'b111:fault?{3{flash}}:3'b000; 36 37 endmodule
测试代码
//backlight control circuit testbench //2020-11-03 //by YongFengXie `timescale 1ns/1ns module backlight_tb; reg clk50m; reg turnl; reg turnr; reg brake; reg fault; wire [2:0] lightl; wire [2:0] lightr; backlight ub(clk50m,turnl,turnr,brake,fault, lightl,lightr); initial begin clk50m=0; turnl=0; turnr=0; brake=0; fault=0; #20 turnl=1; #20 brake=1; #20 fault=1; #0 turnl=0; #0 turnr=1; #20 turnr=0; #20 brake=0; #20 fault=0; #100 $stop; end always #1 clk50m=~clk50m; endmodule
用到的技巧:按优先级划分状态,用条件运算符实现。分频用N进制计数器可用作N分频器,因为计数器最高位输出脉冲频率等于时钟脉冲CP频率的1/N。这里为了仿真方便,屏蔽了clock,clock分频得到,应该是12500000*1/50000000=0.25s。