问题描述
module clks(
input clk,output [15:0] led
);
wire div2,div4,div8;
reg [2:0] count = 0;
assign div2 = count[0];
assign div4 = count[1];
assign div8 = count[2];
always @(posedge clk) count = count + 1;
endmodule
如何使用时钟打开每个LED(我有15个LED)? 我真的很难在网上找到有用的资源
initial begin
case({count})
2'b00:
led = 15'b000000000000001;
2'b01:
led = 15'b000000000000010;
...
endcase
end
这不起作用。 还是我可以做这样的事情?
led = led + 1;
解决方法
在上面的示例代码中,您将count定义为3位,但是case语句的宽度为2位。另外,您不希望使用初始语句,而要使用Always语句。
always @ (count)
begin
case(count)
3'b000 : led = 15'b000_0000_0001;
3'b001 : led = 15'b000_0000_0010;
...
endcase
end
,
我猜想“通过使用时钟”意味着每个时钟周期都要更改led,对吗?而且看起来您正在尝试按顺序对led进行编码。在这种情况下,您可以执行以下操作:
-
您需要将销售线索重置为初始值sey
15'b1
; -
每个时钟周期,您只需将其左移一个即可。您不应该在一开始就这样做(尽管有技术方法可以这样做)。使用始终阻止:
这里是一个例子:
module clks(
input clk,input reset,output reg [15:0] led
);
always @(posedge clk) begin
if (reset == 1)
led <= 15'b1;
else
led <= led << 1;
end
endmodule
在上述情况下,“ 1”将在15个时钟周期内遍历led的所有位。此后,“ led”将变为“ 0”。如果要连续循环,必须确保它再次变为“ 1”。
另一种可能性是在Always块中初始化“ led”,但它并非总是可合成的。您在这里不需要复位信号。
initial led = 15'b1;
always @(posedge clk) led <= led << 1;