I expect a different behavior. CNT_temp isn't reset (except for a power on initialization), thus it should be increased by 100 every clock cycle until overflowing the default 32 bit number range.Here, what happens on every edge of the clock is that CNT_temp is set to 100, and hence so is CNT. So you have a circuit that sets CNT to 0 at reset and 100 during run time.
I expect a different behavior. CNT_temp isn't reset (except for a power on initialization), thus it should be increased by 100 every clock cycle until overflowing the default 32 bit number range.
In any case, a for loop doesn't implement the intended counter function.
When you say, "Did you draw the expected circuit before you wrote the code?", what do you mean?!
Also, does that mean CNT <= 0000 0000 is correct?!
...
If clk’event and clk=’1’ then
If HIGHER <= “1000” then
If LOWER <= “1000” then
LOWER <= LOWER + “0001”;
Elsif LOWER = “1001” then
LOWER <= “0000”;
HIGHER <= HIGHER + “0001”;
End if;
Elsif HIGHER = “1001” then
If LOWER <= “1000” then
LOWER <= LOWER + “0001”;
Elsif LOWER = “1001” then
LOWER <= “0000”;
HIGHER <= “0000”;
End if;
End if;
End if;
...
It may be one way to write it, but it is not a good way to write it. LOWER should have nothing to do with the value of higher, it is a repeating count of 0-9 and doesn't need to even examine HIGHER.Hi,
This is one way you could write the code for it.
I'm sorry for not including comments.
Hope it helps.Code:... If clk’event and clk=’1’ then If HIGHER <= “1000” then If LOWER <= “1000” then LOWER <= LOWER + “0001”; Elsif LOWER = “1001” then LOWER <= “0000”; HIGHER <= HIGHER + “0001”; End if; Elsif HIGHER = “1001” then If LOWER <= “1000” then LOWER <= LOWER + “0001”; Elsif LOWER = “1001” then LOWER <= “0000”; HIGHER <= “0000”; End if; End if; End if; ...
If clk’event and clk=’1’ then
If HIGHER <= “1000” then
[COLOR="#FF0000"]If LOWER <= “1000” then
LOWER <= LOWER + “0001”;
Elsif LOWER = “1001” then
LOWER <= “0000”;[/COLOR]
HIGHER <= HIGHER + “0001”;
End if;
Elsif HIGHER = “1001” then
[COLOR="#FF0000"] If LOWER <= “1000” then
LOWER <= LOWER + “0001”;
Elsif LOWER = “1001” then
LOWER <= “0000”;[/COLOR]
HIGHER <= “0000”;
End if;
End if;
End if;
I examined HIGHER alongside LOWER here because I would only want to increment it if LOWER is 9 and HIGHER is less than 9 and only clear HIGHER after 9 is counted for both LOWER and HIGHER. I don't want to count 10 already, and then clear it.It may be one way to write it, but it is not a good way to write it. LOWER should have nothing to do with the value of higher, it is a repeating count of 0-9 and doesn't need to even examine HIGHER
Besides that what happens if LOWER or HIGHER ends up with 1010 through 1111 in it? It gets stuck forever.
I would only clear if both LOWER and HIGHER register an illegal value. Otherwise, I would update one register with the other as necessary.ad-see said:I prefer counting when less than the end value and clearing otherwise.
In this case, it is easier to write an if/else for LOWER, and then a second if/elsif for HIGHER. That makes it clear that HIGHER isn't used in the logic for LOWER, and that LOWER is used in the logic for HIGHER. It also gets rid of some duplicate code.
I would only clear if both LOWER and HIGHER register an illegal value. Otherwise, I would update one register with the other as necessary.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?