多种模式的LED流水灯设计
1.实现的功能要求
包含四种模式,分别是①从左到右点亮,②从右到左点亮,③从两边到中间点亮,④从中间到两边点亮。
要求四种模式依次切换,循环执行。
2.实现的HDL代码
module led_run(clk,led,rst);
input clk; //clk with low frequency like 1Hz
input rst; //system reset signal
output [11:0] led; //denotes 12 leds,
reg [11:0] led;
reg [2:0] state; //state variable,internal signals
reg [5:0] count; //control signals of the state diagram,internal signals
always @(posedge clk or posedge rst) //the state diagram
if (rst)
begin
state <= 3'b000;
count <= 6'b000000;
end
else
case(state)
3'b000:
begin
count[3:0]<= 4'b0;
count[5:4]<= count[5:4]+1'b1;
case(count[5:4])
2'b00: state <= 3'b001;
2'b01: state <= 3'b010;
2'b10: state <= 3'b011;
2'b11: state <= 3'b100;
endcase
end
3'b001:
begin
count <= count + 1'b1;
if(count[3:0] == 11)
state <= 3'b000; end
3'b010:
begin
count <= count + 1'b1;
if(count[3:0] == 11)
state <= 3'b000; end
3'b011:
begin
count <= count + 1'b1;
if(count[3:0] == 5)
state <= 3'b000; end
3'b100:
begin
count <= count + 1'b1;
if(count[3:0] == 5)
state <= 3'b000; end
default:
begin
state <= 3'b000;
count <= 6'b000000;
end
endcase
always @(posedge clk or posedge rst) //the behavior of each state
if (rst)
led <= 12'hFFF;
else
case(state)
3'b000: led <= 12'hfff;
3'b001: led <= led << 1;
3'b010: led <= led >> 1;
3'b011:
begin
led[11:6]<= led[11:6]>>1;
led[5:0]<=led[5:0]<<1;
end
3'b100:
begin
led[11:6]<= led[11:6]<<1;
led[5:0]<=led[5:0]>>1;
end
default: led<=12'hfff;
endcase
endmodule
3.实现的功能仿真截图
经Modelsim_altera仿真后的结果如下图所示:(注led低电平为亮,高电平为灭)。