Help > counter stopping

Status
Not open for further replies.

sameem_shabbir

Advanced Member level 4
Joined
Jan 5, 2008
Messages
104
Helped
5
Reputation
10
Reaction score
2
Trophy points
1,298
Location
Pakistan
Activity points
1,946
Will this code stop the counter at 12?


always @ (posedge clk)
begin
if (counter >= 12)
counter <= 12;
else
counter <= counter+1;
end


If not how should i stop it on a certain value.
 

well, analyzing the logic, yes, when reach 12, the counter assumes always 12. but, you could do as follows

if (counter < 12)
counter <= counter + 1;

unless you need to update the counter, you don´t need to keep in 12... this code above must work also. I´m not considering the fact that you are using Else, in other words, you are discarding all other solution, including negative numbers, tri-state and don´t care. good luck!!

regards

Breno
 
Both of those examples will stop at 12 when counting from a lower number, however both behave differently if 'counter' is initialized to a higher value such as 14.

If your application needs to support those higher counter values, a simple change will stop it at 12 irregardless of initial value:
if (counter != 12)
counter <= counter + 1;


Or if you like one-liners:
counter <= counter + (counter != 12);
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…