verilog语言中怎么实现从0计数到255再计回到0,以此循环呢?
求大神提示啊 拜托拜托!
reg [7:0] cnt;
always@(posedge clk)
begin
if(rst)
begin
cnt<=8'd0;
end
else
begin
cnt<=cnt+8'd1;
end
end
你好,我是想先从0以步长1一直加到255,然后再以步长32一直减到0,这要怎么实现呢?谢谢哈!
reg[7:0] cnt;
always[url=]@(posedge[/url] clk)
begin
if(cnt<255) cnt<=cnt+1;
else if(cnt==255) cnt<=cnt-32;
end
reg [6:0]cnt;
always@(posedge clk or negedge rst_n)
begin
if(!rst_n)
begin
cnt<=0;
end
else
begin
if(cnt<255)
begin
cnt<=cnt+1;
end
else
begin
cnt<=cnt-32;
end
end
end
reg [7:0]cnt;
always@(.....)
加一个标志位,0时以1步长加,1时以32步长减。
同意楼上。
- module counter(input clk,input rst,output reg [7:0] count);
- reg flag;
- always@(posedge clk)
- begin
- if(!rst)
- begin
- count <=0;
- flag <=0;
- end
- else
- begin
- if(flag==0)
- begin
- if(count == 255)
- begin
- count<=count-31;
- flag<=1;
- end
- else count<=count+1;
- end
- else
- begin
- count <=count -32;
- if(count==0)
- flag<=0;
- end
- end
- end
谢谢分享哈!