如何解决在摩尔机上使输出LED闪烁
我正在设计的是一种摩尔机,可以为每种状态赋予特定的颜色。 这是我的代码的一部分。
always @(*) begin
case (state)
S0 : led = 6'b111111;
S1 : led = 6'b010100; // have to blink //
S2 : led = 6'b100010;
S3 : led = 6'b110110;
default : led_output = 6'b000000;
endcase
end
endmodule
我剩下的就是为每个状态分配led输出值。
但是S1的条件不仅是特定的颜色,而且还必须“闪烁” 1秒钟。
我已经在Google上搜索了“闪烁LED”验证码,但与我的情况没什么不同。
我不知道所需的编码块。
您能给我一些建议或答案,以补充使S1闪烁的内容吗?
解决方法
要使其闪烁,您需要区分2种状态。因此,请创建一个1位信号,例如sel
,该信号在S1
状态下进行切换,其切换速度及其占空比可以满足您在'blink' for period of 1s
中的要求。这基本上是在计数器的帮助下实现的。
reg [3:0] cnt; // assume 4-bit long
reg sel;
// counter to determine the HIGH / LOW time of sel.
always@(posedge clk or negedge resetn)begin
if(!resetn)begin
cnt <= 4'h0;
end
// since you can exit S1 state at any time using push_button,// make sure cnt is always ZERO when entering S1 state next time.
// also to save power when using clock gating,because this statement
// is only active for 1 cycle.
else if((state == S1) & ((push_button == 2'b01) | (push_button == 2'b10)))begin // S1 exit condition
cnt <= 4'h0;
end
else if(state == S1)begin
// sel HIGH lasts <HIGH_MAX_VAL>+1 cycles
// sel LOW lasts <LOW_MAX_VAL>+1 cycles
if(cnt == (sel ? <HIGH_MAX_VAL> : <LOW_MAX_VAL>))begin
cnt <= 4'h0;
end
else begin
cnt <= cnt + 4'h1;
end
end
end
always@(posedge clk or negedge resetn)begin
if(!resetn)begin
sel <= 1'h0;
end
else if((state == S1) & ((push_button == 2'b01) | (push_button == 2'b10)))begin
sel <= 1'h0;
end
else if(state == S1)begin
if(cnt == (sel ? <HIGH_MAX_VAL> : <LOW_MAX_VAL>))begin
sel <= ~sel;
end
end
end
使用sel
在2个led值之间进行选择。
always@(*)begin
case(state)
....
S1 : led = sel ? 6'b010100 : <ANOTHER_VALUE>;
....
endcase
end
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。