设计让8个LED灯以每个0.5S的速率循环闪烁(跑马灯)
方案一:移位法
代码:
//第一种方案:左移位
module paomadeng(clk, rst, LED);
input clk;
input rst;
output reg[7:0] LED;
reg [24:0] count;
always @(posedge clk or negedge rst )
if(!rst) begin
LED <= 8'b0000_0001;
count <=0;
end
else if(count == 24999999) begin
count <= 0;
if(LED == 8'b1000_0000)
LED <= 8'b0000_0001;//继续从第一个LED开始
else
LED <= LED << 1;//左移1位
end
else
count <= count +1'b1;
endmodule
激励代码:
module paomadeng_tb();
reg clk;
reg rst;
wire [7:0] LED;
paomadeng paomadengDemo(
.clk(clk),
.rst(rst),
.LED(LED)
);
initial clk = 1;
always #10 clk = ~clk;
initial begin
rst = 0;
#201;
rst = 1;
#8000000000;
$stop;
end
endmodule
仿真图:
实图:
方案二:循环移位
循环移位就相当于让8’b0000_0001在时钟上升沿来临时进行一个左移操作,将移出的最高位放置在最低位上,如此便可实现0000_0001~1000_0000的循环。
代码:
//第二种方案:循环移位
module paomadeng(clk, rst, LED);
input clk;
input rst;
output reg[7:0] LED;
reg [24:0] count;
always @(posedge clk or negedge rst )
if(!rst) begin
LED <= 8'b0000_0001;
count <=0;
end
else if(count == 24999999) begin
count <= 0;
LED <= {LED[6:0] , LED[7]};//拼接语句
end
else
count <= count +1'b1;
endmodule
激励与方案一相同。
标题方案三:三八译码器
引用3-8译码器的模块,前面有3-8译码器的文章,在此不做过多解释。
代码:
//3-8译码器法
module paomadeng(clk, rst, LED);
input clk;
input rst;
output[7:0] LED;
reg[24:0] count;
reg [2:0] count1;//3-8译码器的输入信号
always@(posedge clk or negedge rst)
if(!rst)
count <= 0;
else if(count == 24999999)
count <= 0;
else
count <= count + 1'b1;
always@(posedge clk or negedge rst)
if(!rst)
count1 <= 0;
else if(count1 == 7)
count1 <= 0;
else if(count == 24999999)
count1 <= count1 + 1'b1;
//调用前面3-8译码器模块
yimaqi_3_8 yimaqiDemo(
.date_in(count1),
.out(LED)
);
endmodule
全文到此结束。